From 8cf558ab8c566ced7ffcab26d2ed3e29b31ff17b Mon Sep 17 00:00:00 2001 From: brentam Date: Sat, 23 Dec 2023 11:59:44 +1100 Subject: [PATCH 1/4] dummy CHANGELOG.md Signed-off-by: brentam --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b28c79c7d..834ce3df68 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 ([ooo](https://github.com/opensearch-project/opensearch-java/pull/ooo)) ### 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 From dcc33420c6e5ab6294d84ba2e80f9ee67493808d Mon Sep 17 00:00:00 2001 From: brentam Date: Sat, 23 Dec 2023 12:01:32 +1100 Subject: [PATCH 2/4] corrected the InnerHits storedFields json field name to "stored_fields" Signed-off-by: brentam --- .../opensearch/core/search/InnerHits.java | 36 ++++----- .../opensearch/core/search/InnerHitsTest.java | 80 +++++++++++++++++++ 2 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java 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 String innerHitsJson = String.format( + "{\"_source\":false,\"stored_fields\":[\"%s\",\"%s\"]}", + storedJobId, + storedSalary + ); + private 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 98332a148ed3d9b387bac37df41d2a1d55aa1ee9 Mon Sep 17 00:00:00 2001 From: brentam Date: Sat, 23 Dec 2023 12:08:00 +1100 Subject: [PATCH 3/4] adding correct PR information to CHANGELOG.md Signed-off-by: brentam --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 834ce3df68..673202a241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +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 ([ooo](https://github.com/opensearch-project/opensearch-java/pull/ooo)) +- Fix InnerHits storedFields deserialization/serialization ([#781](https://github.com/opensearch-project/opensearch-java/pull/781) ### Security From 633741b926e590ab2ada272ba00b9bcfa8987497 Mon Sep 17 00:00:00 2001 From: brentam Date: Sat, 23 Dec 2023 23:59:31 +1100 Subject: [PATCH 4/4] ran ./gradlew :java-client:spotlessApply Signed-off-by: brentam --- .../opensearch/core/search/InnerHitsTest.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java index 0bcc6e7bbc..1269ebb0df 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java @@ -1,27 +1,25 @@ package org.opensearch.client.opensearch.core.search; -import jakarta.json.spi.JsonProvider; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.List; import org.junit.Test; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.opensearch.core.SearchRequest; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.List; - -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class InnerHitsTest { - private JsonpMapper mapper = new JsonbJsonpMapper(); - private String storedSalary = "details.salary"; - private String storedJobId = "details.jobId"; + private final JsonpMapper mapper = new JsonbJsonpMapper(); + private final String storedSalary = "details.salary"; + private final String storedJobId = "details.jobId"; /** * test if the json field for storedFields is generated with the correct name "stored_fields" @@ -44,7 +42,6 @@ public void testInnerHitFromParsed() { String actualJson = toJson(innerHits); assertEquals(innerHitsJson, actualJson); - } /** @@ -68,13 +65,11 @@ private String toJson(JsonpSerializable obj) { } return stringWriter.toString(); } - private String innerHitsJson = String.format( - "{\"_source\":false,\"stored_fields\":[\"%s\",\"%s\"]}", - storedJobId, - storedSalary + + 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 ); - private 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); }