Skip to content

Apache Lucene open-source search software

SOLR-8192: SOLR-8206: fix allBuckets for multi-valued fields, fix limit:0 and normalize offset to 0 when limit==0

Yonik Seeleyb3ee21b7e940
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 24849b130d1..bcaf668cd31 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -295,6 +295,11 @@ Bug Fixes
* SOLR-7993: Raw json output for fields stopped working in 5.3.0 when requested fields do not include
the unique key field name. (Bill Bell, Ryan McKinley via shalin)
+* SOLR-8192: JSON Facet API allBuckets:true did not work correctly when faceting
+ on a multi-valued field with sub-facets / facet functions. (yonik)
+
+* SOLR-8206: JSON Facet API limit:0 did not always work correctly. (yonik)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetField.java b/solr/core/src/java/org/apache/solr/search/facet/FacetField.java
index d054f27ec1e..17aba21d8d0 100644
--- a/solr/core/src/java/org/apache/solr/search/facet/FacetField.java
+++ b/solr/core/src/java/org/apache/solr/search/facet/FacetField.java
@@ -271,7 +271,7 @@ abstract class FacetFieldProcessor extends FacetProcessor<FacetField> {
return;
}
- // create the deffered aggs up front for use by allBuckets
+ // create the deferred aggs up front for use by allBuckets
createOtherAccs(numDocs, 1);
}
@@ -449,6 +449,20 @@ class SpecialSlotAcc extends SlotAcc {
}
}
+ @Override
+ public void setNextReader(LeafReaderContext readerContext) throws IOException {
+ // collectAcc and otherAccs will normally have setNextReader called directly on them.
+ // This, however, will be used when collect(DocSet,slot) variant is used on this Acc.
+ if (collectAcc != null) {
+ collectAcc.setNextReader(readerContext);
+ }
+ if (otherAccs != null) {
+ for (SlotAcc otherAcc : otherAccs) {
+ otherAcc.setNextReader(readerContext);
+ }
+ }
+ }
+
@Override
public int compare(int slotA, int slotB) {
throw new UnsupportedOperationException();
@@ -561,7 +575,7 @@ abstract class FacetFieldProcessorFCBase extends FacetFieldProcessor {
// add a modest amount of over-request if this is a shard request
int lim = freq.limit >= 0 ? (fcontext.isShard() ? (int)(freq.limit*1.1+4) : (int)freq.limit) : Integer.MAX_VALUE;
- int maxsize = (int)(freq.limit > 0 ? freq.offset + lim : Integer.MAX_VALUE - 1);
+ int maxsize = (int)(freq.limit >= 0 ? freq.offset + lim : Integer.MAX_VALUE - 1);
maxsize = Math.min(maxsize, nTerms);
final int sortMul = freq.sortDirection.getMultiplier();
@@ -596,7 +610,7 @@ abstract class FacetFieldProcessorFCBase extends FacetFieldProcessor {
bottom.slot = i;
bottom = queue.updateTop();
}
- } else {
+ } else if (lim > 0) {
// queue not full
Slot s = new Slot();
s.slot = i;
diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorNumeric.java b/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorNumeric.java
index cd04518a080..3deed979350 100644
--- a/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorNumeric.java
+++ b/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorNumeric.java
@@ -342,7 +342,7 @@ class FacetFieldProcessorNumeric extends FacetFieldProcessor {
// add a modest amount of over-request if this is a shard request
int lim = freq.limit >= 0 ? (fcontext.isShard() ? (int)(freq.limit*1.1+4) : (int)freq.limit) : Integer.MAX_VALUE;
- int maxsize = (int)(freq.limit > 0 ? freq.offset + lim : Integer.MAX_VALUE - 1);
+ int maxsize = (int)(freq.limit >= 0 ? freq.offset + lim : Integer.MAX_VALUE - 1);
maxsize = Math.min(maxsize, table.cardinality);
final int sortMul = freq.sortDirection.getMultiplier();
diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetRequest.java b/solr/core/src/java/org/apache/solr/search/facet/FacetRequest.java
index eee88cc3d63..571e295dbf7 100644
--- a/solr/core/src/java/org/apache/solr/search/facet/FacetRequest.java
+++ b/solr/core/src/java/org/apache/solr/search/facet/FacetRequest.java
@@ -523,6 +523,7 @@ class FacetFieldParser extends FacetParser<FacetField> {
facet.field = getField(m);
facet.offset = getLong(m, "offset", facet.offset);
facet.limit = getLong(m, "limit", facet.limit);
+ if (facet.limit == 0) facet.offset = 0; // normalize. an offset with a limit of non-zero isn't useful.
facet.mincount = getLong(m, "mincount", facet.mincount);
facet.missing = getBoolean(m, "missing", facet.missing);
facet.numBuckets = getBoolean(m, "numBuckets", facet.numBuckets);
diff --git a/solr/core/src/test/org/apache/solr/search/facet/TestJsonFacets.java b/solr/core/src/test/org/apache/solr/search/facet/TestJsonFacets.java
index 8f596fa9aa9..d805613fb70 100644
--- a/solr/core/src/test/org/apache/solr/search/facet/TestJsonFacets.java
+++ b/solr/core/src/test/org/apache/solr/search/facet/TestJsonFacets.java
@@ -802,6 +802,40 @@ public class TestJsonFacets extends SolrTestCaseHS {
"x:{ buckets:[{val:a, count:3}, {val:b, count:3}] , allBuckets:{count:6} } }"
);
+ // allBuckets for multi-valued field with stats. This can sometimes take a different path of adding complete DocSets to the Acc
+ // also test limit:0
+ client.testJQ(params(p, "q", "*:*"
+ , "json.facet", "{" +
+ " f0:{type:terms, field:${multi_ss}, allBuckets:true, limit:0} " +
+ ",f1:{type:terms, field:${multi_ss}, allBuckets:true, limit:0, offset:1} " + // offset with 0 limit
+ ",f2:{type:terms, field:${multi_ss}, allBuckets:true, limit:0, facet:{x:'sum(${num_d})'}, sort:'x desc' } " +
+ ",f3:{type:terms, field:${multi_ss}, allBuckets:true, limit:0, missing:true, facet:{x:'sum(${num_d})', y:'avg(${num_d})'}, sort:'x desc' } " +
+ "}"
+ )
+ , "facets=={ 'count':6, " +
+ " f0:{allBuckets:{count:6}, buckets:[]}" +
+ ",f1:{allBuckets:{count:6}, buckets:[]}" +
+ ",f2:{allBuckets:{count:6, x:-15.0}, buckets:[]} " +
+ ",f3:{allBuckets:{count:6, x:-15.0, y:-2.5}, buckets:[], missing:{count:2, x:4.0, y:4.0} }} " +
+ "}"
+ );
+
+ // allBuckets with numeric field with stats.
+ // also test limit:0
+ client.testJQ(params(p, "q", "*:*"
+ , "json.facet", "{" +
+ " f0:{type:terms, field:${num_i}, allBuckets:true, limit:0} " +
+ ",f1:{type:terms, field:${num_i}, allBuckets:true, limit:0, offset:1} " + // offset with 0 limit
+ ",f2:{type:terms, field:${num_i}, allBuckets:true, limit:0, facet:{x:'sum(${num_d})'}, sort:'x desc' } " +
+ "}"
+ )
+ , "facets=={ 'count':6, " +
+ " f0:{allBuckets:{count:5}, buckets:[]}" +
+ ",f1:{allBuckets:{count:5}, buckets:[]}" +
+ ",f2:{allBuckets:{count:5, x:3.0}, buckets:[]} " +
+ "}"
+ );
+
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// test converting legacy facets