-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into analyzers-for-extra…
…-languages
- Loading branch information
Showing
7 changed files
with
142 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))) | ||
``` |
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
14 changes: 14 additions & 0 deletions
14
java-client/src/test/java/org/opensearch/client/opensearch/core/SearchRequestTest.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,14 @@ | ||
package org.opensearch.client.opensearch.core; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.client.opensearch._types.FieldValue; | ||
import org.opensearch.client.opensearch.model.ModelTestCase; | ||
|
||
public class SearchRequestTest extends ModelTestCase { | ||
@Test | ||
public void afterSearch() { | ||
SearchRequest request = new SearchRequest.Builder().searchAfter(FieldValue.of(1), FieldValue.of("string")).build(); | ||
|
||
assertEquals("{\"search_after\":[1,\"string\"]}", toJson(request)); | ||
} | ||
} |
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 | ||
); | ||
} |