Fix P/C assertions for rewrite reader

Inner queries must be rewritten as soon as a weight is pulled ie. must
be non-null.

Closes #5731
This commit is contained in:
Simon Willnauer 2014-04-08 14:20:34 +02:00
parent 5b6fd6de42
commit adc9a250ab
4 changed files with 60 additions and 61 deletions

View File

@ -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 {

View File

@ -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());

View File

@ -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 {

View File

@ -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);