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 + ); +}