Skip to content

Handle empty terms index in TermsSliceQuery #43078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public boolean isCacheable(LeafReaderContext ctx) {
private DocIdSet build(LeafReader reader) throws IOException {
final DocIdSetBuilder builder = new DocIdSetBuilder(reader.maxDoc());
final Terms terms = reader.terms(getField());
if (terms == null) {
return DocIdSet.EMPTY;
}
final TermsEnum te = terms.iterator();
PostingsEnum docsEnum = null;
for (BytesRef term = te.next(); term != null; term = te.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import static org.hamcrest.Matchers.equalTo;

public class TermsSliceQueryTests extends ESTestCase {

public void testBasics() {
TermsSliceQuery query1 =
new TermsSliceQuery("field1", 1, 10);
Expand All @@ -62,6 +61,24 @@ public void testBasics() {
QueryUtils.checkUnequal(query1, query4);
}

public void testEmpty() throws Exception {
final Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random(), dir, new KeywordAnalyzer());
for (int i = 0; i < 10; ++i) {
Document doc = new Document();
doc.add(new StringField("field", Integer.toString(i), Field.Store.YES));
w.addDocument(doc);
}
final IndexReader reader = w.getReader();
final IndexSearcher searcher = newSearcher(reader);
TermsSliceQuery query =
new TermsSliceQuery("unknown", 1, 1);
assertThat(searcher.count(query), equalTo(0));
w.close();
reader.close();
dir.close();
}

public void testSearch() throws Exception {
final int numDocs = randomIntBetween(100, 200);
final Directory dir = newDirectory();
Expand Down