From d792d14926619cf36143fa44635dbc33d73e607b Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 15 Apr 2014 19:02:45 +0200 Subject: [PATCH] Instantiate facets/aggregations during the QUERY phase. In case of a DFS_QUERY_THEN_FETCH request, facets and aggregations are currently instantiated during the DFS phase while they only become useful during the QUERY phase. By instantiating during the QUERY phase instead, we can make better use of recycling since objects will have a shorter life out of the recyclers. Close #5821 --- .../org/elasticsearch/search/aggregations/Aggregator.java | 4 +--- .../java/org/elasticsearch/search/query/QueryPhase.java | 8 ++++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/aggregations/Aggregator.java b/src/main/java/org/elasticsearch/search/aggregations/Aggregator.java index 35e2b3a8025..c6cc465b801 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/Aggregator.java +++ b/src/main/java/org/elasticsearch/search/aggregations/Aggregator.java @@ -84,9 +84,7 @@ public abstract class Aggregator implements Releasable, ReaderContextAware { assert factories != null : "sub-factories provided to BucketAggregator must not be null, use AggragatorFactories.EMPTY instead"; this.factories = factories; this.subAggregators = factories.createSubAggregators(this, estimatedBucketsCount); - // TODO: change it to SEARCH_PHASE, but this would imply allocating the aggregators in the QUERY - // phase instead of DFS like it is done today - context.searchContext().addReleasable(this, Lifetime.CONTEXT); + context.searchContext().addReleasable(this, Lifetime.PHASE); } /** diff --git a/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/src/main/java/org/elasticsearch/search/query/QueryPhase.java index 017c2c69482..5f10cd361df 100644 --- a/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -89,11 +89,15 @@ public class QueryPhase implements SearchPhase { @Override public void preProcess(SearchContext context) { context.preProcess(); - facetPhase.preProcess(context); - aggregationPhase.preProcess(context); } public void execute(SearchContext searchContext) throws QueryPhaseExecutionException { + // Pre-process facets and aggregations as late as possible. In the case of a DFS_Q_T_F + // request, preProcess is called on the DFS phase phase, this is why we pre-process them + // here to make sure it happens during the QUERY phase + facetPhase.preProcess(searchContext); + aggregationPhase.preProcess(searchContext); + searchContext.queryResult().searchTimedOut(false); searchContext.searcher().inStage(ContextIndexSearcher.Stage.MAIN_QUERY);