From 40e8f00f766c7f0f5ae4c0180f91f3394621e233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 5 Jul 2023 12:14:56 +0200 Subject: [PATCH] Changing to modified term query in test --- .../query/PartialHitCountCollectorTests.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/query/PartialHitCountCollectorTests.java b/server/src/test/java/org/elasticsearch/search/query/PartialHitCountCollectorTests.java index 78fe9792ec1e..aacc4411a60d 100644 --- a/server/src/test/java/org/elasticsearch/search/query/PartialHitCountCollectorTests.java +++ b/server/src/test/java/org/elasticsearch/search/query/PartialHitCountCollectorTests.java @@ -16,6 +16,7 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.FilterWeight; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; @@ -27,7 +28,6 @@ import org.elasticsearch.test.ESTestCase; import java.io.IOException; -import java.util.List; public class PartialHitCountCollectorTests extends ESTestCase { @@ -105,33 +105,33 @@ public void testCollectedHitCount() throws Exception { } public void testCollectedHitCountEarlyTerminated() throws Exception { - Query query = new BooleanQuery.Builder().add(new TermQuery(new Term("string", "a1")), BooleanClause.Occur.SHOULD) - .add(new TermQuery(new Term("string", "b3")), BooleanClause.Occur.SHOULD) - .add(new TermQuery(new Term("string", "b6")), BooleanClause.Occur.SHOULD) + Query query = new BooleanQuery.Builder().add(termQuery(new Term("string", "a1")), BooleanClause.Occur.SHOULD) + .add(termQuery(new Term("string", "b3")), BooleanClause.Occur.SHOULD) + .add(termQuery(new Term("string", "b6")), BooleanClause.Occur.SHOULD) .build(); // there's three docs matching the query: any totalHitsThreshold lower than 3 will trigger early termination int totalHitsThreshold = randomInt(2); PartialHitCountCollector partialHitCountCollector = new PartialHitCountCollector(totalHitsThreshold); searcher.search(query, partialHitCountCollector); - try { - assertEquals(totalHitsThreshold, partialHitCountCollector.getTotalHits()); - assertTrue(partialHitCountCollector.hasEarlyTerminated()); - } catch (AssertionError e) { - // if we get very unlucky with the document distribution, we can get more than one segment with only one document - // in this case early termination might not kick in because we never collect - // check if this was the case here and disregard error in that case - Weight weight = searcher.createWeight(query, ScoreMode.TOP_DOCS, 1.0f); - List leafContexts = searcher.getLeafContexts(); - int totalHitsWithoutCollecting = 0; - for (LeafReaderContext leafContext : leafContexts) { - int count = weight.count(leafContext); - if (count != -1) { - totalHitsWithoutCollecting += count; - } + assertEquals(totalHitsThreshold, partialHitCountCollector.getTotalHits()); + assertTrue(partialHitCountCollector.hasEarlyTerminated()); + } + + /** + * Create a {@link TermQuery} which cannot terminate collection early + */ + private TermQuery termQuery(Term term) { + return new TermQuery(term) { + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { + + Weight w = super.createWeight(searcher, scoreMode, boost); + return new FilterWeight(w) { + public int count(LeafReaderContext context) throws IOException { + return -1; + } + }; } - assertTrue(totalHitsWithoutCollecting > totalHitsThreshold); - assertFalse(partialHitCountCollector.hasEarlyTerminated()); - } + }; } }