Skip to content

Apache Lucene open-source search software

LUCENE-5691: DocTermOrds lookupTerm is wrong in some cases

Robert Muira67d9a89a04e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 629aed1c55c..ad5e01dc4d6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -196,6 +196,10 @@ Bug fixes
* LUCENE-5682: NPE in QueryRescorer when Scorer is null
(Joel Bernstein, Mike McCandless)
+* LUCENE-5691: DocTermOrds lookupTerm(BytesRef) would return incorrect results
+ if the underlying TermsEnum supports ord() and the insertion point would
+ be at the end. (Robert Muir)
+
Test Framework
* LUCENE-5622: Fail tests if they print over the given limit of bytes to
diff --git a/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java b/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
index 2015e4f2922..417bed170fb 100644
--- a/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
+++ b/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
@@ -894,10 +894,15 @@ public class DocTermOrds {
@Override
public long lookupTerm(BytesRef key) {
try {
- if (te.seekCeil(key) == SeekStatus.FOUND) {
- return te.ord();
- } else {
- return -te.ord()-1;
+ switch (te.seekCeil(key)) {
+ case FOUND:
+ assert te.ord() >= 0;
+ return te.ord();
+ case NOT_FOUND:
+ assert te.ord() >= 0;
+ return -te.ord()-1;
+ default: /* END */
+ return -numTerms()-1;
}
} catch (IOException e) {
throw new RuntimeException(e);
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
index 785f5dfb4d8..9dabcf33a44 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
@@ -579,6 +579,16 @@ public class TestDocTermOrds extends LuceneTestCase {
termsEnum.seekExact(2);
assertEquals("world", termsEnum.term().utf8ToString());
assertEquals(2, termsEnum.ord());
+
+ // lookupTerm(BytesRef)
+ assertEquals(-1, dv.lookupTerm(new BytesRef("apple")));
+ assertEquals(0, dv.lookupTerm(new BytesRef("beer")));
+ assertEquals(-2, dv.lookupTerm(new BytesRef("car")));
+ assertEquals(1, dv.lookupTerm(new BytesRef("hello")));
+ assertEquals(-3, dv.lookupTerm(new BytesRef("matter")));
+ assertEquals(2, dv.lookupTerm(new BytesRef("world")));
+ assertEquals(-4, dv.lookupTerm(new BytesRef("zany")));
+
ireader.close();
directory.close();
}