diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java index f9b9ba4480c..25857182fd0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.Term; import org.elasticsearch.search.SearchParseElement; import org.elasticsearch.search.SearchPhase; import org.elasticsearch.search.internal.SearchContext; +import org.elasticsearch.util.ThreadLocals; import org.elasticsearch.util.collect.ImmutableMap; import org.elasticsearch.util.gnu.trove.THashSet; @@ -33,6 +34,12 @@ import java.util.Map; */ public class DfsPhase implements SearchPhase { + private static ThreadLocal>> cachedTermsSet = new ThreadLocal>>() { + @Override protected ThreadLocals.CleanableValue> initialValue() { + return new ThreadLocals.CleanableValue>(new THashSet()); + } + }; + @Override public Map parseElements() { return ImmutableMap.of(); } @@ -46,7 +53,8 @@ public class DfsPhase implements SearchPhase { context.updateRewriteQuery(context.searcher().rewrite(context.query())); } - THashSet termsSet = new THashSet(); + THashSet termsSet = cachedTermsSet.get().get(); + termsSet.clear(); context.query().extractTerms(termsSet); Term[] terms = termsSet.toArray(new Term[termsSet.size()]); int[] freqs = context.searcher().docFreqs(terms);