Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-7800: avg facet function should skip missing values

Yonik Seeley58ec4d882427
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a2f7c22dc43..2faa3c5ee4b 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -329,6 +329,12 @@ Other Changes
* SOLR-7703: Authentication plugin is now loaded using the RessourceLoader.
(Avi Digmi via Anshum Gupta)
+* SOLR-7800: JSON Facet API: the avg() facet function now skips missing values
+ rather than treating them as a 0 value. The def() function can be used to
+ treat missing values as 0 if that is desired.
+ Example: facet:{ mean:"avg(def(myfield,0))" }
+
+
================== 5.2.1 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release
diff --git a/solr/core/src/java/org/apache/solr/search/facet/SlotAcc.java b/solr/core/src/java/org/apache/solr/search/facet/SlotAcc.java
index db638d90459..30e5485d8bc 100644
--- a/solr/core/src/java/org/apache/solr/search/facet/SlotAcc.java
+++ b/solr/core/src/java/org/apache/solr/search/facet/SlotAcc.java
@@ -348,9 +348,11 @@ class AvgSlotAcc extends DoubleFuncSlotAcc {
@Override
public void collect(int doc, int slotNum) {
- double val = values.doubleVal(doc); // todo: worth trying to share this value across multiple stats that need it?
- result[slotNum] += val;
- counts[slotNum] += 1;
+ double val = values.doubleVal(doc);
+ if (val != 0 || values.exists(doc)) {
+ result[slotNum] += val;
+ counts[slotNum] += 1;
+ }
}
private double avg(double tot, int count) {
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 3e6d67d5a5d..44f314ac923 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
@@ -737,13 +737,13 @@ public class TestJsonFacets extends SolrTestCaseHS {
// stats at top level
client.testJQ(params(p, "q", "*:*"
- , "json.facet", "{ sum1:'sum(${num_d})', sumsq1:'sumsq(${num_d})', avg1:'avg(${num_d})', min1:'min(${num_d})', max1:'max(${num_d})'" +
+ , "json.facet", "{ sum1:'sum(${num_d})', sumsq1:'sumsq(${num_d})', avg1:'avg(${num_d})', avg2:'avg(def(${num_d},0))', min1:'min(${num_d})', max1:'max(${num_d})'" +
", numwhere:'unique(${where_s})', unique_num_i:'unique(${num_i})', unique_num_d:'unique(${num_d})', unique_date:'unique(${date})'" +
", where_hll:'hll(${where_s})', hll_num_i:'hll(${num_i})', hll_num_d:'hll(${num_d})', hll_date:'hll(${date})'" +
", med:'percentile(${num_d},50)', perc:'percentile(${num_d},0,50.0,100)' }"
)
, "facets=={ 'count':6, " +
- "sum1:3.0, sumsq1:247.0, avg1:0.5, min1:-9.0, max1:11.0" +
+ "sum1:3.0, sumsq1:247.0, avg1:0.6, avg2:0.5, min1:-9.0, max1:11.0" +
", numwhere:2, unique_num_i:4, unique_num_d:5, unique_date:5" +
", where_hll:2, hll_num_i:4, hll_num_d:5, hll_date:5" +
", med:2.0, perc:[-9.0,2.0,11.0] }"