Skip to content

Commit 0bdca10

Browse files
authored
Adding support for docvalue_fields in msearch (#1429) (#1432)
* Adding support for docvalue_fields in msearch Signed-off-by: Ankit Jain <[email protected]> * Add changelog and spotless fixes Signed-off-by: Ankit Jain <[email protected]> * Fixing integration test failures Signed-off-by: Ankit Jain <[email protected]> --------- Signed-off-by: Ankit Jain <[email protected]> (cherry picked from commit 0521c3c)
1 parent 8756018 commit 0bdca10

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
44
## [Unreleased 2.x]
55
### Added
66
- Added support for `GeoDistanceQuery`'s `ignore_unmapped` property ([#1430](https://github.com/opensearch-project/opensearch-java/pull/1430))
7+
- Added support for docvalue_fields in msearch ([#1429](https://github.com/opensearch-project/opensearch-java/pull/1429)])
78

89
### Dependencies
910
- Bump `commons-logging:commons-logging` from 1.3.4 to 1.3.5 ([#1418](https://github.com/opensearch-project/opensearch-java/pull/1418))

java-client/src/main/java/org/opensearch/client/opensearch/core/msearch/MultisearchBody.java

+66
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public class MultisearchBody implements PlainJsonSerializable {
113113

114114
private final List<FieldAndFormat> fields;
115115

116+
private final List<FieldAndFormat> docvalueFields;
117+
116118
private final List<Map<String, Double>> indicesBoost;
117119

118120
@Nullable
@@ -150,6 +152,7 @@ private MultisearchBody(Builder builder) {
150152
this.storedFields = ApiTypeHelper.unmodifiable(builder.storedFields);
151153
this.explain = builder.explain;
152154
this.fields = ApiTypeHelper.unmodifiable(builder.fields);
155+
this.docvalueFields = ApiTypeHelper.unmodifiable(builder.docvalueFields);
153156
this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost);
154157
this.collapse = builder.collapse;
155158
this.version = builder.version;
@@ -322,6 +325,16 @@ public final List<FieldAndFormat> fields() {
322325
return this.fields;
323326
}
324327

328+
/**
329+
* Array of wildcard (*) patterns. The request returns doc values for field
330+
* names matching these patterns in the hits.fields property of the response.
331+
* <p>
332+
* API name: {@code docvalue_fields}
333+
*/
334+
public final List<FieldAndFormat> docvalueFields() {
335+
return this.docvalueFields;
336+
}
337+
325338
/**
326339
* Boosts the _score of documents from specified indices.
327340
* <p>
@@ -513,6 +526,17 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
513526
generator.writeEnd();
514527
}
515528

529+
if (ApiTypeHelper.isDefined(this.docvalueFields)) {
530+
generator.writeKey("docvalue_fields");
531+
generator.writeStartArray();
532+
for (FieldAndFormat item0 : this.docvalueFields) {
533+
item0.serialize(generator, mapper);
534+
535+
}
536+
generator.writeEnd();
537+
538+
}
539+
516540
if (ApiTypeHelper.isDefined(this.indicesBoost)) {
517541
generator.writeKey("indices_boost");
518542
generator.writeStartArray();
@@ -628,6 +652,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
628652
@Nullable
629653
private List<FieldAndFormat> fields;
630654

655+
@Nullable
656+
private List<FieldAndFormat> docvalueFields;
657+
631658
@Nullable
632659
private List<Map<String, Double>> indicesBoost;
633660

@@ -993,6 +1020,44 @@ public final Builder fields(Function<FieldAndFormat.Builder, ObjectBuilder<Field
9931020
return fields(fn.apply(new FieldAndFormat.Builder()).build());
9941021
}
9951022

1023+
/**
1024+
* Array of wildcard (*) patterns. The request returns doc values for field
1025+
* names matching these patterns in the hits.fields property of the response.
1026+
* <p>
1027+
* API name: {@code docvalue_fields}
1028+
* <p>
1029+
* Adds all elements of <code>list</code> to <code>docvalueFields</code>.
1030+
*/
1031+
public final Builder docvalueFields(List<FieldAndFormat> list) {
1032+
this.docvalueFields = _listAddAll(this.docvalueFields, list);
1033+
return this;
1034+
}
1035+
1036+
/**
1037+
* Array of wildcard (*) patterns. The request returns doc values for field
1038+
* names matching these patterns in the hits.fields property of the response.
1039+
* <p>
1040+
* API name: {@code docvalue_fields}
1041+
* <p>
1042+
* Adds one or more values to <code>docvalueFields</code>.
1043+
*/
1044+
public final Builder docvalueFields(FieldAndFormat value, FieldAndFormat... values) {
1045+
this.docvalueFields = _listAdd(this.docvalueFields, value, values);
1046+
return this;
1047+
}
1048+
1049+
/**
1050+
* Array of wildcard (*) patterns. The request returns doc values for field
1051+
* names matching these patterns in the hits.fields property of the response.
1052+
* <p>
1053+
* API name: {@code docvalue_fields}
1054+
* <p>
1055+
* Adds a value to <code>docvalueFields</code> using a builder lambda.
1056+
*/
1057+
public final Builder docvalueFields(Function<FieldAndFormat.Builder, ObjectBuilder<FieldAndFormat>> fn) {
1058+
return docvalueFields(fn.apply(new FieldAndFormat.Builder()).build());
1059+
}
1060+
9961061
/**
9971062
* Boosts the _score of documents from specified indices.
9981063
* <p>
@@ -1146,6 +1211,7 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
11461211
op.add(Builder::storedFields, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "stored_fields");
11471212
op.add(Builder::explain, JsonpDeserializer.booleanDeserializer(), "explain");
11481213
op.add(Builder::fields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "fields");
1214+
op.add(Builder::docvalueFields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "docvalue_fields");
11491215
op.add(
11501216
Builder::indicesBoost,
11511217
JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringMapDeserializer(JsonpDeserializer.doubleDeserializer())),

java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractMultiSearchRequestIT.java

+15
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ public void shouldReturnMultiSearchesFields() throws Exception {
246246
assertThat(response.responses().get(0).result().hits().hits().get(2).fields(), hasKey("name"));
247247
}
248248

249+
@Test
250+
public void shouldReturnMultiSearchesDocvalueFields() throws Exception {
251+
String index = "multiple_searches_request_docvalue_fields";
252+
createTestDocuments(index);
253+
254+
RequestItem sortedItemsQuery = createMSearchFuzzyRequest(b -> b.docvalueFields(FieldAndFormat.of(f -> f.field("quantity"))));
255+
256+
MsearchResponse<ShopItem> response = sendMSearchRequest(index, List.of(sortedItemsQuery));
257+
assertEquals(1, response.responses().size());
258+
assertEquals(3, response.responses().get(0).result().hits().hits().size());
259+
assertThat(response.responses().get(0).result().hits().hits().get(0).fields(), hasKey("quantity"));
260+
assertThat(response.responses().get(0).result().hits().hits().get(1).fields(), hasKey("quantity"));
261+
assertThat(response.responses().get(0).result().hits().hits().get(2).fields(), hasKey("quantity"));
262+
}
263+
249264
@Test
250265
public void shouldReturnMultiSearchesStoredFields() throws Exception {
251266
String index = "multiple_searches_request_stored_fields";

0 commit comments

Comments
 (0)