Another small TopChildrenQuery cleanup.
This commit is contained in:
parent
7ef65688cd
commit
5e811e5382
|
@ -51,21 +51,19 @@ import java.util.*;
|
||||||
public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
||||||
|
|
||||||
private final SearchContext searchContext;
|
private final SearchContext searchContext;
|
||||||
private final Query query;
|
private final Query childQuery;
|
||||||
private final String parentType;
|
private final String parentType;
|
||||||
private final String childType;
|
private final String childType;
|
||||||
private final ScoreType scoreType;
|
private final ScoreType scoreType;
|
||||||
private final int factor;
|
private final int factor;
|
||||||
private final int incrementalFactor;
|
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<Object, ParentDoc[]> parentDocs;
|
private ExtTHashMap<Object, ParentDoc[]> parentDocs;
|
||||||
|
|
||||||
// Note, the query is expected to already be filtered to only child type docs
|
// 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.searchContext = searchContext;
|
||||||
this.query = query;
|
this.childQuery = childQuery;
|
||||||
this.childType = childType;
|
this.childType = childType;
|
||||||
this.parentType = parentType;
|
this.parentType = parentType;
|
||||||
this.scoreType = scoreType;
|
this.scoreType = scoreType;
|
||||||
|
@ -75,19 +73,17 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
||||||
|
|
||||||
private TopChildrenQuery(TopChildrenQuery existing, Query rewrittenChildQuery) {
|
private TopChildrenQuery(TopChildrenQuery existing, Query rewrittenChildQuery) {
|
||||||
this.searchContext = existing.searchContext;
|
this.searchContext = existing.searchContext;
|
||||||
this.query = rewrittenChildQuery;
|
this.childQuery = rewrittenChildQuery;
|
||||||
this.parentType = existing.parentType;
|
this.parentType = existing.parentType;
|
||||||
this.childType = existing.childType;
|
this.childType = existing.childType;
|
||||||
this.scoreType = existing.scoreType;
|
this.scoreType = existing.scoreType;
|
||||||
this.factor = existing.factor;
|
this.factor = existing.factor;
|
||||||
this.incrementalFactor = existing.incrementalFactor;
|
this.incrementalFactor = existing.incrementalFactor;
|
||||||
this.parentDocs = existing.parentDocs;
|
this.parentDocs = existing.parentDocs;
|
||||||
this.properlyInvoked = existing.properlyInvoked;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextRewrite(SearchContext searchContext) throws Exception {
|
public void contextRewrite(SearchContext searchContext) throws Exception {
|
||||||
properlyInvoked = true;
|
|
||||||
this.parentDocs = CacheRecycler.popHashMap();
|
this.parentDocs = CacheRecycler.popHashMap();
|
||||||
searchContext.idCache().refresh(searchContext.searcher().getTopReaderContext().leaves());
|
searchContext.idCache().refresh(searchContext.searcher().getTopReaderContext().leaves());
|
||||||
|
|
||||||
|
@ -99,7 +95,7 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
||||||
numChildDocs *= factor;
|
numChildDocs *= factor;
|
||||||
while (true) {
|
while (true) {
|
||||||
parentDocs.clear();
|
parentDocs.clear();
|
||||||
TopDocs topChildDocs = searchContext.searcher().search(query, numChildDocs);
|
TopDocs topChildDocs = searchContext.searcher().search(childQuery, numChildDocs);
|
||||||
parentHitsResolved = resolveParentDocuments(topChildDocs, searchContext);
|
parentHitsResolved = resolveParentDocuments(topChildDocs, searchContext);
|
||||||
|
|
||||||
// check if we found enough docs, if so, break
|
// check if we found enough docs, if so, break
|
||||||
|
@ -201,8 +197,8 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Query rewrite(IndexReader reader) throws IOException {
|
public Query rewrite(IndexReader reader) throws IOException {
|
||||||
Query rewrittenChildQuery = query.rewrite(reader);
|
Query rewrittenChildQuery = childQuery.rewrite(reader);
|
||||||
if (rewrittenChildQuery == query) {
|
if (rewrittenChildQuery == childQuery) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
int index = searchContext.rewrites().indexOf(this);
|
int index = searchContext.rewrites().indexOf(this);
|
||||||
|
@ -213,25 +209,21 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extractTerms(Set<Term> terms) {
|
public void extractTerms(Set<Term> terms) {
|
||||||
query.extractTerms(terms);
|
childQuery.extractTerms(terms);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
||||||
if (!properlyInvoked) {
|
if (parentDocs == null) {
|
||||||
throw new ElasticSearchIllegalStateException("top_children query hasn't executed properly");
|
throw new ElasticSearchIllegalStateException("top_children query hasn't executed properly");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentDocs != null) {
|
return new ParentWeight(searcher, childQuery.createWeight(searcher));
|
||||||
return new ParentWeight(searcher, query.createWeight(searcher));
|
|
||||||
} else {
|
|
||||||
return query.createWeight(searcher);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(String field) {
|
public String toString(String field) {
|
||||||
StringBuilder sb = new StringBuilder();
|
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()));
|
sb.append(ToStringUtils.boost(getBoost()));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue