From 0b5003cfed5187286ac49fa56d5aafe15165101d Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Wed, 16 Dec 2020 17:42:24 +0000 Subject: [PATCH] SOLR-14939: JSON range faceting to support cache=false parameter (#1992) --- solr/CHANGES.txt | 1 + .../apache/solr/search/facet/FacetContext.java | 2 ++ .../apache/solr/search/facet/FacetModule.java | 2 ++ .../solr/search/facet/FacetRangeProcessor.java | 17 ++++++++++++++++- .../solr/search/facet/TestJsonRangeFacets.java | 6 ++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index c704930b173..9d700e8fffa 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -262,6 +262,7 @@ Bug Fixes * SOLR-12182: Don't persist base_url in ZK as the URL scheme is variable, compute from node_name instead when reading state back from ZK. (Timothy Potter) +* SOLR-14939: JSON range faceting to support cache=false parameter. (Christine Poerschke, Mike Drob) Other Changes --------------------- diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetContext.java b/solr/core/src/java/org/apache/solr/search/facet/FacetContext.java index cef9d018cc7..1caf1bde827 100644 --- a/solr/core/src/java/org/apache/solr/search/facet/FacetContext.java +++ b/solr/core/src/java/org/apache/solr/search/facet/FacetContext.java @@ -38,6 +38,7 @@ public class FacetContext { Query filter; // TODO: keep track of as a DocSet or as a Query? DocSet base; FacetContext parent; + boolean cache = true; int flags; FacetDebugInfo debugInfo; @@ -100,6 +101,7 @@ public class FacetContext { ctx.filter = filter; // carry over from parent + ctx.cache = cache; ctx.flags = flags; ctx.qcontext = qcontext; ctx.req = req; diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetModule.java b/solr/core/src/java/org/apache/solr/search/facet/FacetModule.java index 0bd6651de1e..3cde18a5e4d 100644 --- a/solr/core/src/java/org/apache/solr/search/facet/FacetModule.java +++ b/solr/core/src/java/org/apache/solr/search/facet/FacetModule.java @@ -126,6 +126,7 @@ public class FacetModule extends SearchComponent { FacetComponentState facetState = getFacetComponentState(rb); if (facetState == null) return; + boolean cache = rb.req.getParams().getBool(CommonParams.CACHE, true); boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false); FacetContext fcontext = new FacetContext(); @@ -133,6 +134,7 @@ public class FacetModule extends SearchComponent { fcontext.req = rb.req; fcontext.searcher = rb.req.getSearcher(); fcontext.qcontext = QueryContext.newContext(fcontext.searcher); + fcontext.cache = cache; if (isShard) { fcontext.flags |= FacetContext.IS_SHARD; fcontext.facetInfo = facetState.facetInfo.isEmpty() ? null : (Map) facetState.facetInfo.get(FACET_REFINE); diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetRangeProcessor.java b/solr/core/src/java/org/apache/solr/search/facet/FacetRangeProcessor.java index 7319d284a76..dd7fc77c615 100644 --- a/solr/core/src/java/org/apache/solr/search/facet/FacetRangeProcessor.java +++ b/solr/core/src/java/org/apache/solr/search/facet/FacetRangeProcessor.java @@ -25,7 +25,9 @@ import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.schema.*; import org.apache.solr.search.DocSet; +import org.apache.solr.search.ExtendedQuery; import org.apache.solr.search.SyntaxError; +import org.apache.solr.search.WrappedQuery; import org.apache.solr.util.DateMathParser; import java.io.IOException; @@ -531,7 +533,20 @@ class FacetRangeProcessor extends FacetProcessor { private Query[] filters; private DocSet[] intersections; private void rangeStats(Range range, int slot) throws IOException { - Query rangeQ = sf.getType().getRangeQuery(null, sf, range.low == null ? null : calc.formatValue(range.low), range.high==null ? null : calc.formatValue(range.high), range.includeLower, range.includeUpper); + final Query rangeQ; + { + final Query rangeQuery = sf.getType().getRangeQuery(null, sf, range.low == null ? null : calc.formatValue(range.low), range.high==null ? null : calc.formatValue(range.high), range.includeLower, range.includeUpper); + if (fcontext.cache) { + rangeQ = rangeQuery; + } else if (rangeQuery instanceof ExtendedQuery) { + ((ExtendedQuery) rangeQuery).setCache(false); + rangeQ = rangeQuery; + } else { + final WrappedQuery wrappedQuery = new WrappedQuery(rangeQuery); + wrappedQuery.setCache(false); + rangeQ = wrappedQuery; + } + } // TODO: specialize count only DocSet intersection = fcontext.searcher.getDocSet(rangeQ, fcontext.base); filters[slot] = rangeQ; diff --git a/solr/core/src/test/org/apache/solr/search/facet/TestJsonRangeFacets.java b/solr/core/src/test/org/apache/solr/search/facet/TestJsonRangeFacets.java index da9d93e8a96..9124da34175 100644 --- a/solr/core/src/test/org/apache/solr/search/facet/TestJsonRangeFacets.java +++ b/solr/core/src/test/org/apache/solr/search/facet/TestJsonRangeFacets.java @@ -30,6 +30,7 @@ import org.junit.Test; public class TestJsonRangeFacets extends SolrTestCaseHS { private static SolrInstances servers; // for distributed testing + private static String cache; @SuppressWarnings("deprecation") @BeforeClass @@ -41,6 +42,7 @@ public class TestJsonRangeFacets extends SolrTestCaseHS { if (Boolean.getBoolean(NUMERIC_POINTS_SYSPROP)) System.setProperty(NUMERIC_DOCVALUES_SYSPROP,"true"); initCore("solrconfig-tlog.xml","schema_latest.xml"); + cache = Boolean.toString(random().nextBoolean()); } /** @@ -99,6 +101,7 @@ public class TestJsonRangeFacets extends SolrTestCaseHS { * will cause the correct "actual_end" to be returned */ private void doRangeOtherWhitebox(Client client) throws Exception { + client.queryDefaults().set("cache", cache); indexSimple(client); // false is default, but randomly check explicit false as well @@ -177,6 +180,7 @@ public class TestJsonRangeFacets extends SolrTestCaseHS { } private void doDateFacets(Client client) throws Exception { + client.queryDefaults().set("cache", cache); client.deleteByQuery("*:*", null); boolean multiValue = random().nextBoolean(); String dateField = multiValue? "b_dts": "b_dt"; @@ -244,6 +248,7 @@ public class TestJsonRangeFacets extends SolrTestCaseHS { } private void doRangeFacetWithRanges(Client client) throws Exception { + client.queryDefaults().set("cache", cache); client.deleteByQuery("*:*", null); indexSimple(client); @@ -315,6 +320,7 @@ public class TestJsonRangeFacets extends SolrTestCaseHS { } private void doRangeFacetWithRangesInNewFormat(Client client) throws Exception { + client.queryDefaults().set("cache", cache); client.deleteByQuery("*:*", null); indexSimple(client); SolrParams p = params("q", "*:*", "rows", "0");