Skip to content

Commit

Permalink
Remove more explicit references to SearchResponse in tests (#101052)
Browse files Browse the repository at this point in the history
Follow up to #100966 introducing new combined assertion `assertSearchHitsWithoutFailures`
to combine no-failure, count, and id assertions into one block.
  • Loading branch information
original-brownbear committed Oct 18, 2023
1 parent 3b8825c commit 03ea4bb
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 448 deletions.
Expand Up @@ -54,7 +54,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -612,18 +612,17 @@ public void testInnerHitsWithIgnoreUnmapped() {
createIndexRequest("index1", "child_type", "2", "1").get();
client().prepareIndex("index2").setId("3").setSource("key", "value").get();
refresh();

SearchResponse response = client().prepareSearch("index1", "index2")
.setQuery(
boolQuery().should(
hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder().setIgnoreUnmapped(true))
).should(termQuery("key", "value"))
)
.get();
assertNoFailures(response);
assertHitCount(response, 2);
assertSearchHits(response, "1", "3");
assertSearchHitsWithoutFailures(
client().prepareSearch("index1", "index2")
.setQuery(
boolQuery().should(
hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder().setIgnoreUnmapped(true))
).should(termQuery("key", "value"))
),
"1",
"3"
);
}

public void testTooHighResultWindow() {
Expand Down
Expand Up @@ -9,7 +9,6 @@

import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -47,7 +46,7 @@
import static org.elasticsearch.index.query.QueryBuilders.scriptQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -87,17 +86,17 @@ public void testPercolateScriptQuery() throws IOException {
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.execute()
.actionGet();
SearchResponse response = client().prepareSearch("index")
.setQuery(
new PercolateQueryBuilder(
"query",
BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()),
XContentType.JSON
)
)
.get();
assertHitCount(response, 1);
assertSearchHits(response, "1");
assertSearchHitsWithoutFailures(
client().prepareSearch("index")
.setQuery(
new PercolateQueryBuilder(
"query",
BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()),
XContentType.JSON
)
),
"1"
);
}

public void testPercolateQueryWithNestedDocuments_doNotLeakBitsetCacheEntries() throws Exception {
Expand Down Expand Up @@ -265,17 +264,17 @@ public void testMapUnmappedFieldAsText() throws IOException {
.get();
indicesAdmin().prepareRefresh().get();

SearchResponse response = client().prepareSearch("test")
.setQuery(
new PercolateQueryBuilder(
"query",
BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()),
XContentType.JSON
)
)
.get();
assertHitCount(response, 1);
assertSearchHits(response, "1");
assertSearchHitsWithoutFailures(
client().prepareSearch("test")
.setQuery(
new PercolateQueryBuilder(
"query",
BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()),
XContentType.JSON
)
),
"1"
);
}

public void testRangeQueriesWithNow() throws Exception {
Expand Down
Expand Up @@ -75,7 +75,7 @@ public void testDelete() throws InterruptedException, ExecutionException {
DeleteResponse delete = client().prepareDelete("test", "1").setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).get();
assertEquals(DocWriteResponse.Result.DELETED, delete.getResult());
assertFalse("request shouldn't have forced a refresh", delete.forcedRefresh());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")).get());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")));
}

public void testUpdate() throws InterruptedException, ExecutionException {
Expand Down Expand Up @@ -109,7 +109,7 @@ public void testUpdate() throws InterruptedException, ExecutionException {
.get();
assertEquals(2, update.getVersion());
assertFalse("request shouldn't have forced a refresh", update.forcedRefresh());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "cat")).get());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "cat")));
}

public void testBulk() {
Expand All @@ -129,7 +129,7 @@ public void testBulk() {
bulk = client().prepareBulk().setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
bulk.add(client().prepareDelete("test", "1"));
assertBulkSuccess(bulk.get());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")).get());
assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")));

// Update makes a noop
bulk = client().prepareBulk().setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
Expand Down
Expand Up @@ -88,7 +88,7 @@ private void runTestAutomaticRefresh(final IntToLongFunction count) throws Inter
assertFalse(indexService.getIndexSettings().isExplicitRefresh());
ensureGreen();
AtomicInteger totalNumDocs = new AtomicInteger(Integer.MAX_VALUE);
assertNoSearchHits(client().prepareSearch().get());
assertNoSearchHits(client().prepareSearch());
int numDocs = scaledRandomIntBetween(25, 100);
totalNumDocs.set(numDocs);
CountDownLatch indexingDone = new CountDownLatch(numDocs);
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -79,7 +80,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.in;
Expand Down Expand Up @@ -522,9 +523,7 @@ public void testIndexSearchAndRelocateConcurrently() throws Exception {
final int numIters = randomIntBetween(10, 20);
for (int i = 0; i < numIters; i++) {
logger.info(" --> checking iteration {}", i);
SearchResponse afterRelocation = client().prepareSearch().setSize(ids.size()).get();
assertNoFailures(afterRelocation);
assertSearchHits(afterRelocation, ids.toArray(new String[ids.size()]));
assertSearchHitsWithoutFailures(client().prepareSearch().setSize(ids.size()), ids.toArray(Strings.EMPTY_ARRAY));
}
stopped.set(true);
for (Thread searchThread : searchThreads) {
Expand Down
Expand Up @@ -52,7 +52,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -922,17 +922,17 @@ public void testInnerHitsWithIgnoreUnmapped() throws Exception {
client().prepareIndex("index2").setId("3").setSource("key", "value").get();
refresh();

SearchResponse response = client().prepareSearch("index1", "index2")
.setQuery(
boolQuery().should(
nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder().setIgnoreUnmapped(true))
).should(termQuery("key", "value"))
)
.get();
assertNoFailures(response);
assertHitCount(response, 2);
assertSearchHits(response, "1", "3");
assertSearchHitsWithoutFailures(
client().prepareSearch("index1", "index2")
.setQuery(
boolQuery().should(
nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder().setIgnoreUnmapped(true))
).should(termQuery("key", "value"))
),
"1",
"3"
);
}

public void testUseMaxDocInsteadOfSize() throws Exception {
Expand Down
Expand Up @@ -53,7 +53,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
import static org.hamcrest.Matchers.anyOf;
Expand Down Expand Up @@ -348,17 +348,18 @@ public void testPhraseType() {
.get();
assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(1L));

searchResponse = client().prepareSearch("test")
.setQuery(
randomizeType(
multiMatchQuery("the Ul", "full_name_phrase", "first_name_phrase", "last_name_phrase", "category_phrase").operator(
Operator.OR
).type(MatchQueryParser.Type.PHRASE_PREFIX)
)
)
.get();
assertSearchHits(searchResponse, "ultimate2", "ultimate1");
assertHitCount(searchResponse, 2L);
assertSearchHitsWithoutFailures(
client().prepareSearch("test")
.setQuery(
randomizeType(
multiMatchQuery("the Ul", "full_name_phrase", "first_name_phrase", "last_name_phrase", "category_phrase").operator(
Operator.OR
).type(MatchQueryParser.Type.PHRASE_PREFIX)
)
),
"ultimate2",
"ultimate1"
);
}

public void testSingleField() throws NoSuchFieldException, IllegalAccessException {
Expand Down

0 comments on commit 03ea4bb

Please sign in to comment.