small improvement to stats facet computing min/max

This commit is contained in:
kimchy 2011-03-31 17:41:21 +02:00
parent 3ff35d42b5
commit a0cddf0745
3 changed files with 38 additions and 20 deletions

View File

@ -36,9 +36,9 @@ public class ScriptStatisticalFacetCollector extends AbstractFacetCollector {
private final SearchScript script;
private double min = Double.NaN;
private double min = Double.MAX_VALUE;
private double max = Double.NaN;
private double max = Double.MIN_VALUE;
private double total = 0;
@ -54,10 +54,10 @@ public class ScriptStatisticalFacetCollector extends AbstractFacetCollector {
@Override protected void doCollect(int doc) throws IOException {
script.setNextDocId(doc);
double value = script.runAsDouble();
if (value < min || Double.isNaN(min)) {
if (value < min) {
min = value;
}
if (value > max || Double.isNaN(max)) {
if (value > max) {
max = value;
}
sumOfSquares += value * value;
@ -74,6 +74,12 @@ public class ScriptStatisticalFacetCollector extends AbstractFacetCollector {
}
@Override public Facet facet() {
if (min == Double.MAX_VALUE) {
min = Double.NaN;
}
if (max == Double.MIN_VALUE) {
max = Double.NaN;
}
return new InternalStatisticalFacet(facetName, min, max, total, sumOfSquares, count);
}
}

View File

@ -73,28 +73,34 @@ public class StatisticalFacetCollector extends AbstractFacetCollector {
}
@Override public Facet facet() {
if (statsProc.min == Double.MAX_VALUE) {
statsProc.min = Double.NaN;
}
if (statsProc.max == Double.MIN_VALUE) {
statsProc.max = Double.NaN;
}
return new InternalStatisticalFacet(facetName, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
}
public static class StatsProc implements NumericFieldData.DoubleValueInDocProc {
private double min = Double.NaN;
double min = Double.MAX_VALUE;
private double max = Double.NaN;
double max = Double.MIN_VALUE;
private double total = 0;
double total = 0;
private double sumOfSquares = 0.0;
double sumOfSquares = 0.0;
private long count;
long count;
private int missing;
int missing;
@Override public void onValue(int docId, double value) {
if (value < min || Double.isNaN(min)) {
if (value < min) {
min = value;
}
if (value > max || Double.isNaN(max)) {
if (value > max) {
max = value;
}
sumOfSquares += value * value;

View File

@ -78,28 +78,34 @@ public class StatisticalFieldsFacetCollector extends AbstractFacetCollector {
}
@Override public Facet facet() {
if (statsProc.min == Double.MAX_VALUE) {
statsProc.min = Double.NaN;
}
if (statsProc.max == Double.MIN_VALUE) {
statsProc.max = Double.NaN;
}
return new InternalStatisticalFacet(facetName, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
}
public static class StatsProc implements NumericFieldData.DoubleValueInDocProc {
private double min = Double.NaN;
double min = Double.MAX_VALUE;
private double max = Double.NaN;
double max = Double.MIN_VALUE;
private double total = 0;
double total = 0;
private double sumOfSquares = 0.0;
double sumOfSquares = 0.0;
private long count;
long count;
private int missing;
int missing;
@Override public void onValue(int docId, double value) {
if (value < min || Double.isNaN(min)) {
if (value < min) {
min = value;
}
if (value > max || Double.isNaN(max)) {
if (value > max) {
max = value;
}
sumOfSquares += value * value;