mirror of https://github.com/apache/lucene.git
LUCENE-8379: add TermQuery.getTermStates
This commit is contained in:
parent
4dc2008dc1
commit
fa3cd86de8
|
@ -130,6 +130,8 @@ API Changes:
|
|||
* LUCENE-8378: Add DocIdSetIterator.range static method to return an iterator
|
||||
matching a range of docids (Mike McCandless)
|
||||
|
||||
* LUCENE-8379: Add experimental TermQuery.getTermStates method (Mike McCandless)
|
||||
|
||||
Bug Fixes:
|
||||
|
||||
* LUCENE-8380: UTF8TaxonomyWriterCache inconsistency. (Ruslan Torobaev, Dawid Weiss)
|
||||
|
|
|
@ -217,6 +217,13 @@ public class TermQuery extends Query {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
/** Returns the {@link TermStates} passed to the constructor, or null if it was not passed.
|
||||
*
|
||||
* @lucene.experimental */
|
||||
public TermStates getTermStates() {
|
||||
return perReaderTermState;
|
||||
}
|
||||
|
||||
/** Returns true iff <code>o</code> is equal to this. */
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
|
|
|
@ -92,6 +92,36 @@ public class TestTermQuery extends LuceneTestCase {
|
|||
IOUtils.close(reader, w, dir);
|
||||
}
|
||||
|
||||
public void testGetTermStates() throws Exception {
|
||||
|
||||
// no term states:
|
||||
assertNull(new TermQuery(new Term("foo", "bar")).getTermStates());
|
||||
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE));
|
||||
// segment that contains the term
|
||||
Document doc = new Document();
|
||||
doc.add(new StringField("foo", "bar", Store.NO));
|
||||
w.addDocument(doc);
|
||||
w.getReader().close();
|
||||
// segment that does not contain the term
|
||||
doc = new Document();
|
||||
doc.add(new StringField("foo", "baz", Store.NO));
|
||||
w.addDocument(doc);
|
||||
w.getReader().close();
|
||||
// segment that does not contain the field
|
||||
w.addDocument(new Document());
|
||||
|
||||
DirectoryReader reader = w.getReader();
|
||||
FilterDirectoryReader noSeekReader = new NoSeekDirectoryReader(reader);
|
||||
IndexSearcher noSeekSearcher = new IndexSearcher(noSeekReader);
|
||||
Query query = new TermQuery(new Term("foo", "bar"));
|
||||
TermQuery queryWithContext = new TermQuery(new Term("foo", "bar"),
|
||||
TermStates.build(reader.getContext(), new Term("foo", "bar"), true));
|
||||
assertNotNull(queryWithContext.getTermStates());
|
||||
IOUtils.close(reader, w, dir);
|
||||
}
|
||||
|
||||
private static class NoSeekDirectoryReader extends FilterDirectoryReader {
|
||||
|
||||
public NoSeekDirectoryReader(DirectoryReader in) throws IOException {
|
||||
|
|
Loading…
Reference in New Issue