Skip to content

Commit

Permalink
Added bwc version of searchAfter which accepts FieldValues
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jul 24, 2024
1 parent 57e5a79 commit 6f7b825
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased 2.x]
### Added
- Added `searchAfterVals` to `SearchRequest` to allow passing arbitrary `FieldValue`s to `search_after` ([#1105](https://github.com/opensearch-project/opensearch-java/pull/1105))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.ExpandWildcard;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.ScriptField;
import org.opensearch.client.opensearch._types.SearchType;
Expand Down Expand Up @@ -191,7 +192,7 @@ public class SearchRequest extends RequestBase implements PlainJsonSerializable
@Nullable
private final Time scroll;

private final List<String> searchAfter;
private final List<FieldValue> searchAfter;

@Nullable
private final SearchType searchType;
Expand Down Expand Up @@ -710,8 +711,19 @@ public final Time scroll() {

/**
* API name: {@code search_after}
*
* <p><b>NOTE: In version 3.0.0 of opensearch-java, this method will instead return a {@code List<FieldValue>}.</b></p>
*/
public final List<String> searchAfter() {
return this.searchAfter.stream().map(FieldValue::_toJsonString).collect(Collectors.toList());
}

/**
* API name: {@code search_after}
*
* <p><b>In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter()}.</b></p>
*/
public final List<FieldValue> searchAfterVals() {
return this.searchAfter;
}

Expand Down Expand Up @@ -1006,8 +1018,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
if (ApiTypeHelper.isDefined(this.searchAfter)) {
generator.writeKey("search_after");
generator.writeStartArray();
for (String item0 : this.searchAfter) {
generator.write(item0);
for (FieldValue item0 : this.searchAfter) {
item0.serialize(generator, mapper);

}
generator.writeEnd();
Expand Down Expand Up @@ -1143,7 +1155,7 @@ public Builder toBuilder() {
.runtimeMappings(runtimeMappings)
.scriptFields(scriptFields)
.scroll(scroll)
.searchAfter(searchAfter)
.searchAfterVals(searchAfter)
.searchType(searchType)
.seqNoPrimaryTerm(seqNoPrimaryTerm)
.size(size)
Expand Down Expand Up @@ -1288,7 +1300,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<S
private Time scroll;

@Nullable
private List<String> searchAfter;
private List<FieldValue> searchAfter;

@Nullable
private SearchType searchType;
Expand Down Expand Up @@ -1998,18 +2010,46 @@ public final Builder scroll(Function<Time.Builder, ObjectBuilder<Time>> fn) {
* API name: {@code search_after}
* <p>
* Adds all elements of <code>list</code> to <code>searchAfter</code>.
*
* <p><b>In version 3.0.0 of opensearch-java, this method will instead accept a {@code List<FieldValue>}.</b></p>
*/
public final Builder searchAfter(List<String> list) {
this.searchAfter = _listAddAll(this.searchAfter, list);
this.searchAfter = _listAddAll(this.searchAfter, FieldValue::of, list);
return this;
}

/**
* API name: {@code search_after}
* <p>
* Adds one or more values to <code>searchAfter</code>.
*
* <p><b>In version 3.0.0 of opensearch-java, this method will instead accept values of type {@code FieldValue}.</b></p>
*/
public final Builder searchAfter(String value, String... values) {
this.searchAfter = _listAdd(this.searchAfter, FieldValue::of, value, values);
return this;
}

/**
* API name: {@code search_after}
* <p>
* Adds all elements of <code>list</code> to <code>searchAfter</code>.
*
* <p><b>In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter(List)}.</b></p>
*/
public final Builder searchAfterVals(List<FieldValue> list) {
this.searchAfter = _listAddAll(this.searchAfter, list);
return this;
}

/**
* API name: {@code search_after}
* <p>
* Adds one or more values to <code>searchAfter</code>.
*
* <p><b>In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter(String, String...)}.</b></p>
*/
public final Builder searchAfterVals(FieldValue value, FieldValue... values) {
this.searchAfter = _listAdd(this.searchAfter, value, values);
return this;
}
Expand Down Expand Up @@ -2301,7 +2341,7 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer<SearchRe
op.add(Builder::rescore, JsonpDeserializer.arrayDeserializer(Rescore._DESERIALIZER), "rescore");
op.add(Builder::runtimeMappings, JsonpDeserializer.stringMapDeserializer(RuntimeField._DESERIALIZER), "runtime_mappings");
op.add(Builder::scriptFields, JsonpDeserializer.stringMapDeserializer(ScriptField._DESERIALIZER), "script_fields");
op.add(Builder::searchAfter, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "search_after");
op.add(Builder::searchAfterVals, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "search_after");
op.add(Builder::seqNoPrimaryTerm, JsonpDeserializer.booleanDeserializer(), "seq_no_primary_term");
op.add(Builder::size, JsonpDeserializer.integerDeserializer(), "size");
op.add(Builder::slice, SlicedScroll._DESERIALIZER, "slice");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ObjectBuilderBase {
private boolean _used = false;
Expand Down Expand Up @@ -73,17 +75,34 @@ private static <T> List<T> _mutableList(List<T> list) {
}
}

/** Add a value to a (possibly {@code null}) list */
@SafeVarargs
protected static <TOut, TIn> List<TOut> _listAdd(List<TOut> list, Function<TIn, TOut> mapper, TIn value, TIn... values) {
List<TOut> mappedValues = values.length > 0 ? Arrays.stream(values).map(mapper).collect(Collectors.toList()) : null;
return _listAdd(list, mapper.apply(value), mappedValues);
}

/** Add a value to a (possibly {@code null}) list */
@SafeVarargs
protected static <T> List<T> _listAdd(List<T> list, T value, T... values) {
return _listAdd(list, value, values.length > 0 ? Arrays.asList(values) : null);
}

private static <T> List<T> _listAdd(List<T> list, T value, List<T> values) {
list = _mutableList(list);
list.add(value);
if (values.length > 0) {
list.addAll(Arrays.asList(values));
if (values != null) {
list.addAll(values);
}
return list;
}

/** Add all elements of a list to a (possibly {@code null}) list */
protected static <TOut, TIn> List<TOut> _listAddAll(List<TOut> list, Function<TIn, TOut> mapper, List<TIn> values) {
List<TOut> mappedValues = values != null ? values.stream().map(mapper).collect(Collectors.toList()) : null;
return _listAddAll(list, mappedValues);
}

/** Add all elements of a list to a (possibly {@code null}) list */
protected static <T> List<T> _listAddAll(List<T> list, List<T> values) {
if (list == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SearchRequestTest extends ModelTestCase {

@Test
public void afterSearch() {
SearchRequest request = new SearchRequest.Builder().searchAfter("string1", "string2").build();

assertEquals("{\"search_after\":[\"string1\",\"string2\"]}", toJson(request));

request = new SearchRequest.Builder().searchAfterVals(FieldValue.of(1), FieldValue.of("string")).build();

assertEquals("{\"search_after\":[1,\"string\"]}", toJson(request));
}

@Test
public void ext() {
SearchRequest request = new SearchRequest.Builder().ext(
Expand Down

0 comments on commit 6f7b825

Please sign in to comment.