Improve resource handling in Parent/ChildQuery

This commit is contained in:
Simon Willnauer 2014-03-14 14:36:33 +01:00
parent 821173b5cf
commit aa654a26c9
2 changed files with 49 additions and 35 deletions

View File

@ -137,7 +137,6 @@ public class ChildrenQuery extends Query {
@Override
public Weight createWeight(IndexSearcher searcher) throws IOException {
SearchContext searchContext = SearchContext.current();
final Query childQuery;
if (rewrittenChildQuery == null) {
childQuery = rewrittenChildQuery = searcher.rewrite(originalChildQuery);
@ -154,27 +153,36 @@ public class ChildrenQuery extends Query {
switch (scoreType) {
case MAX:
MaxCollector maxCollector = new MaxCollector(parentChildIndexFieldData, parentType, searchContext);
try {
indexSearcher.search(childQuery, maxCollector);
parentIds = maxCollector.parentIds;
scores = maxCollector.scores;
occurrences = null;
} finally {
Releasables.release(maxCollector.parentIdsIndex);
}
break;
case SUM:
SumCollector sumCollector = new SumCollector(parentChildIndexFieldData, parentType, searchContext);
try {
indexSearcher.search(childQuery, sumCollector);
parentIds = sumCollector.parentIds;
scores = sumCollector.scores;
occurrences = null;
} finally {
Releasables.release(sumCollector.parentIdsIndex);
}
break;
case AVG:
AvgCollector avgCollector = new AvgCollector(parentChildIndexFieldData, parentType, searchContext);
try {
indexSearcher.search(childQuery, avgCollector);
parentIds = avgCollector.parentIds;
scores = avgCollector.scores;
occurrences = avgCollector.occurrences;
} finally {
Releasables.release(avgCollector.parentIdsIndex);
}
break;
default:
throw new RuntimeException("Are we missing a score type here? -- " + scoreType);

View File

@ -124,7 +124,9 @@ public class ParentQuery extends Query {
public Weight createWeight(IndexSearcher searcher) throws IOException {
SearchContext searchContext = SearchContext.current();
ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType);
ChildWeight childWeight;
boolean releaseCollectorResource = true;
try {
final Query parentQuery;
if (rewrittenParentQuery == null) {
parentQuery = rewrittenParentQuery = searcher.rewrite(originalParentQuery);
@ -137,13 +139,17 @@ public class ParentQuery extends Query {
indexSearcher.search(parentQuery, collector);
FloatArray scores = collector.scores;
BytesRefHash parentIds = collector.parentIds;
if (parentIds.size() == 0) {
Releasables.release(parentIds, scores);
return Queries.newMatchNoDocsQuery().createWeight(searcher);
}
ChildWeight childWeight = new ChildWeight(searchContext, parentQuery.createWeight(searcher), childrenFilter, parentIds, scores);
childWeight = new ChildWeight(searchContext, parentQuery.createWeight(searcher), childrenFilter, parentIds, scores);
releaseCollectorResource = false;
} finally {
if (releaseCollectorResource) {
// either if we run into an exception or if we return early
Releasables.release(collector.parentIds, collector.scores);
}
}
searchContext.addReleasable(childWeight);
return childWeight;
}