From 9539661d40d5eb219f68e1298feddb9359b4a14d Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sun, 27 Jan 2013 13:45:38 +0100 Subject: [PATCH] move facet reduce from facet process to the actual facet this will simplify execution, and actually let the process just be a parser (rename will probably happen) --- .../controller/SearchPhaseController.java | 13 ++++---- .../org/elasticsearch/search/facet/Facet.java | 2 -- .../search/facet/FacetProcessor.java | 3 -- .../search/facet/InternalFacet.java | 3 ++ .../DateHistogramFacetProcessor.java | 8 ----- .../InternalCountDateHistogramFacet.java | 2 +- .../InternalDateHistogramFacet.java | 5 --- .../InternalFullDateHistogramFacet.java | 2 +- .../facet/filter/FilterFacetProcessor.java | 14 --------- .../facet/filter/InternalFilterFacet.java | 13 ++++++++ .../GeoDistanceFacetProcessor.java | 27 ---------------- .../geodistance/InternalGeoDistanceFacet.java | 25 +++++++++++++++ .../histogram/HistogramFacetProcessor.java | 8 ----- .../histogram/InternalHistogramFacet.java | 5 --- .../InternalBoundedCountHistogramFacet.java | 2 +- .../InternalBoundedFullHistogramFacet.java | 2 +- .../InternalCountHistogramFacet.java | 2 +- .../unbounded/InternalFullHistogramFacet.java | 2 +- .../facet/query/InternalQueryFacet.java | 15 +++++++++ .../facet/query/QueryFacetProcessor.java | 16 ---------- .../facet/range/InternalRangeFacet.java | 29 +++++++++++++++++ .../facet/range/RangeFacetProcessor.java | 30 ------------------ .../statistical/InternalStatisticalFacet.java | 31 +++++++++++++++++++ .../StatisticalFacetProcessor.java | 31 ------------------- .../facet/terms/InternalTermsFacet.java | 5 --- .../facet/terms/TermsFacetProcessor.java | 7 ----- .../doubles/InternalDoubleTermsFacet.java | 2 +- .../terms/longs/InternalLongTermsFacet.java | 2 +- .../strings/InternalStringTermsFacet.java | 2 +- .../termsstats/InternalTermsStatsFacet.java | 5 --- .../termsstats/TermsStatsFacetProcessor.java | 8 ----- .../InternalTermsStatsDoubleFacet.java | 2 +- .../longs/InternalTermsStatsLongFacet.java | 2 +- .../InternalTermsStatsStringFacet.java | 2 +- 34 files changed, 134 insertions(+), 193 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java b/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java index 6363e7f413b..0f8ed1cdad3 100644 --- a/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java +++ b/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java @@ -39,7 +39,7 @@ import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.search.dfs.AggregatedDfs; import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.facet.Facet; -import org.elasticsearch.search.facet.FacetProcessors; +import org.elasticsearch.search.facet.InternalFacet; import org.elasticsearch.search.facet.InternalFacets; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.FetchSearchResultProvider; @@ -73,14 +73,11 @@ public class SearchPhaseController extends AbstractComponent { private static final ShardDoc[] EMPTY = new ShardDoc[0]; - private final FacetProcessors facetProcessors; - private final boolean optimizeSingleShard; @Inject - public SearchPhaseController(Settings settings, FacetProcessors facetProcessors) { + public SearchPhaseController(Settings settings) { super(settings); - this.facetProcessors = facetProcessors; this.optimizeSingleShard = componentSettings.getAsBoolean("optimize_single_shard", true); } @@ -317,8 +314,10 @@ public class SearchPhaseController extends AbstractComponent { } } } - Facet aggregatedFacet = facetProcessors.processor(facet.type()).reduce(facet.name(), namedFacets); - aggregatedFacets.add(aggregatedFacet); + if (!namedFacets.isEmpty()) { + Facet aggregatedFacet = ((InternalFacet) namedFacets.get(0)).reduce(namedFacets); + aggregatedFacets.add(aggregatedFacet); + } } facets = new InternalFacets(aggregatedFacets); } diff --git a/src/main/java/org/elasticsearch/search/facet/Facet.java b/src/main/java/org/elasticsearch/search/facet/Facet.java index ee2e605b701..a1fa2d79282 100644 --- a/src/main/java/org/elasticsearch/search/facet/Facet.java +++ b/src/main/java/org/elasticsearch/search/facet/Facet.java @@ -21,8 +21,6 @@ package org.elasticsearch.search.facet; /** * A search facet. - * - * */ public interface Facet { diff --git a/src/main/java/org/elasticsearch/search/facet/FacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/FacetProcessor.java index e44734617e9..7e057ea63eb 100644 --- a/src/main/java/org/elasticsearch/search/facet/FacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/FacetProcessor.java @@ -23,7 +23,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; /** * @@ -33,6 +32,4 @@ public interface FacetProcessor { String[] types(); FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException; - - Facet reduce(String name, List facets); } diff --git a/src/main/java/org/elasticsearch/search/facet/InternalFacet.java b/src/main/java/org/elasticsearch/search/facet/InternalFacet.java index d2b94355f52..01b3c05d870 100644 --- a/src/main/java/org/elasticsearch/search/facet/InternalFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/InternalFacet.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; import java.io.IOException; +import java.util.List; /** * @@ -34,6 +35,8 @@ public interface InternalFacet extends Facet, Streamable, ToXContent { String streamType(); + Facet reduce(List facets); + public static interface Stream { Facet readFacet(String type, StreamInput in) throws IOException; } diff --git a/src/main/java/org/elasticsearch/search/facet/datehistogram/DateHistogramFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/datehistogram/DateHistogramFacetProcessor.java index ce8ca02accc..a9ddf71915b 100644 --- a/src/main/java/org/elasticsearch/search/facet/datehistogram/DateHistogramFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/datehistogram/DateHistogramFacetProcessor.java @@ -31,7 +31,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.script.SearchScript; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -42,7 +41,6 @@ import org.joda.time.DateTimeZone; import org.joda.time.chrono.ISOChronology; import java.io.IOException; -import java.util.List; import java.util.Map; /** @@ -212,12 +210,6 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa } } - @Override - public Facet reduce(String name, List facets) { - InternalDateHistogramFacet first = (InternalDateHistogramFacet) facets.get(0); - return first.reduce(name, facets); - } - static interface DateFieldParser { DateTimeField parse(Chronology chronology); diff --git a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalCountDateHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalCountDateHistogramFacet.java index 6a4ba6f16c9..4254d7f684a 100644 --- a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalCountDateHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalCountDateHistogramFacet.java @@ -218,7 +218,7 @@ public class InternalCountDateHistogramFacet extends InternalDateHistogramFacet } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { return facets.get(0); } diff --git a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalDateHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalDateHistogramFacet.java index 0e5287e59bf..66a2606d944 100644 --- a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalDateHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalDateHistogramFacet.java @@ -19,11 +19,8 @@ package org.elasticsearch.search.facet.datehistogram; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; -import java.util.List; - /** * */ @@ -33,6 +30,4 @@ public abstract class InternalDateHistogramFacet implements DateHistogramFacet, InternalCountDateHistogramFacet.registerStreams(); InternalFullDateHistogramFacet.registerStreams(); } - - public abstract Facet reduce(String name, List facets); } diff --git a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java index 00ea799b6c3..9a8159e5f04 100644 --- a/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/datehistogram/InternalFullDateHistogramFacet.java @@ -214,7 +214,7 @@ public class InternalFullDateHistogramFacet extends InternalDateHistogramFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { // we need to sort it InternalFullDateHistogramFacet internalFacet = (InternalFullDateHistogramFacet) facets.get(0); diff --git a/src/main/java/org/elasticsearch/search/facet/filter/FilterFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/filter/FilterFacetProcessor.java index 3f64a17aff5..16bbeb8f493 100644 --- a/src/main/java/org/elasticsearch/search/facet/filter/FilterFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/filter/FilterFacetProcessor.java @@ -24,13 +24,11 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetProcessor; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; /** * @@ -53,16 +51,4 @@ public class FilterFacetProcessor extends AbstractComponent implements FacetProc Filter facetFilter = context.queryParserService().parseInnerFilter(parser); return new FilterFacetCollector(facetName, facetFilter, context.filterCache()); } - - @Override - public Facet reduce(String name, List facets) { - if (facets.size() == 1) { - return facets.get(0); - } - int count = 0; - for (Facet facet : facets) { - count += ((FilterFacet) facet).count(); - } - return new InternalFilterFacet(name, count); - } } diff --git a/src/main/java/org/elasticsearch/search/facet/filter/InternalFilterFacet.java b/src/main/java/org/elasticsearch/search/facet/filter/InternalFilterFacet.java index fadb4400895..59789d482a8 100644 --- a/src/main/java/org/elasticsearch/search/facet/filter/InternalFilterFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/filter/InternalFilterFacet.java @@ -27,6 +27,7 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import java.io.IOException; +import java.util.List; /** * @@ -100,6 +101,18 @@ public class InternalFilterFacet implements FilterFacet, InternalFacet { return count; } + @Override + public Facet reduce(List facets) { + if (facets.size() == 1) { + return facets.get(0); + } + int count = 0; + for (Facet facet : facets) { + count += ((FilterFacet) facet).count(); + } + return new InternalFilterFacet(name, count); + } + static final class Fields { static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); static final XContentBuilderString COUNT = new XContentBuilderString("count"); diff --git a/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetProcessor.java index a3a590bea60..3e444243d3e 100644 --- a/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetProcessor.java @@ -33,7 +33,6 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -187,30 +186,4 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face return new GeoDistanceFacetCollector(facetName, keyIndexFieldData, point.lat(), point.lon(), unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]), context); } - - @Override - public Facet reduce(String name, List facets) { - InternalGeoDistanceFacet agg = null; - for (Facet facet : facets) { - InternalGeoDistanceFacet geoDistanceFacet = (InternalGeoDistanceFacet) facet; - if (agg == null) { - agg = geoDistanceFacet; - } else { - for (int i = 0; i < geoDistanceFacet.entries.length; i++) { - GeoDistanceFacet.Entry aggEntry = agg.entries[i]; - GeoDistanceFacet.Entry currentEntry = geoDistanceFacet.entries[i]; - aggEntry.count += currentEntry.count; - aggEntry.totalCount += currentEntry.totalCount; - aggEntry.total += currentEntry.total; - if (currentEntry.min < aggEntry.min) { - aggEntry.min = currentEntry.min; - } - if (currentEntry.max > aggEntry.max) { - aggEntry.max = currentEntry.max; - } - } - } - } - return agg; - } } diff --git a/src/main/java/org/elasticsearch/search/facet/geodistance/InternalGeoDistanceFacet.java b/src/main/java/org/elasticsearch/search/facet/geodistance/InternalGeoDistanceFacet.java index b08420f3f72..33fbe2d2f3f 100644 --- a/src/main/java/org/elasticsearch/search/facet/geodistance/InternalGeoDistanceFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/geodistance/InternalGeoDistanceFacet.java @@ -101,6 +101,31 @@ public class InternalGeoDistanceFacet implements GeoDistanceFacet, InternalFacet return entries().iterator(); } + @Override + public Facet reduce(List facets) { + if (facets.size() == 1) { + return facets.get(0); + } + InternalGeoDistanceFacet agg = (InternalGeoDistanceFacet) facets.get(0); + for (int i = 1; i < facets.size(); i++) { + InternalGeoDistanceFacet geoDistanceFacet = (InternalGeoDistanceFacet) facets.get(i); + for (int j = 0; j < geoDistanceFacet.entries.length; j++) { + GeoDistanceFacet.Entry aggEntry = agg.entries[j]; + GeoDistanceFacet.Entry currentEntry = geoDistanceFacet.entries[j]; + aggEntry.count += currentEntry.count; + aggEntry.totalCount += currentEntry.totalCount; + aggEntry.total += currentEntry.total; + if (currentEntry.min < aggEntry.min) { + aggEntry.min = currentEntry.min; + } + if (currentEntry.max > aggEntry.max) { + aggEntry.max = currentEntry.max; + } + } + } + return agg; + } + public static InternalGeoDistanceFacet readGeoDistanceFacet(StreamInput in) throws IOException { InternalGeoDistanceFacet facet = new InternalGeoDistanceFacet(); facet.readFrom(in); diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/HistogramFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/histogram/HistogramFacetProcessor.java index 56e8cb5475f..4caee82a0cf 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/HistogramFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/HistogramFacetProcessor.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -37,7 +36,6 @@ import org.elasticsearch.search.facet.histogram.unbounded.*; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; import java.util.Map; /** @@ -155,10 +153,4 @@ public class HistogramFacetProcessor extends AbstractComponent implements FacetP return new ValueHistogramFacetCollector(facetName, keyIndexFieldData, valueIndexFieldData, interval, comparatorType, context); } } - - @Override - public Facet reduce(String name, List facets) { - InternalHistogramFacet first = (InternalHistogramFacet) facets.get(0); - return first.reduce(name, facets); - } } diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/InternalHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/histogram/InternalHistogramFacet.java index f822825407c..0ec6a8e2ae9 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/InternalHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/InternalHistogramFacet.java @@ -19,15 +19,12 @@ package org.elasticsearch.search.facet.histogram; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import org.elasticsearch.search.facet.histogram.bounded.InternalBoundedCountHistogramFacet; import org.elasticsearch.search.facet.histogram.bounded.InternalBoundedFullHistogramFacet; import org.elasticsearch.search.facet.histogram.unbounded.InternalCountHistogramFacet; import org.elasticsearch.search.facet.histogram.unbounded.InternalFullHistogramFacet; -import java.util.List; - /** * */ @@ -39,6 +36,4 @@ public abstract class InternalHistogramFacet implements HistogramFacet, Internal InternalBoundedCountHistogramFacet.registerStreams(); InternalBoundedFullHistogramFacet.registerStreams(); } - - public abstract Facet reduce(String name, List facets); } diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedCountHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedCountHistogramFacet.java index 207b1a04f7c..95e1869cc05 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedCountHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedCountHistogramFacet.java @@ -221,7 +221,7 @@ public class InternalBoundedCountHistogramFacet extends InternalHistogramFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { InternalBoundedCountHistogramFacet firstHistoFacet = (InternalBoundedCountHistogramFacet) facets.get(0); if (comparatorType != ComparatorType.KEY) { diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedFullHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedFullHistogramFacet.java index b9da0d8e6ac..e949ef4ff11 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedFullHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/bounded/InternalBoundedFullHistogramFacet.java @@ -227,7 +227,7 @@ public class InternalBoundedFullHistogramFacet extends InternalHistogramFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { // we need to sort it InternalBoundedFullHistogramFacet internalFacet = (InternalBoundedFullHistogramFacet) facets.get(0); diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalCountHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalCountHistogramFacet.java index 42a5e2c4f51..7e5c746eb91 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalCountHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalCountHistogramFacet.java @@ -220,7 +220,7 @@ public class InternalCountHistogramFacet extends InternalHistogramFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { return facets.get(0); } diff --git a/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalFullHistogramFacet.java b/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalFullHistogramFacet.java index b655e90b1cb..939da313fbf 100644 --- a/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalFullHistogramFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/histogram/unbounded/InternalFullHistogramFacet.java @@ -213,7 +213,7 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { // we need to sort it InternalFullHistogramFacet internalFacet = (InternalFullHistogramFacet) facets.get(0); diff --git a/src/main/java/org/elasticsearch/search/facet/query/InternalQueryFacet.java b/src/main/java/org/elasticsearch/search/facet/query/InternalQueryFacet.java index 4fcc10ed1f0..8e776a910f9 100644 --- a/src/main/java/org/elasticsearch/search/facet/query/InternalQueryFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/query/InternalQueryFacet.java @@ -27,6 +27,7 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import java.io.IOException; +import java.util.List; /** * @@ -100,6 +101,20 @@ public class InternalQueryFacet implements QueryFacet, InternalFacet { return count; } + @Override + public Facet reduce(List facets) { + if (facets.size() == 1) { + return facets.get(0); + } + int count = 0; + for (Facet facet : facets) { + if (facet.name().equals(name)) { + count += ((QueryFacet) facet).count(); + } + } + return new InternalQueryFacet(name, count); + } + static final class Fields { static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); static final XContentBuilderString COUNT = new XContentBuilderString("count"); diff --git a/src/main/java/org/elasticsearch/search/facet/query/QueryFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/query/QueryFacetProcessor.java index 170f060fb45..0b38476dbf2 100644 --- a/src/main/java/org/elasticsearch/search/facet/query/QueryFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/query/QueryFacetProcessor.java @@ -24,13 +24,11 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetProcessor; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; /** * @@ -53,18 +51,4 @@ public class QueryFacetProcessor extends AbstractComponent implements FacetProce Query facetQuery = context.queryParserService().parse(parser).query(); return new QueryFacetCollector(facetName, facetQuery, context.filterCache()); } - - @Override - public Facet reduce(String name, List facets) { - if (facets.size() == 1) { - return facets.get(0); - } - int count = 0; - for (Facet facet : facets) { - if (facet.name().equals(name)) { - count += ((QueryFacet) facet).count(); - } - } - return new InternalQueryFacet(name, count); - } } diff --git a/src/main/java/org/elasticsearch/search/facet/range/InternalRangeFacet.java b/src/main/java/org/elasticsearch/search/facet/range/InternalRangeFacet.java index 6aab1e265c4..444caa69157 100644 --- a/src/main/java/org/elasticsearch/search/facet/range/InternalRangeFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/range/InternalRangeFacet.java @@ -101,6 +101,35 @@ public class InternalRangeFacet implements RangeFacet, InternalFacet { return entries().iterator(); } + @Override + public Facet reduce(List facets) { + if (facets.size() == 1) { + return facets.get(0); + } + InternalRangeFacet agg = null; + for (Facet facet : facets) { + InternalRangeFacet geoDistanceFacet = (InternalRangeFacet) facet; + if (agg == null) { + agg = geoDistanceFacet; + } else { + for (int i = 0; i < geoDistanceFacet.entries.length; i++) { + RangeFacet.Entry aggEntry = agg.entries[i]; + RangeFacet.Entry currentEntry = geoDistanceFacet.entries[i]; + aggEntry.count += currentEntry.count; + aggEntry.totalCount += currentEntry.totalCount; + aggEntry.total += currentEntry.total; + if (currentEntry.min < aggEntry.min) { + aggEntry.min = currentEntry.min; + } + if (currentEntry.max > aggEntry.max) { + aggEntry.max = currentEntry.max; + } + } + } + } + return agg; + } + public static InternalRangeFacet readRangeFacet(StreamInput in) throws IOException { InternalRangeFacet facet = new InternalRangeFacet(); facet.readFrom(in); diff --git a/src/main/java/org/elasticsearch/search/facet/range/RangeFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/range/RangeFacetProcessor.java index cb83479afb9..f885c5b7f86 100644 --- a/src/main/java/org/elasticsearch/search/facet/range/RangeFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/range/RangeFacetProcessor.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -156,33 +155,4 @@ public class RangeFacetProcessor extends AbstractComponent implements FacetProce return new KeyValueRangeFacetCollector(facetName, keyIndexFieldData, valueIndexFieldData, rangeEntries, context); } } - - @Override - public Facet reduce(String name, List facets) { - if (facets.size() == 1) { - return facets.get(0); - } - InternalRangeFacet agg = null; - for (Facet facet : facets) { - InternalRangeFacet geoDistanceFacet = (InternalRangeFacet) facet; - if (agg == null) { - agg = geoDistanceFacet; - } else { - for (int i = 0; i < geoDistanceFacet.entries.length; i++) { - RangeFacet.Entry aggEntry = agg.entries[i]; - RangeFacet.Entry currentEntry = geoDistanceFacet.entries[i]; - aggEntry.count += currentEntry.count; - aggEntry.totalCount += currentEntry.totalCount; - aggEntry.total += currentEntry.total; - if (currentEntry.min < aggEntry.min) { - aggEntry.min = currentEntry.min; - } - if (currentEntry.max > aggEntry.max) { - aggEntry.max = currentEntry.max; - } - } - } - } - return agg; - } } diff --git a/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java b/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java index 28f51d73c02..520242ad3ac 100644 --- a/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/statistical/InternalStatisticalFacet.java @@ -27,6 +27,7 @@ import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import java.io.IOException; +import java.util.List; /** * @@ -174,6 +175,36 @@ public class InternalStatisticalFacet implements StatisticalFacet, InternalFacet return stdDeviation(); } + @Override + public Facet reduce(List facets) { + if (facets.size() == 1) { + return facets.get(0); + } + double min = Double.NaN; + double max = Double.NaN; + double total = 0; + double sumOfSquares = 0; + long count = 0; + + for (Facet facet : facets) { + if (!facet.name().equals(name)) { + continue; + } + InternalStatisticalFacet statsFacet = (InternalStatisticalFacet) facet; + if (statsFacet.min() < min || Double.isNaN(min)) { + min = statsFacet.min(); + } + if (statsFacet.max() > max || Double.isNaN(max)) { + max = statsFacet.max(); + } + total += statsFacet.total(); + sumOfSquares += statsFacet.sumOfSquares(); + count += statsFacet.count(); + } + + return new InternalStatisticalFacet(name, min, max, total, sumOfSquares, count); + } + static final class Fields { static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); static final XContentBuilderString COUNT = new XContentBuilderString("count"); diff --git a/src/main/java/org/elasticsearch/search/facet/statistical/StatisticalFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/statistical/StatisticalFacetProcessor.java index 98470cb7297..65ed0240a99 100644 --- a/src/main/java/org/elasticsearch/search/facet/statistical/StatisticalFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/statistical/StatisticalFacetProcessor.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -113,34 +112,4 @@ public class StatisticalFacetProcessor extends AbstractComponent implements Face return new ScriptStatisticalFacetCollector(facetName, scriptLang, script, params, context); } } - - @Override - public Facet reduce(String name, List facets) { - if (facets.size() == 1) { - return facets.get(0); - } - double min = Double.NaN; - double max = Double.NaN; - double total = 0; - double sumOfSquares = 0; - long count = 0; - - for (Facet facet : facets) { - if (!facet.name().equals(name)) { - continue; - } - InternalStatisticalFacet statsFacet = (InternalStatisticalFacet) facet; - if (statsFacet.min() < min || Double.isNaN(min)) { - min = statsFacet.min(); - } - if (statsFacet.max() > max || Double.isNaN(max)) { - max = statsFacet.max(); - } - total += statsFacet.total(); - sumOfSquares += statsFacet.sumOfSquares(); - count += statsFacet.count(); - } - - return new InternalStatisticalFacet(name, min, max, total, sumOfSquares, count); - } } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/InternalTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/InternalTermsFacet.java index d9bdea3704f..018e4767704 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/InternalTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/InternalTermsFacet.java @@ -19,14 +19,11 @@ package org.elasticsearch.search.facet.terms; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import org.elasticsearch.search.facet.terms.doubles.InternalDoubleTermsFacet; import org.elasticsearch.search.facet.terms.longs.InternalLongTermsFacet; import org.elasticsearch.search.facet.terms.strings.InternalStringTermsFacet; -import java.util.List; - /** * */ @@ -37,6 +34,4 @@ public abstract class InternalTermsFacet implements TermsFacet, InternalFacet { InternalLongTermsFacet.registerStream(); InternalDoubleTermsFacet.registerStream(); } - - public abstract Facet reduce(String name, List facets); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetProcessor.java index b19afcb3f8c..7a7e22f082f 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetProcessor.java @@ -31,7 +31,6 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.script.SearchScript; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -173,10 +172,4 @@ public class TermsFacetProcessor extends AbstractComponent implements FacetProce } } } - - @Override - public Facet reduce(String name, List facets) { - InternalTermsFacet first = (InternalTermsFacet) facets.get(0); - return first.reduce(name, facets); - } } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java index c7757132d27..7dbe22bd9d5 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java @@ -213,7 +213,7 @@ public class InternalDoubleTermsFacet extends InternalTermsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { return facets.get(0); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java index f3c8d18a564..a0d44d00dcf 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java @@ -213,7 +213,7 @@ public class InternalLongTermsFacet extends InternalTermsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { return facets.get(0); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java index cf5a19d99cd..c98003ca554 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java @@ -224,7 +224,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { return facets.get(0); } diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/InternalTermsStatsFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/InternalTermsStatsFacet.java index bab83d0c136..6c8f020f075 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/InternalTermsStatsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/InternalTermsStatsFacet.java @@ -19,14 +19,11 @@ package org.elasticsearch.search.facet.termsstats; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.InternalFacet; import org.elasticsearch.search.facet.termsstats.doubles.InternalTermsStatsDoubleFacet; import org.elasticsearch.search.facet.termsstats.longs.InternalTermsStatsLongFacet; import org.elasticsearch.search.facet.termsstats.strings.InternalTermsStatsStringFacet; -import java.util.List; - public abstract class InternalTermsStatsFacet implements TermsStatsFacet, InternalFacet { public static void registerStreams() { @@ -34,6 +31,4 @@ public abstract class InternalTermsStatsFacet implements TermsStatsFacet, Intern InternalTermsStatsLongFacet.registerStream(); InternalTermsStatsDoubleFacet.registerStream(); } - - public abstract Facet reduce(String name, List facets); } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacetProcessor.java b/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacetProcessor.java index fdd9026ff96..f92315f9f59 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacetProcessor.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacetProcessor.java @@ -27,7 +27,6 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.script.SearchScript; -import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetProcessor; @@ -37,7 +36,6 @@ import org.elasticsearch.search.facet.termsstats.strings.TermsStatsStringFacetCo import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; import java.util.Map; public class TermsStatsFacetProcessor extends AbstractComponent implements FacetProcessor { @@ -128,10 +126,4 @@ public class TermsStatsFacetProcessor extends AbstractComponent implements Facet } return new TermsStatsStringFacetCollector(facetName, keyIndexFieldData, valueIndexFieldData, valueScript, size, comparatorType, context); } - - @Override - public Facet reduce(String name, List facets) { - InternalTermsStatsFacet first = (InternalTermsStatsFacet) facets.get(0); - return first.reduce(name, facets); - } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java index 850d588b684..723d278ee86 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java @@ -240,7 +240,7 @@ public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { if (requiredSize == 0) { // we need to sort it here! diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java index 32fbb9623af..53561a5307d 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java @@ -240,7 +240,7 @@ public class InternalTermsStatsLongFacet extends InternalTermsStatsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { if (requiredSize == 0) { // we need to sort it here! diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java index b7375c3a300..d7bc9636073 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java @@ -245,7 +245,7 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { } @Override - public Facet reduce(String name, List facets) { + public Facet reduce(List facets) { if (facets.size() == 1) { if (requiredSize == 0) { // we need to sort it here!