Skip to content

Commit

Permalink
Fixes opensearch-project#1116 - Add cancel_after_time_interval (opens…
Browse files Browse the repository at this point in the history
…earch-project#1147)

* Fixes opensearch-project#1116

Signed-off-by: uri.nudelman <[email protected]>

* Adds MsearchRequest. Fixes SearchRequest.

Signed-off-by: uri.nudelman <[email protected]>

* Update CHANGELOG.md

Signed-off-by: uri.nudelman <[email protected]>

* spotlessJavaCheck

Signed-off-by: uri.nudelman <[email protected]>

* Adds Builder Function method in MSearch

Signed-off-by: uri.nudelman <[email protected]>

---------

Signed-off-by: uri.nudelman <[email protected]>
Co-authored-by: uri.nudelman <[email protected]>
  • Loading branch information
urinud and uriofferup authored Aug 21, 2024
1 parent 08e7e65 commit c84ad24
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
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
- Adds `queryImage` (query_image) field to `NeuralQuery`, following definition in ([Neural Query](https://opensearch.org/docs/latest/query-dsl/specialized/neural/)) ([#1137](https://github.com/opensearch-project/opensearch-java/pull/1138))
- Adds `cancelAfterTimeInterval` to `SearchRequest` and `MsearchRequest` ([#1147](https://github.com/opensearch-project/opensearch-java/pull/1147))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensearch.client.opensearch._types.ExpandWildcard;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.SearchType;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch.core.msearch.RequestItem;
import org.opensearch.client.transport.Endpoint;
import org.opensearch.client.transport.endpoints.SimpleEndpoint;
Expand All @@ -66,6 +67,9 @@ public class MsearchRequest extends RequestBase implements NdJsonpSerializable,
@Nullable
private final Boolean allowNoIndices;

@Nullable
private final Time cancelAfterTimeInterval;

@Nullable
private final Boolean ccsMinimizeRoundtrips;

Expand Down Expand Up @@ -98,6 +102,7 @@ public class MsearchRequest extends RequestBase implements NdJsonpSerializable,
private MsearchRequest(Builder builder) {

this.allowNoIndices = builder.allowNoIndices;
this.cancelAfterTimeInterval = builder.cancelAfterTimeInterval;
this.ccsMinimizeRoundtrips = builder.ccsMinimizeRoundtrips;
this.expandWildcards = ApiTypeHelper.unmodifiable(builder.expandWildcards);
this.ignoreThrottled = builder.ignoreThrottled;
Expand Down Expand Up @@ -134,6 +139,17 @@ public final Boolean allowNoIndices() {
return this.allowNoIndices;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
@Nullable
public final Time cancelAfterTimeInterval() {
return this.cancelAfterTimeInterval;
}

/**
* If true, network roundtrips between the coordinating node and remote clusters
* are minimized for cross-cluster search requests.
Expand Down Expand Up @@ -256,6 +272,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {

public Builder toBuilder() {
return new Builder().allowNoIndices(allowNoIndices)
.cancelAfterTimeInterval(cancelAfterTimeInterval)
.ccsMinimizeRoundtrips(ccsMinimizeRoundtrips)
.expandWildcards(expandWildcards)
.ignoreThrottled(ignoreThrottled)
Expand All @@ -278,6 +295,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
@Nullable
private Boolean allowNoIndices;

@Nullable
private Time cancelAfterTimeInterval;

@Nullable
private Boolean ccsMinimizeRoundtrips;

Expand Down Expand Up @@ -321,6 +341,27 @@ public final Builder allowNoIndices(@Nullable Boolean value) {
return this;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
public final Builder cancelAfterTimeInterval(@Nullable Time value) {
this.cancelAfterTimeInterval = value;
return this;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
public final Builder cancelAfterTimeInterval(Function<Time.Builder, ObjectBuilder<Time>> fn) {
return this.cancelAfterTimeInterval(fn.apply(new Time.Builder()).build());
}

/**
* If true, network roundtrips between the coordinating node and remote clusters
* are minimized for cross-cluster search requests.
Expand Down Expand Up @@ -540,6 +581,9 @@ public MsearchRequest build() {
request -> {
Map<String, String> params = new HashMap<>();
params.put("typed_keys", "true");
if (request.cancelAfterTimeInterval != null) {
params.put("cancel_after_time_interval", request.cancelAfterTimeInterval._toJsonString());
}
if (request.preFilterShardSize != null) {
params.put("pre_filter_shard_size", String.valueOf(request.preFilterShardSize));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class SearchRequest extends RequestBase implements PlainJsonSerializable
@Nullable
private final String pipeline;

@Nullable
private final Time cancelAfterTimeInterval;

@Nullable
private final FieldCollapse collapse;

Expand Down Expand Up @@ -246,6 +249,7 @@ private SearchRequest(Builder builder) {
this.ccsMinimizeRoundtrips = builder.ccsMinimizeRoundtrips;
this.phaseTook = builder.phaseTook;
this.pipeline = builder.pipeline;
this.cancelAfterTimeInterval = builder.cancelAfterTimeInterval;
this.collapse = builder.collapse;
this.defaultOperator = builder.defaultOperator;
this.df = builder.df;
Expand Down Expand Up @@ -405,6 +409,17 @@ public final String pipeline() {
return this.pipeline;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
@Nullable
public final Time cancelAfterTimeInterval() {
return this.cancelAfterTimeInterval;
}

/**
* API name: {@code collapse}
*/
Expand Down Expand Up @@ -1114,6 +1129,7 @@ public Builder toBuilder() {
.ccsMinimizeRoundtrips(ccsMinimizeRoundtrips)
.phaseTook(phaseTook)
.pipeline(pipeline)
.cancelAfterTimeInterval(cancelAfterTimeInterval)
.collapse(collapse)
.defaultOperator(defaultOperator)
.df(df)
Expand Down Expand Up @@ -1198,6 +1214,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<S
@Nullable
private String pipeline;

@Nullable
private Time cancelAfterTimeInterval;

@Nullable
private FieldCollapse collapse;

Expand Down Expand Up @@ -1472,6 +1491,27 @@ public final Builder pipeline(@Nullable String value) {
return this;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
public final Builder cancelAfterTimeInterval(@Nullable Time value) {
this.cancelAfterTimeInterval = value;
return this;
}

/**
* The time after which the search request will be canceled.
* Request-level parameter takes precedence over cancel_after_time_interval cluster setting.
* <p>
* API name: {@code cancel_after_time_interval}
*/
public final Builder cancelAfterTimeInterval(Function<Time.Builder, ObjectBuilder<Time>> fn) {
return this.cancelAfterTimeInterval(fn.apply(new Time.Builder()).build());
}

/**
* API name: {@code collapse}
*/
Expand Down Expand Up @@ -2405,6 +2445,9 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer<SearchRe
if (request.scroll != null) {
params.put("scroll", request.scroll._toJsonString());
}
if (request.cancelAfterTimeInterval != null) {
params.put("cancel_after_time_interval", request.cancelAfterTimeInterval._toJsonString());
}
if (request.searchType != null) {
params.put("search_type", request.searchType.jsonValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
package org.opensearch.client.opensearch.core;

import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class MsearchRequestTest extends Assert {
public class MsearchRequestTest extends ModelTestCase {

@Test
public void toBuilder() {
Expand All @@ -21,4 +22,17 @@ public void toBuilder() {

assertEquals(copied.index(), origin.index());
}

@Test
public void cancelAfterTimeInterval() {
Time cancelAfterTimeInterval = Time.of(ti -> ti.time("1000ms"));
MsearchRequest request = new MsearchRequest.Builder().index("index")
.searches(Collections.emptyList())
.cancelAfterTimeInterval(cancelAfterTimeInterval)
.build();

assertEquals("[]", toJson(request));
assertEquals(cancelAfterTimeInterval, request.cancelAfterTimeInterval());
assertEquals("1000ms", MsearchRequest._ENDPOINT.queryParameters(request).get("cancel_after_time_interval"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch.core.search.SourceConfig;
import org.opensearch.client.opensearch.core.search.SourceFilter;
import org.opensearch.client.opensearch.model.ModelTestCase;
Expand Down Expand Up @@ -53,6 +54,16 @@ public void pipeline() {
assertEquals("my_pipeline", SearchRequest._ENDPOINT.queryParameters(request).get("search_pipeline"));
}

@Test
public void cancelAfterTimeInterval() {
Time cancelAfterTimeInterval = Time.of(ti -> ti.time("1000ms"));
SearchRequest request = new SearchRequest.Builder().cancelAfterTimeInterval(cancelAfterTimeInterval).build();

assertEquals("{}", toJson(request));
assertEquals(cancelAfterTimeInterval, request.cancelAfterTimeInterval());
assertEquals("1000ms", SearchRequest._ENDPOINT.queryParameters(request).get("cancel_after_time_interval"));
}

@Test
public void toBuilder() {
SearchRequest origin = new SearchRequest.Builder().index("index").build();
Expand Down

0 comments on commit c84ad24

Please sign in to comment.