cached termsSet used in dfs phase

This commit is contained in:
kimchy 2010-05-27 20:31:48 +03:00
parent 8cc2638be8
commit 477024e3be
1 changed files with 9 additions and 1 deletions

View File

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