From 5e811e53829c6881d05f6762cf5f4532d42815fa Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 31 Jan 2013 23:49:32 +0100 Subject: [PATCH] Another small TopChildrenQuery cleanup. --- .../index/search/child/TopChildrenQuery.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java index 16cd13f2db0..5445d9008d9 100644 --- a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java @@ -51,21 +51,19 @@ import java.util.*; public class TopChildrenQuery extends Query implements SearchContext.Rewrite { private final SearchContext searchContext; - private final Query query; + private final Query childQuery; private final String parentType; private final String childType; private final ScoreType scoreType; private final int factor; private final int incrementalFactor; - // Need to know if this query is properly used, otherwise the results are unexpected for example in the count api - private boolean properlyInvoked = false; private ExtTHashMap parentDocs; // Note, the query is expected to already be filtered to only child type docs - public TopChildrenQuery(SearchContext searchContext, Query query, String childType, String parentType, ScoreType scoreType, int factor, int incrementalFactor) { + public TopChildrenQuery(SearchContext searchContext, Query childQuery, String childType, String parentType, ScoreType scoreType, int factor, int incrementalFactor) { this.searchContext = searchContext; - this.query = query; + this.childQuery = childQuery; this.childType = childType; this.parentType = parentType; this.scoreType = scoreType; @@ -75,19 +73,17 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { private TopChildrenQuery(TopChildrenQuery existing, Query rewrittenChildQuery) { this.searchContext = existing.searchContext; - this.query = rewrittenChildQuery; + this.childQuery = rewrittenChildQuery; this.parentType = existing.parentType; this.childType = existing.childType; this.scoreType = existing.scoreType; this.factor = existing.factor; this.incrementalFactor = existing.incrementalFactor; this.parentDocs = existing.parentDocs; - this.properlyInvoked = existing.properlyInvoked; } @Override public void contextRewrite(SearchContext searchContext) throws Exception { - properlyInvoked = true; this.parentDocs = CacheRecycler.popHashMap(); searchContext.idCache().refresh(searchContext.searcher().getTopReaderContext().leaves()); @@ -99,7 +95,7 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { numChildDocs *= factor; while (true) { parentDocs.clear(); - TopDocs topChildDocs = searchContext.searcher().search(query, numChildDocs); + TopDocs topChildDocs = searchContext.searcher().search(childQuery, numChildDocs); parentHitsResolved = resolveParentDocuments(topChildDocs, searchContext); // check if we found enough docs, if so, break @@ -201,8 +197,8 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { @Override public Query rewrite(IndexReader reader) throws IOException { - Query rewrittenChildQuery = query.rewrite(reader); - if (rewrittenChildQuery == query) { + Query rewrittenChildQuery = childQuery.rewrite(reader); + if (rewrittenChildQuery == childQuery) { return this; } int index = searchContext.rewrites().indexOf(this); @@ -213,25 +209,21 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { @Override public void extractTerms(Set terms) { - query.extractTerms(terms); + childQuery.extractTerms(terms); } @Override public Weight createWeight(IndexSearcher searcher) throws IOException { - if (!properlyInvoked) { + if (parentDocs == null) { throw new ElasticSearchIllegalStateException("top_children query hasn't executed properly"); } - if (parentDocs != null) { - return new ParentWeight(searcher, query.createWeight(searcher)); - } else { - return query.createWeight(searcher); - } + return new ParentWeight(searcher, childQuery.createWeight(searcher)); } public String toString(String field) { StringBuilder sb = new StringBuilder(); - sb.append("score_child[").append(childType).append("/").append(parentType).append("](").append(query.toString(field)).append(')'); + sb.append("score_child[").append(childType).append("/").append(parentType).append("](").append(childQuery.toString(field)).append(')'); sb.append(ToStringUtils.boost(getBoost())); return sb.toString(); }