From f416332dbea789b1d47a092065f260688e8ba546 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Tue, 31 Oct 2023 06:46:00 +0000 Subject: [PATCH] Adding test to cover storedFields none and replacing deprecated assertThat Signed-off-by: Vacha Shah --- .../integTest/AbstractSearchRequestIT.java | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java index 96ee498894..6800d6af0d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java @@ -8,12 +8,10 @@ package org.opensearch.client.opensearch.integTest; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.arrayContaining; -import static org.hamcrest.Matchers.hasSize; - import java.io.IOException; +import java.util.Arrays; +import java.util.stream.Collectors; + import org.junit.Test; import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.SortOrder; @@ -27,10 +25,9 @@ public abstract class AbstractSearchRequestIT extends OpenSearchJavaClientTestCase { @Test - public void shouldReturnSearcheResults() throws Exception { - final String index = "searches_request"; - assertThat( - javaClient().indices() + public void shouldReturnSearchResults() throws Exception { + final String index = "search_request"; + assertTrue(javaClient().indices() .create( b -> b.index(index) .mappings( @@ -39,9 +36,7 @@ public void shouldReturnSearcheResults() throws Exception { ) .settings(settings -> settings.sort(s -> s.field("name").order(SegmentSortOrder.Asc))) ) - .acknowledged(), - equalTo(true) - ); + .acknowledged()); createTestDocuments(index); javaClient().indices().refresh(); @@ -61,10 +56,46 @@ public void shouldReturnSearcheResults() throws Exception { ); final SearchResponse response = javaClient().search(request, ShopItem.class); - assertThat(response.hits().hits(), hasSize(2)); + assertEquals(response.hits().hits().size(), 2); + + assertTrue(Arrays.stream(response.hits().hits().get(0).fields().get("name").to(String[].class)).collect(Collectors.toList()).contains("hummer")); + assertTrue(Arrays.stream(response.hits().hits().get(1).fields().get("name").to(String[].class)).collect(Collectors.toList()).contains("jammer")); + } + + @Test + public void shouldReturnSearchResultsWithoutStoredFields() throws Exception { + final String index = "search_request"; + assertTrue(javaClient().indices() + .create( + b -> b.index(index) + .mappings( + m -> m.properties("name", Property.of(p -> p.keyword(v -> v.docValues(true)))) + .properties("size", Property.of(p -> p.keyword(v -> v.docValues(true)))) + ) + .settings(settings -> settings.sort(s -> s.field("name").order(SegmentSortOrder.Asc))) + ) + .acknowledged()); - assertThat(response.hits().hits().get(0).fields().get("name").to(String[].class), arrayContaining("hummer")); - assertThat(response.hits().hits().get(1).fields().get("name").to(String[].class), arrayContaining("jammer")); + createTestDocuments(index); + javaClient().indices().refresh(); + + final Query query = Query.of( + q -> q.bool( + builder -> builder.filter(filter -> filter.term(TermQuery.of(term -> term.field("size").value(FieldValue.of("huge"))))) + ) + ); + + final SearchRequest request = SearchRequest.of( + r -> r.index(index) + .sort(s -> s.field(f -> f.field("name").order(SortOrder.Asc))) + .query(query) + .storedFields("_none_") + ); + + final SearchResponse response = javaClient().search(request, ShopItem.class); + assertEquals(response.hits().hits().size(), 2); + assertNull(response.hits().hits().get(0).id()); + assertNull(response.hits().hits().get(1).id()); } private void createTestDocuments(String index) throws IOException {