diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/bounded/BoundedValueHistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/bounded/BoundedValueHistogramFacetCollector.java index 24d8a4e7ed1..dbf4d99272f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/bounded/BoundedValueHistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/bounded/BoundedValueHistogramFacetCollector.java @@ -121,6 +121,8 @@ public class BoundedValueHistogramFacetCollector extends AbstractFacetCollector NumericFieldData valueFieldData; + final ValueAggregator valueAggregator = new ValueAggregator(); + public HistogramProc(long from, long to, long interval, long offset, int size) { this.from = from; this.to = to; @@ -138,18 +140,11 @@ public class BoundedValueHistogramFacetCollector extends AbstractFacetCollector InternalBoundedFullHistogramFacet.FullEntry entry = (InternalBoundedFullHistogramFacet.FullEntry) entries[index]; if (entry == null) { if (valueFieldData.multiValued()) { - double[] valuesValues = valueFieldData.doubleValues(docId); - entry = new InternalBoundedFullHistogramFacet.FullEntry(index, 1, Double.MAX_VALUE, Double.MIN_VALUE, valuesValues.length, 0); - for (double valueValue : valuesValues) { - entry.total += valueValue; - if (valueValue < entry.min) { - entry.min = valueValue; - } - if (valueValue > entry.max) { - entry.max = valueValue; - } - } + entry = new InternalBoundedFullHistogramFacet.FullEntry(index, 1, Double.MAX_VALUE, Double.MIN_VALUE, 0, 0); entries[index] = entry; + + valueAggregator.entry = entry; + valueFieldData.forEachValueInDoc(docId, valueAggregator); } else { double valueValue = valueFieldData.doubleValue(docId); entry = new InternalBoundedFullHistogramFacet.FullEntry(index, 1, valueValue, valueValue, 1, valueValue); @@ -158,17 +153,8 @@ public class BoundedValueHistogramFacetCollector extends AbstractFacetCollector } else { entry.count++; if (valueFieldData.multiValued()) { - double[] valuesValues = valueFieldData.doubleValues(docId); - entry.totalCount += valuesValues.length; - for (double valueValue : valuesValues) { - entry.total += valueValue; - if (valueValue < entry.min) { - entry.min = valueValue; - } - if (valueValue > entry.max) { - entry.max = valueValue; - } - } + valueAggregator.entry = entry; + valueFieldData.forEachValueInDoc(docId, valueAggregator); } else { entry.totalCount++; double valueValue = valueFieldData.doubleValue(docId); @@ -182,5 +168,22 @@ public class BoundedValueHistogramFacetCollector extends AbstractFacetCollector } } } + + + public static class ValueAggregator implements NumericFieldData.DoubleValueInDocProc { + + InternalBoundedFullHistogramFacet.FullEntry entry; + + @Override public void onValue(int docId, double value) { + entry.totalCount++; + entry.total += value; + if (value < entry.min) { + entry.min = value; + } + if (value > entry.max) { + entry.max = value; + } + } + } } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/ValueHistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/ValueHistogramFacetCollector.java index a9fedf3f628..aa612d1dd35 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/ValueHistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/ValueHistogramFacetCollector.java @@ -109,6 +109,8 @@ public class ValueHistogramFacetCollector extends AbstractFacetCollector { NumericFieldData valueFieldData; + final ValueAggregator valueAggregator = new ValueAggregator(); + public HistogramProc(long interval) { this.interval = interval; } @@ -118,18 +120,10 @@ public class ValueHistogramFacetCollector extends AbstractFacetCollector { InternalFullHistogramFacet.FullEntry entry = entries.get(bucket); if (entry == null) { if (valueFieldData.multiValued()) { - double[] valuesValues = valueFieldData.doubleValues(docId); - entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, Double.MAX_VALUE, Double.MIN_VALUE, valuesValues.length, 0); - for (double valueValue : valuesValues) { - entry.total += valueValue; - if (valueValue < entry.min) { - entry.min = valueValue; - } - if (valueValue > entry.max) { - entry.max = valueValue; - } - } + entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, Double.MAX_VALUE, Double.MIN_VALUE, 0, 0); entries.put(bucket, entry); + valueAggregator.entry = entry; + valueFieldData.forEachValueInDoc(docId, valueAggregator); } else { double valueValue = valueFieldData.doubleValue(docId); entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, valueValue, valueValue, 1, valueValue); @@ -138,17 +132,8 @@ public class ValueHistogramFacetCollector extends AbstractFacetCollector { } else { entry.count++; if (valueFieldData.multiValued()) { - double[] valuesValues = valueFieldData.doubleValues(docId); - entry.totalCount += valuesValues.length; - for (double valueValue : valuesValues) { - entry.total += valueValue; - if (valueValue < entry.min) { - entry.min = valueValue; - } - if (valueValue > entry.max) { - entry.max = valueValue; - } - } + valueAggregator.entry = entry; + valueFieldData.forEachValueInDoc(docId, valueAggregator); } else { entry.totalCount++; double valueValue = valueFieldData.doubleValue(docId); @@ -162,5 +147,21 @@ public class ValueHistogramFacetCollector extends AbstractFacetCollector { } } } + + public static class ValueAggregator implements NumericFieldData.DoubleValueInDocProc { + + InternalFullHistogramFacet.FullEntry entry; + + @Override public void onValue(int docId, double value) { + entry.totalCount++; + entry.total += value; + if (value < entry.min) { + entry.min = value; + } + if (value > entry.max) { + entry.max = value; + } + } + } } } \ No newline at end of file