From aa654a26c9758d6dfb519c6085dd619247fc16b5 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 14 Mar 2014 14:36:33 +0100 Subject: [PATCH] Improve resource handling in Parent/ChildQuery --- .../index/search/child/ChildrenQuery.java | 40 ++++++++++------- .../index/search/child/ParentQuery.java | 44 +++++++++++-------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java index c7549b9cc42..53ee54eb8f4 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -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); diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java index 00ccf5e7230..f8069e288b6 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -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; }