Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bwc handling on non-string sort values on Hits #1224

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ 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))

### 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 @@ -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() {
Copy link
Collaborator

@reta reta Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public final List<FieldValue> sortVals() {
public final List<FieldValue> sortFieldValues() {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 3.x (as you mentioned) we could replace sort() directly

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
Loading