Skip to content

Commit

Permalink
Fix handling on non-string sort values on Hits
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Oct 9, 2024
1 parent 7592881 commit 9e51d6f
Showing 1 changed file with 47 additions and 6 deletions.
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 9e51d6f

Please sign in to comment.