Skip to content

Commit 0521c3c

Browse files
authored
Adding support for docvalue_fields in msearch (#1429)
* 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]>
1 parent 5671aca commit 0521c3c

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
@@ -13,6 +13,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
1313
### Added
1414
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
1515
- Added `toBuilder()` and `Builder.copy()` methods to all generated classes ([#1300](https://github.com/opensearch-project/opensearch-java/pull/1300))
16+
- Added support for docvalue_fields in msearch ([#1429](https://github.com/opensearch-project/opensearch-java/pull/1429)])
1617

1718
### Dependencies
1819

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

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

113113
private final List<FieldAndFormat> fields;
114114

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

117119
@Nullable
@@ -149,6 +151,7 @@ private MultisearchBody(Builder builder) {
149151
this.storedFields = ApiTypeHelper.unmodifiable(builder.storedFields);
150152
this.explain = builder.explain;
151153
this.fields = ApiTypeHelper.unmodifiable(builder.fields);
154+
this.docvalueFields = ApiTypeHelper.unmodifiable(builder.docvalueFields);
152155
this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost);
153156
this.collapse = builder.collapse;
154157
this.version = builder.version;
@@ -310,6 +313,16 @@ public final List<FieldAndFormat> fields() {
310313
return this.fields;
311314
}
312315

316+
/**
317+
* Array of wildcard (*) patterns. The request returns doc values for field
318+
* names matching these patterns in the hits.fields property of the response.
319+
* <p>
320+
* API name: {@code docvalue_fields}
321+
*/
322+
public final List<FieldAndFormat> docvalueFields() {
323+
return this.docvalueFields;
324+
}
325+
313326
/**
314327
* Boosts the _score of documents from specified indices.
315328
* <p>
@@ -501,6 +514,17 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
501514
generator.writeEnd();
502515
}
503516

517+
if (ApiTypeHelper.isDefined(this.docvalueFields)) {
518+
generator.writeKey("docvalue_fields");
519+
generator.writeStartArray();
520+
for (FieldAndFormat item0 : this.docvalueFields) {
521+
item0.serialize(generator, mapper);
522+
523+
}
524+
generator.writeEnd();
525+
526+
}
527+
504528
if (ApiTypeHelper.isDefined(this.indicesBoost)) {
505529
generator.writeKey("indices_boost");
506530
generator.writeStartArray();
@@ -616,6 +640,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
616640
@Nullable
617641
private List<FieldAndFormat> fields;
618642

643+
@Nullable
644+
private List<FieldAndFormat> docvalueFields;
645+
619646
@Nullable
620647
private List<Map<String, Double>> indicesBoost;
621648

@@ -953,6 +980,44 @@ public final Builder fields(Function<FieldAndFormat.Builder, ObjectBuilder<Field
953980
return fields(fn.apply(new FieldAndFormat.Builder()).build());
954981
}
955982

983+
/**
984+
* Array of wildcard (*) patterns. The request returns doc values for field
985+
* names matching these patterns in the hits.fields property of the response.
986+
* <p>
987+
* API name: {@code docvalue_fields}
988+
* <p>
989+
* Adds all elements of <code>list</code> to <code>docvalueFields</code>.
990+
*/
991+
public final Builder docvalueFields(List<FieldAndFormat> list) {
992+
this.docvalueFields = _listAddAll(this.docvalueFields, list);
993+
return this;
994+
}
995+
996+
/**
997+
* Array of wildcard (*) patterns. The request returns doc values for field
998+
* names matching these patterns in the hits.fields property of the response.
999+
* <p>
1000+
* API name: {@code docvalue_fields}
1001+
* <p>
1002+
* Adds one or more values to <code>docvalueFields</code>.
1003+
*/
1004+
public final Builder docvalueFields(FieldAndFormat value, FieldAndFormat... values) {
1005+
this.docvalueFields = _listAdd(this.docvalueFields, value, values);
1006+
return this;
1007+
}
1008+
1009+
/**
1010+
* Array of wildcard (*) patterns. The request returns doc values for field
1011+
* names matching these patterns in the hits.fields property of the response.
1012+
* <p>
1013+
* API name: {@code docvalue_fields}
1014+
* <p>
1015+
* Adds a value to <code>docvalueFields</code> using a builder lambda.
1016+
*/
1017+
public final Builder docvalueFields(Function<FieldAndFormat.Builder, ObjectBuilder<FieldAndFormat>> fn) {
1018+
return docvalueFields(fn.apply(new FieldAndFormat.Builder()).build());
1019+
}
1020+
9561021
/**
9571022
* Boosts the _score of documents from specified indices.
9581023
* <p>
@@ -1106,6 +1171,7 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
11061171
op.add(Builder::storedFields, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "stored_fields");
11071172
op.add(Builder::explain, JsonpDeserializer.booleanDeserializer(), "explain");
11081173
op.add(Builder::fields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "fields");
1174+
op.add(Builder::docvalueFields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "docvalue_fields");
11091175
op.add(
11101176
Builder::indicesBoost,
11111177
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
@@ -247,6 +247,21 @@ public void shouldReturnMultiSearchesFields() throws Exception {
247247
assertThat(response.responses().get(0).result().hits().hits().get(2).fields(), hasKey("name"));
248248
}
249249

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

0 commit comments

Comments
 (0)