You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2021/09/10 09:45:26 UTC

[lucene] branch main updated: LUCENE-10094: Delegate count() from CachingWrapperWeight (#289)

This is an automated email from the ASF dual-hosted git repository.

romseygeek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 1bb5285  LUCENE-10094: Delegate count() from CachingWrapperWeight (#289)
1bb5285 is described below

commit 1bb52859c88193e1e1674d6c360d772c20d1c2ea
Author: Alan Woodward <ro...@apache.org>
AuthorDate: Fri Sep 10 10:45:20 2021 +0100

    LUCENE-10094: Delegate count() from CachingWrapperWeight (#289)
    
    CachingWrapperWeight always returns -1 from its count() method, which
    disables the fast path for TermQuery, MatchAllDocQuery, etc, when running
    IndexSearcher.count(Query). This commit makes it delegate the method
    to its wrapped Weight.
---
 .../org/apache/lucene/search/LRUQueryCache.java    |  5 +++++
 .../apache/lucene/search/TestLRUQueryCache.java    | 26 +++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
index b048a61..7431ee4 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
@@ -824,6 +824,11 @@ public class LRUQueryCache implements QueryCache, Accountable {
     }
 
     @Override
+    public int count(LeafReaderContext context) throws IOException {
+      return in.count(context);
+    }
+
+    @Override
     public boolean isCacheable(LeafReaderContext ctx) {
       return in.isCacheable(ctx);
     }
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
index 526990a..57e7e7b 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
@@ -1719,7 +1719,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
         new BooleanQuery.Builder()
             .add(new TermQuery(new Term("id", "1")), BooleanClause.Occur.FILTER)
             .build();
-    assertEquals(1, searcher.count(query));
+    searcher.search(query, 10);
     assertEquals(1, queryCache.getCacheSize());
     assertEquals(0, queryCache.getEvictionCount());
 
@@ -1860,6 +1860,30 @@ public class TestLRUQueryCache extends LuceneTestCase {
     dir.close();
   }
 
+  public void testCountDelegation() throws IOException {
+    Directory dir = newDirectory();
+    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+    Document doc = new Document();
+    doc.add(new StringField("foo", "bar", Store.NO));
+    int numDocs = random().nextInt(100) + 20;
+    for (int i = 0; i < numDocs; i++) {
+      w.addDocument(doc);
+    }
+    final IndexReader reader = w.getReader();
+    final IndexSearcher searcher = newSearcher(reader);
+    searcher.setQueryCachingPolicy(ALWAYS_CACHE);
+
+    Query q = new TermQuery(new Term("foo", "bar"));
+    searcher.count(q); // add to cache
+
+    Weight weight = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE_NO_SCORES, 1);
+    assertNotEquals(-1, weight.count(reader.leaves().get(0)));
+
+    reader.close();
+    w.close();
+    dir.close();
+  }
+
   public void testSkipCachingForTermQuery() throws IOException {
     Directory dir = newDirectory();
     final RandomIndexWriter w = new RandomIndexWriter(random(), dir);