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



##########
File path: 
lucene/core/src/test/org/apache/lucene/search/TestDocValuesFieldExistsQuery.java
##########
@@ -206,6 +210,42 @@ public void testFieldExistsButNoDocsHaveField() throws 
IOException {
     dir.close();
   }
 
+  public void testQueryMatchesCount() throws IOException {
+    Directory dir = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+    int randomNumDocs = TestUtil.nextInt(random(), 10, 100);
+    int numMatchingDocs = 0;
+
+    for (int i = 0; i < randomNumDocs; i++) {
+      Document doc = new Document();
+      if (random().nextBoolean()) {
+        doc.add(new NumericDocValuesField("f", i));
+        doc.add(new LongPoint("f", i));
+        numMatchingDocs++;
+      }
+      w.addDocument(doc);
+    }
+    w.forceMerge(1);
+
+    DirectoryReader reader = w.getReader();
+    final IndexSearcher searcher = new IndexSearcher(reader);
+
+    Query testQuery = new DocValuesFieldExistsQuery("f");
+    assertEquals(searcher.count(testQuery), numMatchingDocs);
+    final Weight weight = searcher.createWeight(testQuery, ScoreMode.COMPLETE, 
1);
+    assertEquals(weight.count(reader.leaves().get(0)), numMatchingDocs);
+
+    // Test that we can't count in O(1) when there are deleted documents
+    w.deleteDocuments(LongPoint.newRangeQuery("f", 0L, 10L));
+    w.commit();

Review comment:
       FWIW there is no need to commit, the call to `getReader` below will make 
sure to make changes visible.

##########
File path: 
lucene/core/src/java/org/apache/lucene/search/DocValuesFieldExistsQuery.java
##########
@@ -74,6 +74,16 @@ public Scorer scorer(LeafReaderContext context) throws 
IOException {
         return new ConstantScoreScorer(this, score(), scoreMode, iterator);
       }
 
+      @Override
+      public int count(LeafReaderContext context) throws IOException {
+        final LeafReader reader = context.reader();
+        final FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
+        if (reader.hasDeletions() || fieldInfo == null || 
fieldInfo.getPointDimensionCount() == 0) {

Review comment:
       To handle terms, you need to check than `fieldInfo.getIndexOptions()` to 
know if the field is indexed. And then you can do 
`reader.terms(field).getDocCount()`.




-- 
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