Apache Solr open-source search software
LUCENE-5399: add fangs, fix 2 bugs
Michael McCandlessa5691b144753
diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.javaindex c45ad9298fd..44bec130998 100644--- a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java+++ b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java@@ -876,6 +876,7 @@ public abstract class FieldComparator<T> {topOrd = missingOrd;topSameReader = true;}+ //System.out.println(" setNextReader topOrd=" + topOrd + " topSameReader=" + topSameReader);if (bottomSlot != -1) {// Recompute bottomOrd/SameReader@@ -921,6 +922,7 @@ public abstract class FieldComparator<T> {// null is fine: it means the last doc of the prior// search was missing this valuetopValue = value;+ //System.out.println("setTopValue " + topValue);}@Override@@ -932,9 +934,14 @@ public abstract class FieldComparator<T> {public int compareTop(int doc) {int ord = termsIndex.getOrd(doc);+ if (ord == -1) {+ ord = missingOrd;+ }if (topSameReader) {- // ord is precisely comparable, even in the equal case+ // ord is precisely comparable, even in the equal+ // case+ //System.out.println("compareTop doc=" + doc + " ord=" + ord + " ret=" + (topOrd-ord));return topOrd - ord;} else if (ord <= topOrd) {// the equals case always means doc is < value@@ -1066,6 +1073,7 @@ public abstract class FieldComparator<T> {private void setMissingBytes(int doc, BytesRef br) {if (br.length == 0) {+ br.offset = 0;if (docsWithField.get(doc) == false) {br.bytes = MISSING_BYTES;} else {diff --git a/lucene/core/src/java/org/apache/lucene/search/SortField.java b/lucene/core/src/java/org/apache/lucene/search/SortField.javaindex 55f91c43276..57bf39e3d52 100644--- a/lucene/core/src/java/org/apache/lucene/search/SortField.java+++ b/lucene/core/src/java/org/apache/lucene/search/SortField.java@@ -169,13 +169,25 @@ public class SortField {this.parser = parser;}+ // nocommit should missing first/last not be affected by reverse=true???+/** Pass this to {@link #setMissingValue} to have missing* string values sort first. */- public final static Object STRING_FIRST = new Object();+ public final static Object STRING_FIRST = new Object() {+ @Override+ public String toString() {+ return "SortField.STRING_FIRST";+ }+ };/** Pass this to {@link #setMissingValue} to have missing* string values sort last. */- public final static Object STRING_LAST = new Object();+ public final static Object STRING_LAST = new Object() {+ @Override+ public String toString() {+ return "SortField.STRING_LAST";+ }+ };public void setMissingValue(Object missingValue) {if (type == Type.STRING) {@@ -308,6 +320,10 @@ public class SortField {}if (reverse) buffer.append('!');+ if (missingValue != null) {+ buffer.append(" missingValue=");+ buffer.append(missingValue);+ }return buffer.toString();}diff --git a/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java b/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.javaindex fff8a91593d..3f1fa156230 100644--- a/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java+++ b/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java@@ -887,6 +887,8 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {@SuppressWarnings({"unchecked", "rawtypes"})@Overridepublic void collect(int doc) throws IOException {+ //System.out.println(" collect doc=" + doc);+totalHits++;float score = Float.NaN;@@ -919,8 +921,6 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {}}- //System.out.println(" collect doc=" + doc);-// Check if this hit was already collected on a// previous page:boolean sameValues = true;@@ -935,7 +935,7 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {} else if (cmp < 0) {// Not yet collectedsameValues = false;- //System.out.println(" keep: after");+ //System.out.println(" keep: after; reverseMul=" + reverseMul[compIDX]);break;}}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSearchAfter.java b/lucene/core/src/test/org/apache/lucene/search/TestSearchAfter.javaindex c4f438557ef..93054730271 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestSearchAfter.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSearchAfter.java@@ -51,8 +51,6 @@ public class TestSearchAfter extends LuceneTestCase {private IndexSearcher searcher;private int iter;- // nocommit add SF.setMissingValue too-@Overridepublic void setUp() throws Exception {super.setUp();@@ -132,7 +130,7 @@ public class TestSearchAfter extends LuceneTestCase {}}- static SortField[] allSortFields = new SortField[] {+ static List<SortField> allSortFields = new ArrayList<SortField>(Arrays.asList(new SortField[] {new SortField("int", SortField.Type.INT, false),new SortField("long", SortField.Type.LONG, false),new SortField("float", SortField.Type.FLOAT, false),@@ -157,8 +155,25 @@ public class TestSearchAfter extends LuceneTestCase {new SortField("straightbytesdocvalues", SortField.Type.STRING_VAL, true),SortField.FIELD_SCORE,SortField.FIELD_DOC,- // nocommit add RELEVANCE too- };+ }));++ static {+ // Also test missing first / last for the "string" sorts:+ for(String field : new String[] {"bytes", "sortedbytesdocvalues"}) {+ for(int rev=0;rev<2;rev++) {+ boolean reversed = rev == 0;+ SortField sf = new SortField(field, SortField.Type.STRING, reversed);+ sf.setMissingValue(SortField.STRING_FIRST);+ allSortFields.add(sf);++ sf = new SortField(field, SortField.Type.STRING, reversed);+ sf.setMissingValue(SortField.STRING_LAST);+ allSortFields.add(sf);+ }+ }++ // nocommit test missing value for numerics too:+ }void assertQuery(Query query, Filter filter) throws Exception {assertQuery(query, filter, null);@@ -175,7 +190,7 @@ public class TestSearchAfter extends LuceneTestCase {Sort getRandomSort() {SortField[] sortFields = new SortField[_TestUtil.nextInt(random(), 2, 7)];for(int i=0;i<sortFields.length;i++) {- sortFields[i] = allSortFields[random().nextInt(allSortFields.length)];+ sortFields[i] = allSortFields.get(random().nextInt(allSortFields.size()));}return new Sort(sortFields);}