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

Add missed fields to MultisearchBody: collapse, version, timeout #916

Merged
merged 1 commit into from
Apr 4, 2024
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 @@ -35,6 +35,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Added
- Add missed fields to MultisearchBody: seqNoPrimaryTerm, storedFields, explain, fields, indicesBoost ([#914](https://github.com/opensearch-project/opensearch-java/pull/914))
- Add missed fields to MultisearchBody: collapse, version, timeout ([#916](https://github.com/opensearch-project/opensearch-java/pull/916)

### Dependencies
- Bumps `io.github.classgraph:classgraph` from 4.8.161 to 4.8.165
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch.core.SearchRequest.Builder;
import org.opensearch.client.opensearch.core.search.FieldCollapse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.SourceConfig;
import org.opensearch.client.opensearch.core.search.Suggester;
Expand Down Expand Up @@ -111,6 +112,15 @@ public class MultisearchBody implements JsonpSerializable {

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

@Nullable
private final FieldCollapse collapse;

@Nullable
private final Boolean version;

@Nullable
private final String timeout;

// ---------------------------------------------------------------------------------------------

private MultisearchBody(Builder builder) {
Expand All @@ -134,6 +144,9 @@ private MultisearchBody(Builder builder) {
this.explain = builder.explain;
this.fields = ApiTypeHelper.unmodifiable(builder.fields);
this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost);
this.collapse = builder.collapse;
this.version = builder.version;
this.timeout = builder.timeout;
}

public static MultisearchBody of(Function<Builder, ObjectBuilder<MultisearchBody>> fn) {
Expand Down Expand Up @@ -298,6 +311,36 @@ public final List<Map<String, Double>> indicesBoost() {
return this.indicesBoost;
}

/**
* API name: {@code collapse}
*/
@Nullable
public final FieldCollapse collapse() {
return this.collapse;
}

/**
* If true, returns document version as part of a hit.
* <p>
* API name: {@code version}
*/
@Nullable
public final Boolean version() {
return this.version;
}

/**
* Specifies the period of time to wait for a response from each shard. If no
* response is received before the timeout expires, the request fails and
* returns an error. Defaults to no timeout.
* <p>
* API name: {@code timeout}
*/
@Nullable
public final String timeout() {
return this.timeout;
}

/**
* Serialize this object to JSON.
*/
Expand Down Expand Up @@ -453,6 +496,21 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}
generator.writeEnd();
}

if (this.collapse != null) {
generator.writeKey("collapse");
this.collapse.serialize(generator, mapper);
}

if (this.version != null) {
generator.writeKey("version");
generator.write(this.version);
}

if (this.timeout != null) {
generator.writeKey("timeout");
generator.write(this.timeout);
}
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -518,6 +576,15 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
@Nullable
private List<Map<String, Double>> indicesBoost;

@Nullable
private FieldCollapse collapse;

@Nullable
private Boolean version;

@Nullable
private String timeout;

/**
* API name: {@code aggregations}
* <p>
Expand Down Expand Up @@ -861,6 +928,43 @@ public final Builder indicesBoost(Map<String, Double> value, Map<String, Double>
return this;
}

/**
* API name: {@code collapse}
*/
public final Builder collapse(@Nullable FieldCollapse value) {
this.collapse = value;
return this;
}

/**
* API name: {@code collapse}
*/
public final Builder collapse(Function<FieldCollapse.Builder, ObjectBuilder<FieldCollapse>> fn) {
return this.collapse(fn.apply(new FieldCollapse.Builder()).build());
}

/**
* If true, returns document version as part of a hit.
* <p>
* API name: {@code version}
*/
public final Builder version(@Nullable Boolean value) {
this.version = value;
return this;
}

/**
* Specifies the period of time to wait for a response from each shard. If no
* response is received before the timeout expires, the request fails and
* returns an error. Defaults to no timeout.
* <p>
* API name: {@code timeout}
*/
public final Builder timeout(@Nullable String value) {
this.timeout = value;
return this;
}

/**
* Builds a {@link MultisearchBody}.
*
Expand Down Expand Up @@ -909,6 +1013,9 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringMapDeserializer(JsonpDeserializer.doubleDeserializer())),
"indices_boost"
);
op.add(Builder::collapse, FieldCollapse._DESERIALIZER, "collapse");
op.add(Builder::version, JsonpDeserializer.booleanDeserializer(), "version");
op.add(Builder::timeout, JsonpDeserializer.stringDeserializer(), "timeout");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.opensearch.client.opensearch.core.msearch.MultiSearchResponseItem;
import org.opensearch.client.opensearch.core.msearch.MultisearchBody;
import org.opensearch.client.opensearch.core.msearch.RequestItem;
import org.opensearch.client.opensearch.core.search.FieldCollapse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.HighlightField;
import org.opensearch.client.opensearch.core.search.Hit;
Expand Down Expand Up @@ -269,6 +270,19 @@ public void shouldReturnMultiSearchesIndicesBoost() throws Exception {
assertEquals(3, response.responses().get(0).result().hits().hits().size());
}

public void shouldReturnMultiSearchesCollapse() throws Exception {
String index = "multiple_searches_request_indices_boost";
createTestDocuments(index);

RequestItem sortedItemsQuery = createMSearchFuzzyRequest(
b -> b.collapse(FieldCollapse.of(f -> f.field("name"))).version(true).timeout("5s")
);

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

private void assertResponseSources(MultiSearchResponseItem<ShopItem> response) {
List<Hit<ShopItem>> hitsWithHighlights = response.result().hits().hits();
assertEquals(2, hitsWithHighlights.size());
Expand Down
Loading