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
|
@Override
|
||||||
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
||||||
SearchContext searchContext = SearchContext.current();
|
SearchContext searchContext = SearchContext.current();
|
||||||
|
|
||||||
final Query childQuery;
|
final Query childQuery;
|
||||||
if (rewrittenChildQuery == null) {
|
if (rewrittenChildQuery == null) {
|
||||||
childQuery = rewrittenChildQuery = searcher.rewrite(originalChildQuery);
|
childQuery = rewrittenChildQuery = searcher.rewrite(originalChildQuery);
|
||||||
|
@ -154,27 +153,36 @@ public class ChildrenQuery extends Query {
|
||||||
switch (scoreType) {
|
switch (scoreType) {
|
||||||
case MAX:
|
case MAX:
|
||||||
MaxCollector maxCollector = new MaxCollector(parentChildIndexFieldData, parentType, searchContext);
|
MaxCollector maxCollector = new MaxCollector(parentChildIndexFieldData, parentType, searchContext);
|
||||||
indexSearcher.search(childQuery, maxCollector);
|
try {
|
||||||
parentIds = maxCollector.parentIds;
|
indexSearcher.search(childQuery, maxCollector);
|
||||||
scores = maxCollector.scores;
|
parentIds = maxCollector.parentIds;
|
||||||
occurrences = null;
|
scores = maxCollector.scores;
|
||||||
Releasables.release(maxCollector.parentIdsIndex);
|
occurrences = null;
|
||||||
|
} finally {
|
||||||
|
Releasables.release(maxCollector.parentIdsIndex);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SUM:
|
case SUM:
|
||||||
SumCollector sumCollector = new SumCollector(parentChildIndexFieldData, parentType, searchContext);
|
SumCollector sumCollector = new SumCollector(parentChildIndexFieldData, parentType, searchContext);
|
||||||
indexSearcher.search(childQuery, sumCollector);
|
try {
|
||||||
parentIds = sumCollector.parentIds;
|
indexSearcher.search(childQuery, sumCollector);
|
||||||
scores = sumCollector.scores;
|
parentIds = sumCollector.parentIds;
|
||||||
occurrences = null;
|
scores = sumCollector.scores;
|
||||||
Releasables.release(sumCollector.parentIdsIndex);
|
occurrences = null;
|
||||||
|
} finally {
|
||||||
|
Releasables.release(sumCollector.parentIdsIndex);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case AVG:
|
case AVG:
|
||||||
AvgCollector avgCollector = new AvgCollector(parentChildIndexFieldData, parentType, searchContext);
|
AvgCollector avgCollector = new AvgCollector(parentChildIndexFieldData, parentType, searchContext);
|
||||||
indexSearcher.search(childQuery, avgCollector);
|
try {
|
||||||
parentIds = avgCollector.parentIds;
|
indexSearcher.search(childQuery, avgCollector);
|
||||||
scores = avgCollector.scores;
|
parentIds = avgCollector.parentIds;
|
||||||
occurrences = avgCollector.occurrences;
|
scores = avgCollector.scores;
|
||||||
Releasables.release(avgCollector.parentIdsIndex);
|
occurrences = avgCollector.occurrences;
|
||||||
|
} finally {
|
||||||
|
Releasables.release(avgCollector.parentIdsIndex);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Are we missing a score type here? -- " + scoreType);
|
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 {
|
public Weight createWeight(IndexSearcher searcher) throws IOException {
|
||||||
SearchContext searchContext = SearchContext.current();
|
SearchContext searchContext = SearchContext.current();
|
||||||
ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType);
|
ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType);
|
||||||
|
ChildWeight childWeight;
|
||||||
final Query parentQuery;
|
boolean releaseCollectorResource = true;
|
||||||
if (rewrittenParentQuery == null) {
|
try {
|
||||||
parentQuery = rewrittenParentQuery = searcher.rewrite(originalParentQuery);
|
final Query parentQuery;
|
||||||
} else {
|
if (rewrittenParentQuery == null) {
|
||||||
assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader();
|
parentQuery = rewrittenParentQuery = searcher.rewrite(originalParentQuery);
|
||||||
parentQuery = rewrittenParentQuery;
|
} 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);
|
searchContext.addReleasable(childWeight);
|
||||||
return childWeight;
|
return childWeight;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue