Index: lucene/src/java/org/apache/lucene/search/FieldComparator.java =================================================================== --- lucene/src/java/org/apache/lucene/search/FieldComparator.java (revision 962912) +++ lucene/src/java/org/apache/lucene/search/FieldComparator.java (working copy) @@ -703,16 +703,13 @@ private int bottomSlot = -1; private int bottomOrd; + private int bottomGen; private String bottomValue; - private final boolean reversed; - private final int sortPos; public StringOrdValComparator(int numHits, String field, int sortPos, boolean reversed) { ords = new int[numHits]; values = new String[numHits]; readerGen = new int[numHits]; - this.sortPos = sortPos; - this.reversed = reversed; this.field = field; } @@ -743,7 +740,7 @@ assert bottomSlot != -1; int order = this.order[doc]; final int cmp = bottomOrd - order; - if (cmp != 0) { + if (cmp != 0 || bottomGen == currentReaderGen) { return cmp; } @@ -761,35 +758,6 @@ return bottomValue.compareTo(val2); } - private void convert(int slot) { - readerGen[slot] = currentReaderGen; - int index = 0; - String value = values[slot]; - if (value == null) { - 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: - assert bottomOrd < lookup.length; - if (reversed) { - index = binarySearch(lookup, value, bottomOrd, lookup.length-1); - } else { - index = binarySearch(lookup, value, 0, bottomOrd); - } - } else { - // Full binary search - index = binarySearch(lookup, value); - } - - if (index < 0) { - index = -index - 2; - } - ords[slot] = index; - } - @Override public void copy(int slot, int doc) { final int ord = order[doc]; @@ -807,21 +775,29 @@ lookup = currentReaderValues.lookup; assert lookup.length > 0; 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) { + ords[bottomSlot] = 0; + bottomOrd = 0; + bottomGen = currentReaderGen; + } else { + final int index = binarySearch(lookup, bottomValue); + if (index < 0) { + bottomOrd = -index - 2; + } else { + bottomOrd = index; + // exact value match + bottomGen = currentReaderGen; + } } - bottomOrd = ords[bottom]; - assert bottomOrd >= 0; - assert bottomOrd < lookup.length; - bottomValue = values[bottom]; } @Override