diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java index ce6e46ef2e9..2475531573e 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java @@ -98,37 +98,41 @@ public class ChildrenConstantScoreQuery extends Query { @Override public Weight createWeight(IndexSearcher searcher) throws IOException { - SearchContext searchContext = SearchContext.current(); - BytesRefHash parentIds = new BytesRefHash(512, searchContext.bigArrays()); - ParentIdCollector collector = new ParentIdCollector(parentType, parentChildIndexFieldData, parentIds); - final Query childQuery; - if (rewrittenChildQuery == null) { - childQuery = rewrittenChildQuery = searcher.rewrite(originalChildQuery); - } else { + final SearchContext searchContext = SearchContext.current(); + final BytesRefHash parentIds = new BytesRefHash(512, searchContext.bigArrays()); + boolean releaseParentIds = true; + try { + final ParentIdCollector collector = new ParentIdCollector(parentType, parentChildIndexFieldData, parentIds); + assert rewrittenChildQuery != null; assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader(); - childQuery = rewrittenChildQuery; - } - IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); - indexSearcher.search(childQuery, collector); + final Query childQuery = rewrittenChildQuery; + IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); + indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.search(childQuery, collector); - long remaining = parentIds.size(); - if (remaining == 0) { - Releasables.release(parentIds); - return Queries.newMatchNoDocsQuery().createWeight(searcher); + long remaining = parentIds.size(); + if (remaining == 0) { + return Queries.newMatchNoDocsQuery().createWeight(searcher); + } + + Filter shortCircuitFilter = null; + if (remaining == 1) { + BytesRef id = parentIds.get(0, new BytesRef()); + shortCircuitFilter = new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))); + } else if (remaining <= shortCircuitParentDocSet) { + shortCircuitFilter = new ParentIdsFilter(parentType, nonNestedDocsFilter, parentIds); + } + final ParentWeight parentWeight = new ParentWeight(parentFilter, shortCircuitFilter, parentIds); + searchContext.addReleasable(parentWeight); + releaseParentIds = false; + return parentWeight; + } finally { + if (releaseParentIds) { + Releasables.release(parentIds); + } } - Filter shortCircuitFilter = null; - if (remaining == 1) { - BytesRef id = parentIds.get(0, new BytesRef()); - shortCircuitFilter = new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))); - } else if (remaining <= shortCircuitParentDocSet) { - shortCircuitFilter = new ParentIdsFilter(parentType, nonNestedDocsFilter, parentIds); - } - ParentWeight parentWeight = new ParentWeight(parentFilter, shortCircuitFilter, parentIds); - searchContext.addReleasable(parentWeight); - return parentWeight; } private final class ParentWeight extends Weight implements Releasable { 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 f1c28a8c5a8..ee01eea69c6 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -147,13 +147,10 @@ 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); - } else { - assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader(); - childQuery = rewrittenChildQuery; - } + assert rewrittenChildQuery != null; + assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader(); + final Query childQuery = rewrittenChildQuery; + IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); indexSearcher.setSimilarity(searcher.getSimilarity()); diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java index 11095773f2c..2c689309494 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java @@ -87,29 +87,31 @@ public class ParentConstantScoreQuery extends Query { @Override public Weight createWeight(IndexSearcher searcher) throws IOException { - SearchContext searchContext = SearchContext.current(); - BytesRefHash parentIds = new BytesRefHash(512, searchContext.bigArrays()); - ParentIdsCollector collector = new ParentIdsCollector(parentType, parentChildIndexFieldData, parentIds); - - final Query parentQuery; - if (rewrittenParentQuery != null) { - parentQuery = rewrittenParentQuery; - } else { + final SearchContext searchContext = SearchContext.current(); + final BytesRefHash parentIds = new BytesRefHash(512, searchContext.bigArrays()); + boolean releaseParentIds = true; + try { + ParentIdsCollector collector = new ParentIdsCollector(parentType, parentChildIndexFieldData, parentIds); + assert rewrittenParentQuery != null; assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader(); - parentQuery = rewrittenParentQuery = originalParentQuery.rewrite(searcher.getIndexReader()); - } - IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); - indexSearcher.search(parentQuery, collector); + final Query parentQuery = rewrittenParentQuery; + IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); + indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.search(parentQuery, collector); - if (parentIds.size() == 0) { - Releasables.release(parentIds); - return Queries.newMatchNoDocsQuery().createWeight(searcher); - } + if (parentIds.size() == 0) { + return Queries.newMatchNoDocsQuery().createWeight(searcher); + } - ChildrenWeight childrenWeight = new ChildrenWeight(childrenFilter, parentIds); - searchContext.addReleasable(childrenWeight); - return childrenWeight; + final ChildrenWeight childrenWeight = new ChildrenWeight(childrenFilter, parentIds); + searchContext.addReleasable(childrenWeight); + releaseParentIds = false; + return childrenWeight; + } finally { + if (releaseParentIds) { + Releasables.release(parentIds); + } + } } private final class ChildrenWeight extends Weight implements Releasable { 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 6750c7cde8f..9aa0455e889 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -133,17 +133,13 @@ public class ParentQuery extends Query { @Override public Weight createWeight(IndexSearcher searcher) throws IOException { SearchContext searchContext = SearchContext.current(); - ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType); + final ParentIdAndScoreCollector collector = new ParentIdAndScoreCollector(searchContext, parentChildIndexFieldData, parentType); 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; - } + assert rewrittenParentQuery != null; + assert rewriteIndexReader == searcher.getIndexReader() : "not equal, rewriteIndexReader=" + rewriteIndexReader + " searcher.getIndexReader()=" + searcher.getIndexReader(); + final Query parentQuery = rewrittenParentQuery; IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); indexSearcher.setSimilarity(searcher.getSimilarity()); indexSearcher.search(parentQuery, collector);