Index: lucene/src/java/org/apache/lucene/search/FieldComparator.java =================================================================== --- lucene/src/java/org/apache/lucene/search/FieldComparator.java (revision 991498) +++ lucene/src/java/org/apache/lucene/search/FieldComparator.java (working copy) @@ -731,10 +731,7 @@ @Override public int compare(int slot1, int slot2) { if (readerGen[slot1] == readerGen[slot2]) { - int cmp = ords[slot1] - ords[slot2]; - if (cmp != 0) { - return cmp; - } + return ords[slot1] - ords[slot2]; } final BytesRef val1 = values[slot1]; @@ -786,6 +783,7 @@ public void copy(int slot, int doc) { final int ord = (int) currentDocToOrd.get(doc); if (ord == 0) { + ords[slot] = 0; values[slot] = null; } else { ords[slot] = ord; @@ -813,22 +811,31 @@ bottomSlot = bottom; bottomValue = values[bottomSlot]; - if (bottomValue == null) { - // 0 ord is null for all segments - assert ords[bottomSlot] == 0; - bottomOrd = 0; + if (currentReaderGen == readerGen[bottomSlot]) { + bottomOrd = ords[bottomSlot]; bottomSameReader = true; } else { - final int index = binarySearch(tempBR, termsIndex, bottomValue); - if (index < 0) { - bottomOrd = -index - 2; - bottomSameReader = false; + if (bottomValue == null) { + // 0 ord is null for all segments + assert ords[bottomSlot] == 0; + bottomOrd = 0; + bottomSameReader = true; + readerGen[bottomSlot] = currentReaderGen; } else { - bottomOrd = index; - // exact value match - bottomSameReader = true; + final int index = binarySearch(tempBR, termsIndex, bottomValue); + if (index < 0) { + bottomOrd = -index - 2; + bottomSameReader = false; + } else { + bottomOrd = index; + // exact value match + bottomSameReader = true; + } } } + if (bottomSameReader) { + readerGen[bottomSlot] = currentReaderGen; + } } @Override