forked from opensearch-project/opensearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing InnerHits storedFields ("stored_fields") generated Json name. (o…
…pensearch-project#781) * dummy CHANGELOG.md Signed-off-by: brentam <[email protected]> * corrected the InnerHits storedFields json field name to "stored_fields" Signed-off-by: brentam <[email protected]> * adding correct PR information to CHANGELOG.md Signed-off-by: brentam <[email protected]> * ran ./gradlew :java-client:spotlessApply Signed-off-by: brentam <[email protected]> --------- Signed-off-by: brentam <[email protected]> (cherry picked from commit 0729a92)
- Loading branch information
Showing
4 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
java-client/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.opensearch.client.opensearch.core.search; | ||
|
||
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; | ||
|
||
public class InnerHitsTest { | ||
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" | ||
*/ | ||
@Test | ||
public void testInnerHitStoredFields() { | ||
InnerHits hits = InnerHits.of((it) -> 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 | ||
); | ||
} |