Skip to content

Commit

Permalink
Add bwc handling on non-string sort values on Hits (#1224)
Browse files Browse the repository at this point in the history
* Fix handling on non-string sort values on `Hit`s

Signed-off-by: Thomas Farr <[email protected]>

* Update CHANGELOG

Signed-off-by: Thomas Farr <[email protected]>

* Fix MultisearchBody as well

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia authored Oct 9, 2024
1 parent 7592881 commit 74c3721
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased 2.x]
### Added
- Add `hashCode` and `equals` implementations ([#312](https://github.com/opensearch-project/opensearch-java/pull/312)).
- Added `hashCode` and `equals` implementations ([#312](https://github.com/opensearch-project/opensearch-java/pull/312))
- Added `sortVals` to `Hit` to allow retrieving non-string sort values ([#1224](https://github.com/opensearch-project/opensearch-java/pull/1224))
- Added `searchAfterVals` to `MultisearchBody` to allow passing arbitrary `FieldValue`s to `search_after` ([#1224](https://github.com/opensearch-project/opensearch-java/pull/1224))

### Dependencies
- Bumps `org.junit:junit-bom` from 5.10.3 to 5.11.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
Expand All @@ -44,6 +45,7 @@
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.ScriptField;
import org.opensearch.client.opensearch._types.SortOptions;
import org.opensearch.client.opensearch._types.aggregations.Aggregation;
Expand Down Expand Up @@ -77,7 +79,7 @@ public class MultisearchBody implements PlainJsonSerializable {
@Nullable
private final Query postFilter;

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

@Nullable
private final Integer size;
Expand Down Expand Up @@ -201,8 +203,19 @@ public final Query postFilter() {

/**
* 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>NOTE: 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 @@ -407,8 +420,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 @@ -578,7 +591,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
private Query postFilter;

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

@Nullable
private Integer size;
Expand Down Expand Up @@ -715,18 +728,46 @@ public final Builder postFilter(Function<Query.Builder, ObjectBuilder<Query>> fn
* API name: {@code search_after}
* <p>
* Adds all elements of <code>list</code> to <code>searchAfter</code>.
*
* <p><b>NOTE: 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>NOTE: 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>NOTE: 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>NOTE: 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 @@ -1092,7 +1133,7 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
op.add(Builder::from, JsonpDeserializer.integerDeserializer(), "from");
op.add(Builder::minScore, JsonpDeserializer.doubleDeserializer(), "min_score");
op.add(Builder::postFilter, Query._DESERIALIZER, "post_filter");
op.add(Builder::searchAfter, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "search_after");
op.add(Builder::searchAfterVals, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "search_after");
op.add(Builder::size, JsonpDeserializer.integerDeserializer(), "size");
op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(SortOptions._DESERIALIZER), "sort");
op.add(Builder::trackScores, JsonpDeserializer.booleanDeserializer(), "track_scores");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializer;
Expand All @@ -46,6 +47,7 @@
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.core.explain.Explanation;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
Expand Down Expand Up @@ -98,7 +100,7 @@ public class Hit<TDocument> implements PlainJsonSerializable {
@Nullable
private final Long version;

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

@Nullable
private final JsonpSerializer<TDocument> tDocumentSerializer;
Expand Down Expand Up @@ -265,8 +267,19 @@ public final Long version() {

/**
* API name: {@code sort}
*
* <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> sort() {
return this.sort.stream().map(FieldValue::_toJsonString).collect(Collectors.toList());
}

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

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

}
generator.writeEnd();
Expand Down Expand Up @@ -474,7 +487,7 @@ public static class Builder<TDocument> extends ObjectBuilderBase implements Obje
private Long version;

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

@Nullable
private JsonpSerializer<TDocument> tDocumentSerializer;
Expand Down Expand Up @@ -702,18 +715,46 @@ public final Builder<TDocument> version(@Nullable Long value) {
* API name: {@code sort}
* <p>
* Adds all elements of <code>list</code> to <code>sort</code>.
*
* <p><b>NOTE: In version 3.0.0 of opensearch-java, this method will instead accept a {@code List<FieldValue>}.</b></p>
*/
public final Builder<TDocument> sort(List<String> list) {
this.sort = _listAddAll(this.sort, list);
this.sort = _listAddAll(this.sort, FieldValue::of, list);
return this;
}

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

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

/**
* API name: {@code sort}
* <p>
* Adds one or more values to <code>sort</code>.
*
* <p><b>NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #sort(String, String...)}.</b></p>
*/
public final Builder<TDocument> sortVals(FieldValue value, FieldValue... values) {
this.sort = _listAdd(this.sort, value, values);
return this;
}
Expand Down Expand Up @@ -778,7 +819,7 @@ protected static <TDocument> void setupHitDeserializer(
op.add(Builder::seqNo, JsonpDeserializer.longDeserializer(), "_seq_no");
op.add(Builder::primaryTerm, JsonpDeserializer.longDeserializer(), "_primary_term");
op.add(Builder::version, JsonpDeserializer.longDeserializer(), "_version");
op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringOrNullDeserializer()), "sort");
op.add(Builder::sortVals, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "sort");

}

Expand Down

0 comments on commit 74c3721

Please sign in to comment.