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) (opensearch-project#1151)

* Fixes opensearch-project#1116



* Adds MsearchRequest. Fixes SearchRequest.



* Update CHANGELOG.md



* spotlessJavaCheck



* Adds Builder Function method in MSearch



---------



(cherry picked from commit c84ad24)

Signed-off-by: uri.nudelman <uriel.nudelman@offerup.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: uri.nudelman <uriel.nudelman@offerup.com>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent 8e1621b commit b0fdf50
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 @@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased 2.x]
### 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 @@ -1125,6 +1140,7 @@ public Builder toBuilder() {
.ccsMinimizeRoundtrips(ccsMinimizeRoundtrips)
.phaseTook(phaseTook)
.pipeline(pipeline)
.cancelAfterTimeInterval(cancelAfterTimeInterval)
.collapse(collapse)
.defaultOperator(defaultOperator)
.df(df)
Expand Down Expand Up @@ -1209,6 +1225,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 @@ -1483,6 +1502,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 @@ -2444,6 +2484,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 @@ -58,6 +59,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 b0fdf50

Please sign in to comment.