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.
Removed required index for Hit.java (opensearch-project#833)
For InnerHits, since the index and id is assumed to be the same as parent, they were not included in the InnerHit object, but when converting it into a Hit object in the java sdk, it would throw an exception, saying index is required. Removed it, and also added unit tests. Signed-off-by: jvan1997 <32347479+jvan1997@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
98 additions
and
7 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
86 changes: 86 additions & 0 deletions
86
...lient/src/test/java/org/opensearch/client/opensearch/core/search/InnerHitsResultTest.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,86 @@ | ||
package org.opensearch.client.opensearch.core.search; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.io.StringReader; | ||
import org.junit.Test; | ||
import org.opensearch.client.json.JsonData; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.json.jsonb.JsonbJsonpMapper; | ||
|
||
public class InnerHitsResultTest { | ||
private final JsonpMapper mapper = new JsonbJsonpMapper(); | ||
private final String storedSalary = "details.salary"; | ||
private final String storedJobId = "details.jobId"; | ||
|
||
/** | ||
* test if the InnerHitsResult will build the Hit<JsonData> | ||
*/ | ||
@Test | ||
public void testInnerHits() { | ||
|
||
String classString = String.valueOf(hitResultWithIdIndex.innerHits().get("test_child").getClass()); | ||
assertEquals(classString, InnerHitsResult.class.toString()); | ||
// take hitResult and get the InnerHit | ||
InnerHitsResult innerHitsResult = hitResultWithIdIndex.innerHits().get("test_child"); | ||
Hit<JsonData> innerHitResult = innerHitsResult.hits().hits().get(0); | ||
assertNotNull(innerHitResult.index()); | ||
assertEquals(innerHitResult.index(), "_index"); | ||
assertNotNull(innerHitResult.id()); | ||
assertEquals(innerHitResult.id(), "child_id"); | ||
} | ||
|
||
/** | ||
* test if the InnerHitsResult will still build the Hit<JsonData> even if id and index is not specified | ||
*/ | ||
@Test | ||
public void testInnerHitWithoutIdIndex() { | ||
|
||
String classString = String.valueOf(hitResultNoIdIndex.innerHits().get("test_child").getClass()); | ||
assertEquals(classString, InnerHitsResult.class.toString()); | ||
// take hitResult and get the InnerHit | ||
InnerHitsResult innerHitsResult = hitResultNoIdIndex.innerHits().get("test_child"); | ||
Hit<JsonData> innerHitResult = innerHitsResult.hits().hits().get(0); | ||
// Id and index are now nullable. | ||
assertNull(innerHitResult.index()); | ||
assertNull(innerHitResult.id()); | ||
} | ||
|
||
private final String innerHitJsonWithNoIdOrIndex = "{\"key\":\"value\"}"; | ||
private final String innerHitJsonWithIdOrIndex = "{\"id\":\"value\",\"index\":\"value\"}"; | ||
|
||
private final Hit<JsonData> hitResultNoIdIndex = Hit.of( | ||
it -> it.id("test_parent") | ||
.index("_index") | ||
.innerHits( | ||
"test_child", | ||
innerHitsResultBuilder -> innerHitsResultBuilder.hits( | ||
innerHitsMetadataBuilder -> innerHitsMetadataBuilder.total(total -> total.value(1).relation(TotalHitsRelation.Eq)) | ||
.hits( | ||
innerHitsListMemberBuilder -> innerHitsListMemberBuilder.source( | ||
JsonData.from(mapper.jsonProvider().createParser(new StringReader(innerHitJsonWithNoIdOrIndex)), mapper) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
private final Hit<JsonData> hitResultWithIdIndex = Hit.of( | ||
it -> it.id("test_parent") | ||
.index("_index") | ||
.innerHits( | ||
"test_child", | ||
innerHitsResultBuilder -> innerHitsResultBuilder.hits( | ||
innerHitsMetadataBuilder -> innerHitsMetadataBuilder.total(total -> total.value(1).relation(TotalHitsRelation.Eq)) | ||
.hits( | ||
innerHitsListMemberBuilder -> innerHitsListMemberBuilder.id("child_id") | ||
.index("_index") | ||
.source( | ||
JsonData.from(mapper.jsonProvider().createParser(new StringReader(innerHitJsonWithIdOrIndex)), mapper) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
} |