Improve resource handling in Parent/ChildQuery
This commit is contained in:
parent
821173b5cf
commit
aa654a26c9
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue