diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/BytesRefOrdValComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/BytesRefOrdValComparator.java index 7325dd09cba..88c14112893 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/BytesRefOrdValComparator.java +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/BytesRefOrdValComparator.java @@ -228,7 +228,7 @@ public final class BytesRefOrdValComparator extends NestedWrappableComparator= 0) { - // value exists in the current segment - ord = docOrd << 2; + if (value == null) { + ord = Ordinals.MISSING_ORDINAL << 2; } else { - // value doesn't exist, use the ord between the previous and the next term - ord = ((-2 - docOrd) << 2) + 2; + final long docOrd = binarySearch(termsIndex, value); + if (docOrd >= Ordinals.MIN_ORDINAL) { + // value exists in the current segment + ord = docOrd << 2; + } else { + // value doesn't exist, use the ord between the previous and the next term + ord = ((-2 - docOrd) << 2) + 2; + } } + assert (ord & 1) == 0; return ord; } @@ -296,12 +301,8 @@ public final class BytesRefOrdValComparator extends NestedWrappableComparator setNextReader(AtomicReaderContext context) throws IOException { termsIndex = indexFieldData.load(context).getBytesValues(false); assert termsIndex.ordinals() != null; - if (missingValue == null) { - missingOrd = Ordinals.MISSING_ORDINAL; - } else { - missingOrd = ordInCurrentReader(termsIndex, missingValue); - assert consistentInsertedOrd(termsIndex, missingOrd, missingValue); - } + missingOrd = ordInCurrentReader(termsIndex, missingValue); + assert consistentInsertedOrd(termsIndex, missingOrd, missingValue); FieldComparator perSegComp = null; assert termsIndex.ordinals() != null; if (termsIndex.isMultiValued()) { @@ -332,14 +333,12 @@ public final class BytesRefOrdValComparator extends NestedWrappableComparator tie-break // this is why we multiply ords by 4 assert missingValue != null;