lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [lucene-solr] branch branch_8x updated: SOLR-13808: caching {!bool filter=..} by default.
Date Sat, 28 Dec 2019 20:35:51 GMT
This is an automated email from the ASF dual-hosted git repository.

mkhl pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/branch_8x by this push:
     new c01b45b  SOLR-13808: caching {!bool filter=..} by default.
c01b45b is described below

commit c01b45b924db876e715579931cd64f2b008da4bf
Author: Mikhail Khludnev <mkhl@apache.org>
AuthorDate: Sun Oct 27 19:45:23 2019 +0100

    SOLR-13808: caching {!bool filter=..} by default.
---
 solr/CHANGES.txt                                   |  5 ++
 .../org/apache/solr/search/BoolQParserPlugin.java  | 14 +++-
 .../apache/solr/search/json/TestJsonRequest.java   | 79 +++++++++++++++++++---
 solr/solr-ref-guide/src/other-parsers.adoc         |  2 +-
 4 files changed, 87 insertions(+), 13 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 686076d..fe32d14 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -70,6 +70,8 @@ Upgrade Notes
   If you prefer to keep the old (but insecure) serialization strategy, you can start your
nodes using the
   property: `-Dsolr.useUnsafeOverseerResponse=true`. Keep in mind that this will be removed
in future version of Solr.
  
+ * SOLR-13808: add cache=false into uderneath BoolQParser's filter clause or {"bool":{"filter":..}}
to avoid caching in
+   filterCache. (Mikhail Khludnev)
 
 New Features
 ---------------------
@@ -98,6 +100,9 @@ Improvements
 
 * SOLR-14131: Add maxQueryLength option to DirectSolrSpellchecker. (Andy Webb via Bruno Roustant)
 
+* SOLR-13808: filter in BoolQParser and {"bool":{"filter":..}} in Query DSL are cached by
default 
+  (Mikhail Khludnev)
+
 Optimizations
 ---------------------
 (No changes)
diff --git a/solr/core/src/java/org/apache/solr/search/BoolQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/BoolQParserPlugin.java
index c0bebe5..04a9d5a 100644
--- a/solr/core/src/java/org/apache/solr/search/BoolQParserPlugin.java
+++ b/solr/core/src/java/org/apache/solr/search/BoolQParserPlugin.java
@@ -21,6 +21,7 @@ import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Query;
 import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.query.FilterQuery;
 import org.apache.solr.request.SolrQueryRequest;
 
 /**
@@ -49,7 +50,18 @@ public class BoolQParserPlugin extends QParserPlugin {
       private void addQueries(BooleanQuery.Builder builder, String[] subQueries, BooleanClause.Occur
occur) throws SyntaxError {
         if (subQueries != null) {
           for (String subQuery : subQueries) {
-            builder.add(subQuery(subQuery, null).parse(), occur);
+            final QParser subParser = subQuery(subQuery, null);
+            Query extQuery;
+            if (BooleanClause.Occur.FILTER.equals(occur)) {
+              extQuery = subParser.getQuery();
+              if (!(extQuery instanceof ExtendedQuery) || (
+                  ((ExtendedQuery) extQuery).getCache())) {
+                  extQuery = new FilterQuery(extQuery);
+              }
+            } else {
+              extQuery = subParser.parse();
+            }
+            builder.add(extQuery, occur);
           }
         }
       }
diff --git a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
index 9f34db0..10e68a7 100644
--- a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
+++ b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
@@ -16,16 +16,23 @@
  */
 package org.apache.solr.search.json;
 
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.util.LuceneTestCase;
 import org.apache.solr.JSONTestUtil;
 import org.apache.solr.SolrTestCaseHS;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.search.DocSet;
+import org.apache.solr.search.FastLRUCache;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 
+@SuppressWarnings("deprecation")
 @LuceneTestCase.SuppressCodecs({"Lucene3x","Lucene40","Lucene41","Lucene42","Lucene45","Appending"})
 public class TestJsonRequest extends SolrTestCaseHS {
 
@@ -303,17 +310,9 @@ public class TestJsonRequest extends SolrTestCaseHS {
         , "response/numFound==1"
     );
 
-    client.testJQ( params("json","{ " +
-            " query : {" +
-            "  bool : {" +
-            "   must : '{!lucene q.op=AND df=cat_s}A'" +
-            "   must_not : '{!lucene v=\\'id:1\\'}'" +
-            "  }" +
-            " }" +
-            "}")
-        , "response/numFound==1"
-    );
-
+    assertCatANot1(client, "must");
+    
+    testFilterCachingLocally(client);
 
     client.testJQ( params("json","{" +
             " query : '*:*'," +
@@ -407,6 +406,64 @@ public class TestJsonRequest extends SolrTestCaseHS {
 
   }
 
+  private static void testFilterCachingLocally(Client client) throws Exception {
+    if(client.getClientProvider()==null) {
+      final SolrQueryRequest request = req();
+      try {
+        final FastLRUCache<Query,DocSet> filterCache = (FastLRUCache<Query,DocSet>)
request.getSearcher().getFilterCache();
+        filterCache.clear();
+        final TermQuery catA = new TermQuery(new Term("cat_s", "A"));
+        assertNull("cache is empty",filterCache.get(catA));
+
+        if(random().nextBoolean()) {
+          if(random().nextBoolean()) {
+            if(random().nextBoolean()) {
+              assertCatANot1(client, "must");
+            }else {
+              assertCatANot1(client, "must", "cat_s:A");
+            }
+          } else {
+            assertCatANot1(client, "must","{!lucene q.op=AND df=cat_s "+"cache="+random().nextBoolean()+"}A"
);
+          }   
+        } else {
+          assertCatANot1(client, "filter", "{!lucene q.op=AND df=cat_s cache=false}A");
+        }
+        assertNull("no cache still",filterCache.get(catA));
+
+        if (random().nextBoolean()) {
+          if (random().nextBoolean()) {
+            assertCatANot1(client, "filter", "cat_s:A");
+          } else {
+            assertCatANot1(client, "filter");
+          }
+        } else {
+          assertCatANot1(client, "filter","{!lucene q.op=AND df=cat_s cache=true}A");
+        }
+        assertNotNull("got cached ",filterCache.get(catA));
+
+      } finally {
+        request.close();
+      }
+    }
+  }
+
+  private static void assertCatANot1(Client client, final String occur) throws Exception
{
+    assertCatANot1(client, occur,  "{!lucene q.op=AND df=cat_s}A");
+  }
+
+  private static void assertCatANot1(Client client, final String occur, String catAclause)
throws Exception {
+    client.testJQ( params("json","{ " +
+            " query : {" +
+            "  bool : {" +
+            "   " + occur + " : '"+ catAclause+ "'" +
+            "   must_not : '{!lucene v=\\'id:1\\'}'" +
+            "  }" +
+            " }" +
+            "}")
+        , "response/numFound==1"
+    );
+  }
+
   public static void doJsonRequestWithTag(Client client) throws Exception {
     addDocs(client);
 
diff --git a/solr/solr-ref-guide/src/other-parsers.adoc b/solr/solr-ref-guide/src/other-parsers.adoc
index fed1985..1c386d4 100644
--- a/solr/solr-ref-guide/src/other-parsers.adoc
+++ b/solr/solr-ref-guide/src/other-parsers.adoc
@@ -188,7 +188,7 @@ A list of queries that *must not* appear in matching documents.
 A list of queries *should* appear in matching documents. For a BooleanQuery with no `must`
queries, one or more `should` queries must match a document for the BooleanQuery to match.
 
 `filter`::
-A list of queries that *must* appear in matching documents. However, unlike `must`, the score
of filter queries is ignored.
+A list of queries that *must* appear in matching documents. However, unlike `must`, the score
of filter queries is ignored. Also, these queries are cached in filter cache. To avoid caching
add either `cache=false` as local parameter, or `"cache":"false"` property to underneath Query
DLS Object.  
 
 *Examples*
 


Mime
View raw message