jpountz commented on a change in pull request #242:
URL: https://github.com/apache/lucene/pull/242#discussion_r700872648



##########
File path: lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java
##########
@@ -736,6 +737,49 @@ public void testReqOptPropagatesApproximations() throws 
IOException {
     dir.close();
   }
 
+  // LUCENE-9620 Add Weight#count(LeafReaderContext)
+  public void testQueryMatchesCount() throws IOException {
+    Directory dir = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+    int randomNumDocs = random().nextInt(500);
+    int numMatchingDocs = 0;
+
+    for (int i = 0; i < randomNumDocs; i++) {
+      Document doc = new Document();
+      Field f;
+      if (random().nextBoolean()) {
+        f = newTextField("field", "a b c " + random().nextInt(), 
Field.Store.NO);
+        numMatchingDocs++;
+      } else {
+        f = newTextField("field", String.valueOf(random().nextInt()), 
Field.Store.NO);
+      }
+      doc.add(f);
+      w.addDocument(doc);
+      if (random().nextBoolean()) {
+        w.commit();
+      }
+    }
+    w.commit();
+    w.forceMerge(1);
+
+    DirectoryReader reader = w.getReader();
+    final IndexSearcher searcher = new IndexSearcher(reader);
+
+    BooleanQuery.Builder q = new BooleanQuery.Builder();
+    q.add(new PhraseQuery("field", "a", "b"), Occur.SHOULD);
+    q.add(new TermQuery(new Term("field", "c")), Occur.SHOULD);
+
+    Query builtQuery = q.build();
+
+    assert searcher.count(builtQuery) == numMatchingDocs;
+    final Weight weight = searcher.createWeight(builtQuery, 
ScoreMode.COMPLETE, 1);
+    // tests that the Weight#count API returns -1 instead of returning the 
total number of matches
+    assert weight.count(reader.leaves().get(0)) == -1;

Review comment:
       you should use assertEquals, rather than assert, since the latter 
doesn't do anything if assertions are disabled

##########
File path: lucene/core/src/java/org/apache/lucene/search/Weight.java
##########
@@ -174,6 +174,27 @@ public BulkScorer bulkScorer(LeafReaderContext context) 
throws IOException {
     return new DefaultBulkScorer(scorer);
   }
 
+  /**
+   * Counts the number of live documents that match a given {@link 
Weight#parentQuery} in a leaf.
+   *
+   * <p>The default implementation returns -1 for every query. This indicates 
that the count could
+   * not computed in O(1) time.

Review comment:
       ```suggestion
      * not be computed in O(1) time.
   ```

##########
File path: lucene/core/src/test/org/apache/lucene/search/TestTermQuery.java
##########
@@ -103,6 +103,39 @@ public void 
testCreateWeightDoesNotSeekIfScoresAreNotNeeded() throws IOException
     IOUtils.close(reader, w, dir);
   }
 
+  // LUCENE-9620 Add Weight#count(LeafReaderContext)
+  public void testQueryMatchesCount() throws IOException {
+    Directory dir = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+    int randomNumDocs = random().nextInt(500);
+    int numMatchingDocs = 0;
+
+    for (int i = 0; i < randomNumDocs; i++) {
+      Document doc = new Document();
+      if (random().nextBoolean()) {
+        doc.add(new StringField("foo", "bar", Store.NO));
+        numMatchingDocs++;
+      }
+      w.addDocument(doc);
+      if (random().nextBoolean()) {
+        w.commit();

Review comment:
       let's not commit since you forcemerge in the end anyway?

##########
File path: lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java
##########
@@ -736,6 +737,49 @@ public void testReqOptPropagatesApproximations() throws 
IOException {
     dir.close();
   }
 
+  // LUCENE-9620 Add Weight#count(LeafReaderContext)
+  public void testQueryMatchesCount() throws IOException {
+    Directory dir = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+    int randomNumDocs = random().nextInt(500);
+    int numMatchingDocs = 0;
+
+    for (int i = 0; i < randomNumDocs; i++) {
+      Document doc = new Document();
+      Field f;
+      if (random().nextBoolean()) {
+        f = newTextField("field", "a b c " + random().nextInt(), 
Field.Store.NO);
+        numMatchingDocs++;
+      } else {
+        f = newTextField("field", String.valueOf(random().nextInt()), 
Field.Store.NO);
+      }
+      doc.add(f);
+      w.addDocument(doc);
+      if (random().nextBoolean()) {
+        w.commit();
+      }

Review comment:
       let's not commit since you forcemerge in the end anyway?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to