From 0729a92370cd8f81a394c38ccdf94567bfb92687 Mon Sep 17 00:00:00 2001 From: brentam <56659702+brentam@users.noreply.github.com> Date: Wed, 27 Dec 2023 01:44:44 +1100 Subject: [PATCH 1/4] Fixing InnerHits storedFields ("stored_fields") generated Json name. (#781) * dummy CHANGELOG.md Signed-off-by: brentam * corrected the InnerHits storedFields json field name to "stored_fields" Signed-off-by: brentam * adding correct PR information to CHANGELOG.md Signed-off-by: brentam * ran ./gradlew :java-client:spotlessApply Signed-off-by: brentam --------- Signed-off-by: brentam --- CHANGELOG.md | 3 +- .../opensearch/core/search/InnerHits.java | 36 ++++----- .../opensearch/core/search/InnerHitsTest.java | 75 +++++++++++++++++++ 3 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b28c79c7d..673202a241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This section is for maintaining a changelog for all breaking changes for the cli - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) - Fix PutTemplateRequest field deserialization ([#723](https://github.com/opensearch-project/opensearch-java/pull/723)) - Fix PutIndexTemplateRequest field deserialization ([#765](https://github.com/opensearch-project/opensearch-java/pull/765)) +- Fix InnerHits storedFields deserialization/serialization ([#781](https://github.com/opensearch-project/opensearch-java/pull/781) ### Security @@ -254,4 +255,4 @@ This section is for maintaining a changelog for all breaking changes for the cli [2.5.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.4.0...v2.5.0 [2.4.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.3.0...v2.4.0 [2.3.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.2.0...v2.3.0 -[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 \ No newline at end of file +[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/search/InnerHits.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/search/InnerHits.java index a99c9a4315..14ecb45568 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/search/InnerHits.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/search/InnerHits.java @@ -89,7 +89,7 @@ public class InnerHits implements JsonpSerializable { @Nullable private final SourceConfig source; - private final List storedField; + private final List storedFields; @Nullable private final Boolean trackScores; @@ -114,7 +114,7 @@ private InnerHits(Builder builder) { this.fields = ApiTypeHelper.unmodifiable(builder.fields); this.sort = ApiTypeHelper.unmodifiable(builder.sort); this.source = builder.source; - this.storedField = ApiTypeHelper.unmodifiable(builder.storedField); + this.storedFields = ApiTypeHelper.unmodifiable(builder.storedFields); this.trackScores = builder.trackScores; this.version = builder.version; @@ -225,10 +225,10 @@ public final SourceConfig source() { } /** - * API name: {@code stored_field} + * API name: {@code stored_fields} */ - public final List storedField() { - return this.storedField; + public final List storedFields() { + return this.storedFields; } /** @@ -344,10 +344,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { this.source.serialize(generator, mapper); } - if (ApiTypeHelper.isDefined(this.storedField)) { - generator.writeKey("stored_field"); + if (ApiTypeHelper.isDefined(this.storedFields)) { + generator.writeKey("stored_fields"); generator.writeStartArray(); - for (String item0 : this.storedField) { + for (String item0 : this.storedFields) { generator.write(item0); } @@ -414,7 +414,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder storedField; + private List storedFields; @Nullable private Boolean trackScores; @@ -623,22 +623,22 @@ public final Builder source(Function - * Adds all elements of list to storedField. + * Adds all elements of list to storedFields. */ - public final Builder storedField(List list) { - this.storedField = _listAddAll(this.storedField, list); + public final Builder storedFields(List list) { + this.storedFields = _listAddAll(this.storedFields, list); return this; } /** - * API name: {@code stored_field} + * API name: {@code stored_fields} *

- * Adds one or more values to storedField. + * Adds one or more values to storedFields. */ - public final Builder storedField(String value, String... values) { - this.storedField = _listAdd(this.storedField, value, values); + public final Builder storedFields(String value, String... values) { + this.storedFields = _listAdd(this.storedFields, value, values); return this; } @@ -696,7 +696,7 @@ protected static void setupInnerHitsDeserializer(ObjectDeserializer it.storedFields(List.of("field1", "field2"))); + assertTrue(toJson(hits).contains("stored_fields")); + } + + /** + * test if the field "stored_fields" is present after deserialization/serialization + * of InnerHits + */ + @Test + public void testInnerHitFromParsed() { + JsonParser parser = mapper.jsonProvider().createParser(new StringReader(innerHitsJson)); + InnerHits innerHits = InnerHits._DESERIALIZER.deserialize(parser, mapper); + assertThat(innerHits.storedFields(), containsInAnyOrder(storedJobId, storedSalary)); + String actualJson = toJson(innerHits); + assertEquals(innerHitsJson, actualJson); + + } + + /** + * We test if the field "stored_fields" is present in the InnerHits after deserialization/serialization + * of a SearchRequest + */ + @Test + public void testRequestWithInnerHitFromParsed() { + JsonParser parser = mapper.jsonProvider().createParser(new StringReader(searchRequestJson)); + SearchRequest searchRequest = SearchRequest._DESERIALIZER.deserialize(parser, mapper); + InnerHits innerHits = searchRequest.query().bool().must().get(1).nested().innerHits(); + assertThat(innerHits.storedFields(), containsInAnyOrder(storedJobId, storedSalary)); + String actualJson = toJson(searchRequest); + assertEquals(searchRequestJson, actualJson); + } + + private String toJson(JsonpSerializable obj) { + StringWriter stringWriter = new StringWriter(); + try (JsonGenerator generator = mapper.jsonProvider().createGenerator(stringWriter)) { + mapper.serialize(obj, generator); + } + return stringWriter.toString(); + } + + private final String innerHitsJson = String.format("{\"_source\":false,\"stored_fields\":[\"%s\",\"%s\"]}", storedJobId, storedSalary); + private final String searchRequestJson = String.format( + "{\"_source\":false,\"query\":{\"bool\":{\"must\":[{\"match_all\":{}},{\"nested\":{\"inner_hits\":%s,\"path\":\"details\"," + + "\"query\":{\"match_all\":{}}}}]}},\"stored_fields\":[\"title\",\"companyName\"]}", + innerHitsJson + ); +} From 3f9a00cc017d2b42857d8e67c87ed2a98cb86fd9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 26 Dec 2023 09:45:32 -0500 Subject: [PATCH 2/4] Bump com.carrotsearch.randomizedtesting:randomizedtesting-runner from 2.7.1 to 2.8.1 in /java-client (#782) * Bump com.carrotsearch.randomizedtesting:randomizedtesting-runner Bumps com.carrotsearch.randomizedtesting:randomizedtesting-runner from 2.7.1 to 2.8.1. --- updated-dependencies: - dependency-name: com.carrotsearch.randomizedtesting:randomizedtesting-runner dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] * Update changelog Signed-off-by: dependabot[bot] --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] --- CHANGELOG.md | 1 + java-client/build.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 673202a241..46bc5b3730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Bumps `org.apache.httpcomponents.core5:httpcore5-h2` from 5.2.3 to 5.2.4 - Bumps `org.apache.httpcomponents.client5:httpclient5` from 5.2.1 to 5.2.3 - Bumps `org.apache.httpcomponents.core5:httpcore5` from 5.2.3 to 5.2.4 +- Bumps `com.carrotsearch.randomizedtesting:randomizedtesting-runner` from 2.7.1 to 2.8.1 This section is for maintaining a changelog for all breaking changes for the client that cannot be released in the 2.x line. All other non-breaking changes should be added to [Unreleased 2.x] section. diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index 59c574ea62..d0b8b1389f 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -159,7 +159,7 @@ dependencies { // Apache 2.0 compileOnly("org.opensearch.client", "opensearch-rest-client", opensearchVersion) testImplementation("org.hamcrest:hamcrest:2.1") - testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.7.1") { + testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.8.1") { exclude(group = "junit") } testImplementation("org.opensearch.client", "opensearch-rest-client", opensearchVersion) From 373802683b0380985d6f246218d2e1b051c8f394 Mon Sep 17 00:00:00 2001 From: Dante Date: Tue, 26 Dec 2023 23:48:05 +0900 Subject: [PATCH 3/4] Changed SearchAfter field type to FieldValue instead of String (#769) * Changed SearchAfter of SearchRequest type to FieldValue instead of String Signed-off-by: channel-dante * apply spotlessApply Signed-off-by: channel-dante * move log from 2.x to 3.0 on changelog Signed-off-by: channel-dante * add UPGRADING.md Signed-off-by: channel-dante * update UPGRADING.md Signed-off-by: channel-dante --------- Signed-off-by: channel-dante --- CHANGELOG.md | 1 + UPGRADING.md | 20 +++++++++++++++++++ .../client/opensearch/core/SearchRequest.java | 17 ++++++++-------- .../opensearch/core/SearchRequestTest.java | 14 +++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 UPGRADING.md create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 46bc5b3730..8f4622930e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Changed - Migrate client transports to Apache HttpClient / Core 5.x ([#246](https://github.com/opensearch-project/opensearch-java/pull/246)) +- Changed SearchAfter of SearchRequest type to FieldValue instead of String ([#769](https://github.com/opensearch-project/opensearch-java/pull/769)) ### Deprecated - Deprecate RestClientTransport ([#536](https://github.com/opensearch-project/opensearch-java/pull/536)) diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000000..4e437fa770 --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,20 @@ +# UPGRADING + +## [UPGRADING 2.x to 3.0] +### SearchAfter of SearchRequest type +- Changed SearchAfter of SearchRequest type to FieldValue instead of String ([#769](https://github.com/opensearch-project/opensearch-java/pull/769)) +- Consider using `FieldValue.of` to make string type values compatible. + +Before: +``` +.searchAfter("string") +.searchAfter("string1", "string2") +.searchAfter(List.of("String")) +``` + +After: +``` +.searchAfter(FieldValue.of("string")) +.searchAfter(FieldValue.of("string1"), FieldValue.of("string2")) +.searchAfter(List.of(FieldValue.of("String"))) +``` diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/SearchRequest.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/SearchRequest.java index 852415b0a2..f88b8f99ca 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/SearchRequest.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/SearchRequest.java @@ -47,6 +47,7 @@ import org.opensearch.client.json.ObjectDeserializer; 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; @@ -184,7 +185,7 @@ public class SearchRequest extends RequestBase implements JsonpSerializable { @Nullable private final Time scroll; - private final List searchAfter; + private final List searchAfter; @Nullable private final SearchType searchType; @@ -678,7 +679,7 @@ public final Time scroll() { /** * API name: {@code search_after} */ - public final List searchAfter() { + public final List searchAfter() { return this.searchAfter; } @@ -966,8 +967,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(); @@ -1173,7 +1174,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder searchAfter; + private List searchAfter; @Nullable private SearchType searchType; @@ -1860,7 +1861,7 @@ public final Builder scroll(Function> fn) { *

* Adds all elements of list to searchAfter. */ - public final Builder searchAfter(List list) { + public final Builder searchAfter(List list) { this.searchAfter = _listAddAll(this.searchAfter, list); return this; } @@ -1870,7 +1871,7 @@ public final Builder searchAfter(List list) { *

* Adds one or more values to searchAfter. */ - public final Builder searchAfter(String value, String... values) { + public final Builder searchAfter(FieldValue value, FieldValue... values) { this.searchAfter = _listAdd(this.searchAfter, value, values); return this; } @@ -2142,7 +2143,7 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer Date: Tue, 26 Dec 2023 12:13:54 -0500 Subject: [PATCH 4/4] Bump org.hamcrest:hamcrest from 2.1 to 2.2 (#783) * Bump org.hamcrest:hamcrest from 2.1 to 2.2 Bumps [org.hamcrest:hamcrest](https://github.com/hamcrest/JavaHamcrest) from 2.1 to 2.2. - [Release notes](https://github.com/hamcrest/JavaHamcrest/releases) - [Changelog](https://github.com/hamcrest/JavaHamcrest/blob/master/CHANGES.md) - [Commits](https://github.com/hamcrest/JavaHamcrest/compare/v2.1...v2.2) --- updated-dependencies: - dependency-name: org.hamcrest:hamcrest dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] * Update changelog Signed-off-by: dependabot[bot] --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] --- CHANGELOG.md | 3 ++- java-client/build.gradle.kts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f4622930e..f3a10a5a53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Bumps `org.apache.httpcomponents.client5:httpclient5` from 5.2.1 to 5.2.3 - Bumps `org.apache.httpcomponents.core5:httpcore5` from 5.2.3 to 5.2.4 - Bumps `com.carrotsearch.randomizedtesting:randomizedtesting-runner` from 2.7.1 to 2.8.1 +- Bumps `org.hamcrest:hamcrest` from 2.1 to 2.2 This section is for maintaining a changelog for all breaking changes for the client that cannot be released in the 2.x line. All other non-breaking changes should be added to [Unreleased 2.x] section. @@ -257,4 +258,4 @@ This section is for maintaining a changelog for all breaking changes for the cli [2.5.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.4.0...v2.5.0 [2.4.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.3.0...v2.4.0 [2.3.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.2.0...v2.3.0 -[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 +[2.2.0]: https://github.com/opensearch-project/opensearch-java/compare/v2.1.0...v2.2.0 \ No newline at end of file diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index d0b8b1389f..6be21f5950 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -158,7 +158,7 @@ dependencies { // Apache 2.0 compileOnly("org.opensearch.client", "opensearch-rest-client", opensearchVersion) - testImplementation("org.hamcrest:hamcrest:2.1") + testImplementation("org.hamcrest:hamcrest:2.2") testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.8.1") { exclude(group = "junit") }