Skip to content

Commit

Permalink
Search Queries in parallel - part 3 (elastic#117149)
Browse files Browse the repository at this point in the history
Update IT tests grouping assertResponses
  • Loading branch information
drempapis authored Dec 2, 2024
1 parent 9dcd975 commit d2a4c70
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponses;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyArray;
Expand Down Expand Up @@ -262,27 +263,16 @@ public void testSearchingFilteringAliasesSingleIndex() throws Exception {
.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
).actionGet();

logger.info("--> checking single filtering alias search");
assertResponse(
assertResponses(
searchResponse -> assertHits(searchResponse.getHits(), "1"),
prepareSearch("foos").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1")
);

logger.info("--> checking single filtering alias wildcard search");
assertResponse(
prepareSearch("fo*").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1")
prepareSearch("fo*").setQuery(QueryBuilders.matchAllQuery())
);

assertResponse(
assertResponses(
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3"),
prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3")
);

logger.info("--> checking single filtering alias search with sort");
assertResponse(
prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).addSort("_index", SortOrder.ASC),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3")
prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).addSort("_index", SortOrder.ASC)
);

logger.info("--> checking single filtering alias search with global facets");
Expand Down Expand Up @@ -323,28 +313,12 @@ public void testSearchingFilteringAliasesSingleIndex() throws Exception {
searchResponse -> assertHits(searchResponse.getHits(), "1", "2")
);

logger.info("--> checking single non-filtering alias search");
assertResponse(
assertResponses(
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3", "4"),
prepareSearch("alias1").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3", "4")
);

logger.info("--> checking non-filtering alias and filtering alias search");
assertResponse(
prepareSearch("alias1", "foos").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3", "4")
);

logger.info("--> checking index and filtering alias search");
assertResponse(
prepareSearch("test", "foos").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3", "4")
);

logger.info("--> checking index and alias wildcard search");
assertResponse(
prepareSearch("te*", "fo*").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3", "4")
prepareSearch("te*", "fo*").setQuery(QueryBuilders.matchAllQuery())
);
}

Expand Down Expand Up @@ -506,11 +480,11 @@ public void testSearchingFilteringAliasesMultipleIndices() throws Exception {
prepareSearch("filter23", "filter13").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "21", "31", "13", "33")
);
assertResponse(
assertResponses(
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(4L)),
prepareSearch("filter23", "filter13").setSize(0).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(4L))
prepareSearch("filter13", "filter1").setSize(0).setQuery(QueryBuilders.matchAllQuery())
);

assertResponse(
prepareSearch("filter23", "filter1").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "21", "31", "11", "12", "13")
Expand All @@ -519,16 +493,10 @@ public void testSearchingFilteringAliasesMultipleIndices() throws Exception {
prepareSearch("filter23", "filter1").setSize(0).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(5L))
);

assertResponse(
prepareSearch("filter13", "filter1").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "11", "12", "13", "33")
);
assertResponse(
prepareSearch("filter13", "filter1").setSize(0).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(4L))
);

assertResponse(
prepareSearch("filter13", "filter1", "filter23").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "11", "12", "13", "21", "31", "33")
Expand All @@ -537,7 +505,6 @@ public void testSearchingFilteringAliasesMultipleIndices() throws Exception {
prepareSearch("filter13", "filter1", "filter23").setSize(0).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(6L))
);

assertResponse(
prepareSearch("filter23", "filter13", "test2").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "21", "22", "23", "31", "13", "33")
Expand All @@ -546,7 +513,6 @@ public void testSearchingFilteringAliasesMultipleIndices() throws Exception {
prepareSearch("filter23", "filter13", "test2").setSize(0).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(6L))
);

assertResponse(
prepareSearch("filter23", "filter13", "test1", "test2").setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "11", "12", "13", "21", "22", "23", "31", "33")
Expand Down Expand Up @@ -1325,17 +1291,13 @@ public void testIndexingAndQueryingHiddenAliases() throws Exception {
searchResponse -> assertHits(searchResponse.getHits(), "2", "3")
);

// Ensure that all docs can be gotten through the alias
assertResponse(
assertResponses(
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3"),
// Ensure that all docs can be gotten through the alias
prepareSearch(alias).setQuery(QueryBuilders.matchAllQuery()),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3")
);

// And querying using a wildcard with indices options set to expand hidden
assertResponse(
// And querying using a wildcard with indices options set to expand hidden
prepareSearch("alias*").setQuery(QueryBuilders.matchAllQuery())
.setIndicesOptions(IndicesOptions.fromOptions(false, false, true, false, true, true, true, false, false)),
searchResponse -> assertHits(searchResponse.getHits(), "1", "2", "3")
.setIndicesOptions(IndicesOptions.fromOptions(false, false, true, false, true, true, true, false, false))
);

// And that querying the alias with a wildcard and no expand options fails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.elasticsearch.index.query.QueryBuilders.wrapperQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponses;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasKey;
Expand Down Expand Up @@ -105,54 +106,32 @@ public void testSimpleMatchedQueryFromTopLevelFilter() throws Exception {
prepareIndex("test").setId("3").setSource("name", "test").get();
refresh();

assertResponse(
assertResponses(response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else if (hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(1));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
},
prepareSearch().setQuery(matchAllQuery())
.setPostFilter(
boolQuery().should(termQuery("name", "test").queryName("name")).should(termQuery("title", "title1").queryName("title"))
),
response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else if (hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(1));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
);

assertResponse(
prepareSearch().setQuery(matchAllQuery())
.setPostFilter(
boolQuery().should(termQuery("name", "test").queryName("name")).should(termQuery("title", "title1").queryName("title"))
),
response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else if (hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(1));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
)
);
}

Expand All @@ -165,43 +144,25 @@ public void testSimpleMatchedQueryFromTopLevelFilterAndFilteredQuery() throws Ex
prepareIndex("test").setId("3").setSource("name", "test", "title", "title3").get();
refresh();

assertResponse(
assertResponses(response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1") || hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
},
prepareSearch().setQuery(
boolQuery().must(matchAllQuery()).filter(termsQuery("title", "title1", "title2", "title3").queryName("title"))
).setPostFilter(termQuery("name", "test").queryName("name")),
response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1") || hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
);

assertResponse(
prepareSearch().setQuery(termsQuery("title", "title1", "title2", "title3").queryName("title"))
.setPostFilter(matchQuery("name", "test").queryName("name")),
response -> {
assertHitCount(response, 3L);
for (SearchHit hit : response.getHits()) {
if (hit.getId().equals("1") || hit.getId().equals("2") || hit.getId().equals("3")) {
assertThat(hit.getMatchedQueriesAndScores().size(), equalTo(2));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("name"));
assertThat(hit.getMatchedQueryScore("name"), greaterThan(0f));
assertThat(hit.getMatchedQueriesAndScores(), hasKey("title"));
assertThat(hit.getMatchedQueryScore("title"), greaterThan(0f));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
.setPostFilter(matchQuery("name", "test").queryName("name"))
);
}

Expand Down
Loading

0 comments on commit d2a4c70

Please sign in to comment.