Skip to content

Commit

Permalink
Adding test to cover storedFields none and replacing deprecated asser…
Browse files Browse the repository at this point in the history
…tThat

Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Oct 31, 2023
1 parent ad40503 commit f416332
Showing 1 changed file with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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();
Expand All @@ -61,10 +56,46 @@ public void shouldReturnSearcheResults() throws Exception {
);

final SearchResponse<ShopItem> 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<ShopItem> 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 {
Expand Down

0 comments on commit f416332

Please sign in to comment.