LUCENE-4410: fix test bug - return null DocIdSet if reader has no docs for the term

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1388404 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2012-09-21 10:32:50 +00:00
parent bcb88e741e
commit 8cb409e692
1 changed files with 53 additions and 43 deletions

View File

@ -383,12 +383,13 @@ public class TestFilteredQuery extends LuceneTestCase {
} }
/* /*
* Test if the QueryFirst strategy calls the bits only if * Test if the QueryFirst strategy calls the bits only if the document has
* the document has been matched by the query and not otherwise * been matched by the query and not otherwise
*/ */
public void testQueryFirstFilterStrategy() throws IOException { public void testQueryFirstFilterStrategy() throws IOException {
Directory directory = newDirectory(); Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter (random(), directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
int numDocs = atLeast(50); int numDocs = atLeast(50);
int totalDocsWithZero = 0; int totalDocsWithZero = 0;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
@ -397,63 +398,69 @@ public class TestFilteredQuery extends LuceneTestCase {
if (num == 0) { if (num == 0) {
totalDocsWithZero++; totalDocsWithZero++;
} }
doc.add (newTextField("field", ""+num, Field.Store.YES)); doc.add(newTextField("field", "" + num, Field.Store.YES));
writer.addDocument (doc); writer.addDocument(doc);
} }
IndexReader reader = writer.getReader(); IndexReader reader = writer.getReader();
writer.close (); writer.close();
IndexSearcher searcher = newSearcher(reader); IndexSearcher searcher = newSearcher(reader);
Query query = new FilteredQuery(new TermQuery(new Term("field", "0")), new Filter() { Query query = new FilteredQuery(new TermQuery(new Term("field", "0")),
@Override new Filter() {
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs)
throws IOException {
final boolean nullBitset = random().nextInt(10) == 5;
final AtomicReader reader = context.reader();
DocsEnum termDocsEnum = reader.termDocsEnum(new Term("field", "0"));
final BitSet bitSet = new BitSet(reader.maxDoc());
if (termDocsEnum != null) {
int d;
while((d = termDocsEnum.nextDoc()) != DocsEnum.NO_MORE_DOCS) {
bitSet.set(d, true);
}
}
return new DocIdSet() {
@Override @Override
public Bits bits() throws IOException { public DocIdSet getDocIdSet(AtomicReaderContext context,
if (nullBitset) { Bits acceptDocs) throws IOException {
return null; final boolean nullBitset = random().nextInt(10) == 5;
final AtomicReader reader = context.reader();
DocsEnum termDocsEnum = reader.termDocsEnum(new Term("field", "0"));
if (termDocsEnum == null) {
return null; // no docs -- return null
} }
return new Bits() { final BitSet bitSet = new BitSet(reader.maxDoc());
int d;
while ((d = termDocsEnum.nextDoc()) != DocsEnum.NO_MORE_DOCS) {
bitSet.set(d, true);
}
return new DocIdSet() {
@Override @Override
public boolean get(int index) { public Bits bits() throws IOException {
assertTrue("filter was called for a non-matching doc", bitSet.get(index)); if (nullBitset) {
return bitSet.get(index); return null;
}
return new Bits() {
@Override
public boolean get(int index) {
assertTrue("filter was called for a non-matching doc",
bitSet.get(index));
return bitSet.get(index);
}
@Override
public int length() {
return bitSet.length();
}
};
} }
@Override @Override
public int length() { public DocIdSetIterator iterator() throws IOException {
return bitSet.length(); assertTrue(
"iterator should not be called if bitset is present",
nullBitset);
return reader.termDocsEnum(new Term("field", "0"));
} }
}; };
} }
@Override }, FilteredQuery.QUERY_FIRST_FILTER_STRATEGY);
public DocIdSetIterator iterator() throws IOException {
assertTrue("iterator should not be called if bitset is present", nullBitset);
return reader.termDocsEnum(new Term("field", "0"));
}
};
}
}, FilteredQuery.QUERY_FIRST_FILTER_STRATEGY);
TopDocs search = searcher.search(query, 10); TopDocs search = searcher.search(query, 10);
assertEquals(totalDocsWithZero, search.totalHits); assertEquals(totalDocsWithZero, search.totalHits);
IOUtils.close(reader, writer, directory); IOUtils.close(reader, writer, directory);
} }
/* /*
@ -491,6 +498,9 @@ public class TestFilteredQuery extends LuceneTestCase {
@Override @Override
public DocIdSetIterator iterator() throws IOException { public DocIdSetIterator iterator() throws IOException {
final DocsEnum termDocsEnum = context.reader().termDocsEnum(new Term("field", "0")); final DocsEnum termDocsEnum = context.reader().termDocsEnum(new Term("field", "0"));
if (termDocsEnum == null) {
return null;
}
return new DocIdSetIterator() { return new DocIdSetIterator() {
boolean nextCalled; boolean nextCalled;
boolean advanceCalled; boolean advanceCalled;