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);
|
||||
indexSearcher.search(childQuery, maxCollector);
|
||||
parentIds = maxCollector.parentIds;
|
||||
scores = maxCollector.scores;
|
||||
occurrences = null;
|
||||
Releasables.release(maxCollector.parentIdsIndex);
|
||||
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);
|
||||
indexSearcher.search(childQuery, sumCollector);
|
||||
parentIds = sumCollector.parentIds;
|
||||
scores = sumCollector.scores;
|
||||
occurrences = null;
|
||||
Releasables.release(sumCollector.parentIdsIndex);
|
||||
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);
|
||||
indexSearcher.search(childQuery, avgCollector);
|
||||
parentIds = avgCollector.parentIds;
|
||||
scores = avgCollector.scores;
|
||||
occurrences = avgCollector.occurrences;
|
||||
Releasables.release(avgCollector.parentIdsIndex);
|
||||
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,26 +124,32 @@ public class ParentQuery extends Query {
|
|||
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
||||
SearchContext searchContext = SearchContext.current();
|
||||
ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType);
|
||||
|
||||
final Query parentQuery;
|
||||
if (rewrittenParentQuery == null) {
|
||||
parentQuery = rewrittenParentQuery = searcher.rewrite(originalParentQuery);
|
||||
} else {
|
||||
assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader();
|
||||
parentQuery = rewrittenParentQuery;
|
||||
ChildWeight childWeight;
|
||||
boolean releaseCollectorResource = true;
|
||||
try {
|
||||
final Query parentQuery;
|
||||
if (rewrittenParentQuery == null) {
|
||||
parentQuery = rewrittenParentQuery = searcher.rewrite(originalParentQuery);
|
||||
} else {
|
||||
assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader();
|
||||
parentQuery = rewrittenParentQuery;
|
||||
}
|
||||
IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader());
|
||||
indexSearcher.setSimilarity(searcher.getSimilarity());
|
||||
indexSearcher.search(parentQuery, collector);
|
||||
FloatArray scores = collector.scores;
|
||||
BytesRefHash parentIds = collector.parentIds;
|
||||
if (parentIds.size() == 0) {
|
||||
return Queries.newMatchNoDocsQuery().createWeight(searcher);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader());
|
||||
indexSearcher.setSimilarity(searcher.getSimilarity());
|
||||
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);
|
||||
searchContext.addReleasable(childWeight);
|
||||
return childWeight;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue