Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for docvalue_fields in msearch #1429

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Added
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
- Added `toBuilder()` and `Builder.copy()` methods to all generated classes ([#1300](https://github.com/opensearch-project/opensearch-java/pull/1300))
- Added support for docvalue_fields in msearch ([#1429](https://github.com/opensearch-project/opensearch-java/pull/1429)])

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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 @@ -149,6 +151,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 @@ -310,6 +313,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 @@ -501,6 +514,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 @@ -616,6 +640,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 @@ -953,6 +980,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 @@ -1106,6 +1171,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 @@ -247,6 +247,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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration tests are failing on the .result() call as the response is a failure for some reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the integration tests, essentially doc value fields are not allowed on text fields (name). Changing that to quantity fixed the issue

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
Loading