Apache Lucene open-source search software
SOLR-5624: Enable QueryResultCache for CollapsingQParserPlugin
Joel Bernstein97ef952ceda4
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c424074cb64..4fead71946d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -315,6 +315,9 @@ Optimizations* SOLR-4227: Wrap XML RequestWriter's OutputStreamWriter in a BufferedWriterto avoid frequent converter invocations. (Conrad Herrmann, shalin)+* SOLR-5624: Enable QueryResultCache for CollapsingQParserPlugin.+ (David Boychuck, Joel Bernstein)+Other Changes---------------------diff --git a/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.javaindex e613e79fcdd..8b1584148d9 100644--- a/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java@@ -23,6 +23,7 @@ import org.apache.lucene.queries.function.ValueSource;import org.apache.lucene.util.BytesRef;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.request.LocalSolrQueryRequest;+import org.apache.solr.request.SolrRequestInfo;import org.apache.solr.schema.TrieFloatField;import org.apache.solr.schema.TrieIntField;import org.apache.solr.schema.TrieLongField;@@ -126,13 +127,12 @@ public class CollapsingQParserPlugin extends QParserPlugin {public class CollapsingPostFilter extends ExtendedQueryBase implements PostFilter, ScoreFilter {- private Object cacheId;private String field;private String max;private String min;private boolean needsScores = true;private int nullPolicy;- private Map context;+ private Set<String> boosted;public static final int NULL_POLICY_IGNORE = 0;public static final int NULL_POLICY_COLLAPSE = 1;public static final int NULL_POLICY_EXPAND = 2;@@ -154,15 +154,25 @@ public class CollapsingQParserPlugin extends QParserPlugin {}public int hashCode() {- return this.cacheId.hashCode()*((1+Float.floatToIntBits(this.getBoost()))*31);+ int hashCode = field.hashCode();+ hashCode = max!=null ? hashCode+max.hashCode():hashCode;+ hashCode = min!=null ? hashCode+min.hashCode():hashCode;+ hashCode = boosted!=null ? hashCode+boosted.hashCode():hashCode;+ hashCode = hashCode+nullPolicy;+ hashCode = hashCode*((1+Float.floatToIntBits(this.getBoost()))*31);+ return hashCode;}public boolean equals(Object o) {- //Uses the unique id for equals to ensure that the query result cache always fails.+if(o instanceof CollapsingPostFilter) {CollapsingPostFilter c = (CollapsingPostFilter)o;- //Do object comparison to be sure only the same object will return true.- if(this.cacheId == c.cacheId && this.getBoost()==c.getBoost()) {+ if(this.field.equals(c.field) &&+ ((this.max == null && c.max == null) || (this.max != null && c.max != null && this.max.equals(c.max))) &&+ ((this.min == null && c.min == null) || (this.min != null && c.min != null && this.min.equals(c.min))) &&+ this.nullPolicy == c.nullPolicy &&+ ((this.boosted == null && c.boosted == null) || (this.boosted == c.boosted)) &&+ this.getBoost()==c.getBoost()) {return true;}}@@ -178,11 +188,9 @@ public class CollapsingQParserPlugin extends QParserPlugin {}public CollapsingPostFilter(SolrParams localParams, SolrParams params, SolrQueryRequest request) throws IOException {- this.cacheId = new Object();this.field = localParams.get("field");this.max = localParams.get("max");this.min = localParams.get("min");- this.context = request.getContext();if(this.min != null || this.max != null) {this.needsScores = needsScores(params);}@@ -201,6 +209,7 @@ public class CollapsingQParserPlugin extends QParserPlugin {private IntOpenHashSet getBoostDocs(SolrIndexSearcher indexSearcher, Set<String> boosted) throws IOException {IntOpenHashSet boostDocs = null;+if(boosted != null) {SchemaField idField = indexSearcher.getSchema().getUniqueKeyField();String fieldName = idField.getName();@@ -296,7 +305,10 @@ public class CollapsingQParserPlugin extends QParserPlugin {int maxDoc = searcher.maxDoc();int leafCount = searcher.getTopReaderContext().leaves().size();- IntOpenHashSet boostDocs = getBoostDocs(searcher, (Set<String>) (this.context.get(QueryElevationComponent.BOOSTED)));+ SolrRequestInfo info = SolrRequestInfo.getRequestInfo();+ this.boosted = (Set<String>)info.getReq().getContext().get(QueryElevationComponent.BOOSTED);++ IntOpenHashSet boostDocs = getBoostDocs(searcher, this.boosted);if(this.min != null || this.max != null) {@@ -344,7 +356,7 @@ public class CollapsingQParserPlugin extends QParserPlugin {}}- if(this.context.containsKey(QueryElevationComponent.BOOSTED)) {+ if(this.boosted != null) {return true;}diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.javaindex 418cd3399b1..ad6db3c177b 100644--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java@@ -18,6 +18,7 @@ package org.apache.solr.search;import java.util.HashSet;import java.util.Set;+import java.util.Map;import org.apache.lucene.search.Query;import org.apache.lucene.search.QueryUtils;@@ -197,9 +198,32 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {public void testQueryCollapse() throws Exception {SolrQueryRequest req = req("myField","foo_s");+try {assertQueryEquals("collapse", req,"{!collapse field=$myField}");++ assertQueryEquals("collapse", req,+ "{!collapse field=$myField max=a}");++ assertQueryEquals("collapse", req,+ "{!collapse field=$myField min=a}");++ assertQueryEquals("collapse", req,+ "{!collapse field=$myField max=a nullPolicy=expand}");++ //Add boosted documents to the request context.+ Map context = req.getContext();+ Set boosted = new HashSet();+ boosted.add("doc1");+ boosted.add("doc2");+ context.put("BOOSTED", boosted);++ assertQueryEquals("collapse", req,+ "{!collapse field=$myField min=a}",+ "{!collapse field=$myField min=a nullPolicy=ignore}");++} finally {req.close();}diff --git a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.javaindex 5ca3cf0d7aa..d342aa8457b 100644--- a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java+++ b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java@@ -140,9 +140,10 @@ public class TestCollapseQParserPlugin extends SolrTestCaseJ4 {params.add("qf", "term_s");params.add("qt", "/elevate");assertQ(req(params), "*[count(//doc)=4]",- "//result/doc[1]/float[@name='id'][.='1.0']");--+ "//result/doc[1]/float[@name='id'][.='1.0']",+ "//result/doc[2]/float[@name='id'][.='2.0']",+ "//result/doc[3]/float[@name='id'][.='3.0']",+ "//result/doc[4]/float[@name='id'][.='6.0']");//Test collapse by min int field and sort