From 737589f50d85f631fec2c59c86e8d175eb639211 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 27 Sep 2011 00:39:37 +0300 Subject: [PATCH] better handling of mean computation of some facets to handle division by 0, though, won't happen on most facets --- .../datehistogram/InternalFullDateHistogramFacet.java | 11 ++++++++++- .../search/facet/geodistance/GeoDistanceFacet.java | 3 +++ .../bounded/InternalBoundedFullHistogramFacet.java | 3 +++ .../elasticsearch/search/facet/range/RangeFacet.java | 3 +++ .../facet/statistical/InternalStatisticalFacet.java | 3 +++ .../doubles/InternalTermsStatsDoubleFacet.java | 11 ++++++++++- .../termsstats/longs/InternalTermsStatsLongFacet.java | 11 ++++++++++- .../strings/InternalTermsStatsStringFacet.java | 11 ++++++++++- 8 files changed, 52 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java index a915b8d5917..4e54d212762 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java @@ -28,7 +28,13 @@ import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; /** * @author kimchy (shay.banon) @@ -105,6 +111,9 @@ public class InternalFullDateHistogramFacet extends InternalDateHistogramFacet { } @Override public double mean() { + if (totalCount == 0) { + return totalCount; + } return total / totalCount; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacet.java index dc025cade62..f7245085814 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacet.java @@ -118,6 +118,9 @@ public interface GeoDistanceFacet extends Facet, Iterable { * The mean of this facet interval. */ public double mean() { + if (totalCount == 0) { + return 0; + } return total / totalCount; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java index 6032f159b66..7c273b587fc 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java @@ -114,6 +114,9 @@ public class InternalStatisticalFacet implements StatisticalFacet, InternalFacet } @Override public double mean() { + if (count == 0) { + return 0; + } return total / count; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java index 7f4855cee3b..ec626773522 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java @@ -30,7 +30,13 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.termsstats.InternalTermsStatsFacet; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { @@ -128,6 +134,9 @@ public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { } @Override public double mean() { + if (totalCount == 0) { + return 0; + } return total / totalCount; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java index a54ace76358..12d245109db 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java @@ -30,7 +30,13 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.termsstats.InternalTermsStatsFacet; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; public class InternalTermsStatsLongFacet extends InternalTermsStatsFacet { @@ -127,6 +133,9 @@ public class InternalTermsStatsLongFacet extends InternalTermsStatsFacet { } @Override public double mean() { + if (totalCount == 0) { + return 0; + } return total / totalCount; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java index 4f6ab4ef1c3..262fb2b9398 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java @@ -30,7 +30,13 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.termsstats.InternalTermsStatsFacet; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { @@ -128,6 +134,9 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { } @Override public double mean() { + if (totalCount == 0) { + return 0; + } return total / totalCount; }