Skip to content

Commit

Permalink
Adding support for docvalue_fields in msearch (#1429) (#1432)
Browse files Browse the repository at this point in the history
* Adding support for docvalue_fields in msearch

Signed-off-by: Ankit Jain <akjain@amazon.com>

* Add changelog and spotless fixes

Signed-off-by: Ankit Jain <akjain@amazon.com>

* Fixing integration test failures

Signed-off-by: Ankit Jain <akjain@amazon.com>

---------

Signed-off-by: Ankit Jain <akjain@amazon.com>
(cherry picked from commit 0521c3c)
  • Loading branch information
jainankitk authored Feb 13, 2025
1 parent 8756018 commit 0bdca10
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased 2.x]
### Added
- Added support for `GeoDistanceQuery`'s `ignore_unmapped` property ([#1430](https://github.com/opensearch-project/opensearch-java/pull/1430))
- Added support for docvalue_fields in msearch ([#1429](https://github.com/opensearch-project/opensearch-java/pull/1429)])

### Dependencies
- Bump `commons-logging:commons-logging` from 1.3.4 to 1.3.5 ([#1418](https://github.com/opensearch-project/opensearch-java/pull/1418))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class MultisearchBody implements PlainJsonSerializable {

private final List<FieldAndFormat> fields;

private final List<FieldAndFormat> docvalueFields;

private final List<Map<String, Double>> indicesBoost;

@Nullable
Expand Down Expand Up @@ -150,6 +152,7 @@ private MultisearchBody(Builder builder) {
this.storedFields = ApiTypeHelper.unmodifiable(builder.storedFields);
this.explain = builder.explain;
this.fields = ApiTypeHelper.unmodifiable(builder.fields);
this.docvalueFields = ApiTypeHelper.unmodifiable(builder.docvalueFields);
this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost);
this.collapse = builder.collapse;
this.version = builder.version;
Expand Down Expand Up @@ -322,6 +325,16 @@ public final List<FieldAndFormat> fields() {
return this.fields;
}

/**
* Array of wildcard (*) patterns. The request returns doc values for field
* names matching these patterns in the hits.fields property of the response.
* <p>
* API name: {@code docvalue_fields}
*/
public final List<FieldAndFormat> docvalueFields() {
return this.docvalueFields;
}

/**
* Boosts the _score of documents from specified indices.
* <p>
Expand Down Expand Up @@ -513,6 +526,17 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeEnd();
}

if (ApiTypeHelper.isDefined(this.docvalueFields)) {
generator.writeKey("docvalue_fields");
generator.writeStartArray();
for (FieldAndFormat item0 : this.docvalueFields) {
item0.serialize(generator, mapper);

}
generator.writeEnd();

}

if (ApiTypeHelper.isDefined(this.indicesBoost)) {
generator.writeKey("indices_boost");
generator.writeStartArray();
Expand Down Expand Up @@ -628,6 +652,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
@Nullable
private List<FieldAndFormat> fields;

@Nullable
private List<FieldAndFormat> docvalueFields;

@Nullable
private List<Map<String, Double>> indicesBoost;

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

/**
* Array of wildcard (*) patterns. The request returns doc values for field
* names matching these patterns in the hits.fields property of the response.
* <p>
* API name: {@code docvalue_fields}
* <p>
* Adds all elements of <code>list</code> to <code>docvalueFields</code>.
*/
public final Builder docvalueFields(List<FieldAndFormat> list) {
this.docvalueFields = _listAddAll(this.docvalueFields, list);
return this;
}

/**
* Array of wildcard (*) patterns. The request returns doc values for field
* names matching these patterns in the hits.fields property of the response.
* <p>
* API name: {@code docvalue_fields}
* <p>
* Adds one or more values to <code>docvalueFields</code>.
*/
public final Builder docvalueFields(FieldAndFormat value, FieldAndFormat... values) {
this.docvalueFields = _listAdd(this.docvalueFields, value, values);
return this;
}

/**
* Array of wildcard (*) patterns. The request returns doc values for field
* names matching these patterns in the hits.fields property of the response.
* <p>
* API name: {@code docvalue_fields}
* <p>
* Adds a value to <code>docvalueFields</code> using a builder lambda.
*/
public final Builder docvalueFields(Function<FieldAndFormat.Builder, ObjectBuilder<FieldAndFormat>> fn) {
return docvalueFields(fn.apply(new FieldAndFormat.Builder()).build());
}

/**
* Boosts the _score of documents from specified indices.
* <p>
Expand Down Expand Up @@ -1146,6 +1211,7 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
op.add(Builder::storedFields, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "stored_fields");
op.add(Builder::explain, JsonpDeserializer.booleanDeserializer(), "explain");
op.add(Builder::fields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "fields");
op.add(Builder::docvalueFields, JsonpDeserializer.arrayDeserializer(FieldAndFormat._DESERIALIZER), "docvalue_fields");
op.add(
Builder::indicesBoost,
JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringMapDeserializer(JsonpDeserializer.doubleDeserializer())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ public void shouldReturnMultiSearchesFields() throws Exception {
assertThat(response.responses().get(0).result().hits().hits().get(2).fields(), hasKey("name"));
}

@Test
public void shouldReturnMultiSearchesDocvalueFields() throws Exception {
String index = "multiple_searches_request_docvalue_fields";
createTestDocuments(index);

RequestItem sortedItemsQuery = createMSearchFuzzyRequest(b -> b.docvalueFields(FieldAndFormat.of(f -> f.field("quantity"))));

MsearchResponse<ShopItem> response = sendMSearchRequest(index, List.of(sortedItemsQuery));
assertEquals(1, response.responses().size());
assertEquals(3, response.responses().get(0).result().hits().hits().size());
assertThat(response.responses().get(0).result().hits().hits().get(0).fields(), hasKey("quantity"));
assertThat(response.responses().get(0).result().hits().hits().get(1).fields(), hasKey("quantity"));
assertThat(response.responses().get(0).result().hits().hits().get(2).fields(), hasKey("quantity"));
}

@Test
public void shouldReturnMultiSearchesStoredFields() throws Exception {
String index = "multiple_searches_request_stored_fields";
Expand Down

0 comments on commit 0bdca10

Please sign in to comment.