Index: lucene/src/java/org/apache/lucene/search/FieldComparator.java =================================================================== --- lucene/src/java/org/apache/lucene/search/FieldComparator.java (revision 962913) +++ lucene/src/java/org/apache/lucene/search/FieldComparator.java (working copy) @@ -715,17 +715,14 @@ private int bottomSlot = -1; private int bottomOrd; + private int bottomGen; private BytesRef bottomValue; - private final boolean reversed; - private final int sortPos; private final BytesRef tempBR = new BytesRef(); public TermOrdValComparator(int numHits, String field, int sortPos, boolean reversed) { ords = new int[numHits]; values = new BytesRef[numHits]; readerGen = new int[numHits]; - this.sortPos = sortPos; - this.reversed = reversed; this.field = field; } @@ -756,7 +753,7 @@ assert bottomSlot != -1; int order = termsIndex.getOrd(doc); final int cmp = bottomOrd - order; - if (cmp != 0) { + if (cmp != 0 || bottomGen == currentReaderGen) { return cmp; } @@ -775,35 +772,6 @@ return bottomValue.compareTo(tempBR); } - private void convert(int slot) { - readerGen[slot] = currentReaderGen; - int index = 0; - BytesRef value = values[slot]; - if (value == null) { - // 0 ord is null for all segments - assert ords[slot] == 0; - return; - } - - if (sortPos == 0 && bottomSlot != -1 && bottomSlot != slot) { - // Since we are the primary sort, the entries in the - // queue are bounded by bottomOrd: - if (reversed) { - index = binarySearch(tempBR, termsIndex, value, bottomOrd, termsIndex.numOrd()-1); - } else { - index = binarySearch(tempBR, termsIndex, value, 0, bottomOrd); - } - } else { - // Full binary search - index = binarySearch(tempBR, termsIndex, value); - } - - if (index < 0) { - index = -index - 2; - } - ords[slot] = index; - } - @Override public void copy(int slot, int doc) { final int ord = termsIndex.getOrd(doc); @@ -825,19 +793,30 @@ termsIndex = FieldCache.DEFAULT.getTermsIndex(reader, field); currentReaderGen++; if (bottomSlot != -1) { - convert(bottomSlot); - bottomOrd = ords[bottomSlot]; + setBottom(bottomSlot); } } @Override public void setBottom(final int bottom) { bottomSlot = bottom; - if (readerGen[bottom] != currentReaderGen) { - convert(bottomSlot); + + bottomValue = values[bottomSlot]; + if (bottomValue == null) { + // 0 ord is null for all segments + assert ords[bottomSlot] == 0; + bottomOrd = 0; + bottomGen = currentReaderGen; + } else { + final int index = binarySearch(tempBR, termsIndex, bottomValue); + if (index < 0) { + bottomOrd = -index - 2; + } else { + bottomOrd = index; + // exact value match + bottomGen = currentReaderGen; + } } - bottomOrd = ords[bottom]; - bottomValue = values[bottom]; } @Override