Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-1779: improve javadocs & remove unused args from unreleased sorting APIs

Michael McCandless1ab8d5ee6dea
diff --git a/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java b/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java
index 47fc8343430..42aa6b9f826 100644
--- a/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java
+++ b/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java
@@ -210,7 +210,7 @@ public class TestRemoteSort extends LuceneTestCase implements Serializable {
bottomValue = slotValues[bottom];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
docValues = FieldCache.DEFAULT.getInts(reader, "parser", new FieldCache.IntParser() {
public final int parseInt(final String val) {
return (val.charAt(0)-'A') * 123456;
@@ -218,10 +218,6 @@ public class TestRemoteSort extends LuceneTestCase implements Serializable {
});
}
- public int sortType() {
- return 0;
- }
-
public Comparable value(int slot) {
return new Integer(slotValues[slot]);
}
diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/tier/DistanceFieldComparatorSource.java b/contrib/spatial/src/java/org/apache/lucene/spatial/tier/DistanceFieldComparatorSource.java
index 8530414f07f..70f328faaba 100644
--- a/contrib/spatial/src/java/org/apache/lucene/spatial/tier/DistanceFieldComparatorSource.java
+++ b/contrib/spatial/src/java/org/apache/lucene/spatial/tier/DistanceFieldComparatorSource.java
@@ -108,8 +108,8 @@ public class DistanceFieldComparatorSource extends FieldComparatorSource {
}
@Override
- public void setNextReader(IndexReader reader, int docBase,
- int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase)
+ throws IOException {
// each reader in a segmented base
// has an offset based on the maxDocs of previous readers
@@ -120,12 +120,6 @@ public class DistanceFieldComparatorSource extends FieldComparatorSource {
public Comparable<Double> value(int slot) {
return values[slot];
}
-
- @Override
- public int sortType() {
-
- return SortField.DOUBLE;
- }
}
}
diff --git a/src/java/org/apache/lucene/search/FieldComparator.java b/src/java/org/apache/lucene/search/FieldComparator.java
index a31db1d0a39..566ecde9b77 100644
--- a/src/java/org/apache/lucene/search/FieldComparator.java
+++ b/src/java/org/apache/lucene/search/FieldComparator.java
@@ -31,12 +31,53 @@ import org.apache.lucene.search.FieldCache.ShortParser;
import org.apache.lucene.search.FieldCache.StringIndex;
/**
- * A FieldComparator compares hits across multiple IndexReaders.
- *
- * A comparator can compare a hit at hit 'slot a' with hit 'slot b',
- * compare a hit on 'doc i' with hit 'slot a', or copy a hit at 'doc i'
- * to 'slot a'. Each slot refers to a hit while each doc refers to the
- * current IndexReader.
+ * Expert: a FieldComparator compares hits so as to determine their
+ * sort order when collecting the top results with {@link
+ * TopFieldCollector}. The concrete public FieldComparator
+ * classes here correspond to the SortField types.
+ *
+ * <p>This API is designed to achieve high performance
+ * sorting, by exposing a tight interaction with {@link
+ * FieldValueHitQueue} as it visits hits. Whenever a hit is
+ * competitive, it's enrolled into a virtual slot, which is
+ * an int ranging from 0 to numHits-1. The {@link
+ * FieldComparator} is made aware of segment transitions
+ * during searching in case any internal state it's tracking
+ * needs to be recomputed during these transitions.</p>
+ *
+ * <p>A comparator must define these functions:</p>
+ *
+ * <ul>
+ *
+ * <li> {@link #compare} Compare a hit at 'slot a'
+ * with hit 'slot b'.
+ *
+ * <li> {@link #setBottom} This method is called by
+ * {@link FieldValueHitQueue} to notify the
+ * FieldComparator of the current weakest ("bottom")
+ * slot. Note that this slot may not hold the weakest
+ * value according to your comparator, in cases where
+ * your comparator is not the primary one (ie, is only
+ * used to break ties from the comparators before it).
+ *
+ * <li> {@link #compareBottom} Compare a new hit (docID)
+ * against the "weakest" (bottom) entry in the queue.
+ *
+ * <li> {@link #copy} Installs a new hit into the
+ * priority queue. The {@link FieldValueHitQueue}
+ * calls this method when a new hit is competitive.
+ *
+ * <li> {@link #setNextReader} Invoked
+ * when the search is switching to the next segment.
+ * You may need to update internal state of the
+ * comparator, for example retrieving new values from
+ * the {@link FieldCache}.
+ *
+ * <li> {@link #value} Return the sort value stored in
+ * the specified slot. This is only called at the end
+ * of the search, in order to populate {@link
+ * FieldDoc#fields} when returning the top results.
+ * </ul>
*
* <b>NOTE:</b> This API is experimental and might change in
* incompatible ways in the next release.
@@ -70,7 +111,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getBytes(reader, field, parser);
}
@@ -78,10 +119,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.BYTE;
- }
-
public Comparable value(int slot) {
return new Byte(values[slot]);
}
@@ -111,7 +148,7 @@ public abstract class FieldComparator {
docIDs[slot] = docBase + doc;
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) {
+ public void setNextReader(IndexReader reader, int docBase) {
// TODO: can we "map" our docIDs to the current
// reader? saves having to then subtract on every
// compare call
@@ -122,10 +159,6 @@ public abstract class FieldComparator {
this.bottom = docIDs[bottom];
}
- public int sortType() {
- return SortField.DOC;
- }
-
public Comparable value(int slot) {
return new Integer(docIDs[slot]);
}
@@ -173,7 +206,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getDoubles(reader, field, parser);
}
@@ -181,10 +214,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.DOUBLE;
- }
-
public Comparable value(int slot) {
return new Double(values[slot]);
}
@@ -236,7 +265,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getFloats(reader, field, parser);
}
@@ -244,10 +273,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.FLOAT;
- }
-
public Comparable value(int slot) {
return new Float(values[slot]);
}
@@ -303,7 +328,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getInts(reader, field, parser);
}
@@ -311,10 +336,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.INT;
- }
-
public Comparable value(int slot) {
return new Integer(values[slot]);
}
@@ -366,7 +387,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getLongs(reader, field, parser);
}
@@ -374,10 +395,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.LONG;
- }
-
public Comparable value(int slot) {
return new Long(values[slot]);
}
@@ -413,7 +430,7 @@ public abstract class FieldComparator {
scores[slot] = scorer.score();
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) {
+ public void setNextReader(IndexReader reader, int docBase) {
}
public void setBottom(final int bottom) {
@@ -426,10 +443,6 @@ public abstract class FieldComparator {
this.scorer = new ScoreCachingWrappingScorer(scorer);
}
- public int sortType() {
- return SortField.SCORE;
- }
-
public Comparable value(int slot) {
return new Float(scores[slot]);
}
@@ -462,7 +475,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getShorts(reader, field, parser);
}
@@ -470,10 +483,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.BYTE;
- }
-
public Comparable value(int slot) {
return new Short(values[slot]);
}
@@ -526,7 +535,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
@@ -534,18 +543,20 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.STRING;
- }
-
public Comparable value(int slot) {
return values[slot];
}
}
- // NOTE: there were a number of other interesting String
- // comparators explored, but this one seemed to perform
- // best all around. See LUCENE-1483 for details.
+ /** Sorts by field's natural String sort order, using
+ * ordinals. This is functionally equivalent to {@link
+ * StringValComparator}, but it first resolves the string
+ * to their relative ordinal positions (using the index
+ * returned by {@link FieldCache#getStringIndex}), and
+ * does most comparisons using the ordinals. For medium
+ * to large results, this comparator will be much faster
+ * than {@link StringValComparator}. For very small
+ * result sets it may be slower. */
public static final class StringOrdValComparator extends FieldComparator {
private final int[] ords;
@@ -652,7 +663,7 @@ public abstract class FieldComparator {
readerGen[slot] = currentReaderGen;
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
StringIndex currentReaderValues = FieldCache.DEFAULT.getStringIndex(reader, field);
currentReaderGen++;
order = currentReaderValues.order;
@@ -675,10 +686,6 @@ public abstract class FieldComparator {
bottomValue = values[bottom];
}
- public int sortType() {
- return SortField.STRING;
- }
-
public Comparable value(int slot) {
return values[slot];
}
@@ -744,7 +751,7 @@ public abstract class FieldComparator {
values[slot] = currentReaderValues[doc];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
@@ -752,10 +759,6 @@ public abstract class FieldComparator {
this.bottom = values[bottom];
}
- public int sortType() {
- return SortField.STRING_VAL;
- }
-
public Comparable value(int slot) {
return values[slot];
}
@@ -788,7 +791,7 @@ public abstract class FieldComparator {
}
/**
- * Compare hit at slot1 with hit at slot2. Return
+ * Compare hit at slot1 with hit at slot2.
*
* @param slot1 first slot to compare
* @param slot2 second slot to compare
@@ -799,16 +802,25 @@ public abstract class FieldComparator {
public abstract int compare(int slot1, int slot2);
/**
- * Set the bottom queue slot, ie the "weakest" (sorted
- * last) entry in the queue.
+ * Set the bottom slot, ie the "weakest" (sorted last)
+ * entry in the queue. When {@link #compareBottom} is
+ * called, you should compare against this slot. This
+ * will always be called before {@link #compareBottom}.
*
- * @param slot the currently weakest (sorted lost) slot in the queue
+ * @param slot the currently weakest (sorted last) slot in the queue
*/
public abstract void setBottom(final int slot);
/**
* Compare the bottom of the queue with doc. This will
- * only invoked after setBottom has been called.
+ * only invoked after setBottom has been called. This
+ * should return the same result as {@link
+ * #compare(int,int)}} as if bottom were slot1 and the new
+ * document were slot 2.
+ *
+ * <p>For a search that hits many results, this method
+ * will be the hotspot (invoked by far the most
+ * frequently).</p>
*
* @param doc that was hit
* @return any N < 0 if the doc's value is sorted after
@@ -819,7 +831,10 @@ public abstract class FieldComparator {
public abstract int compareBottom(int doc) throws IOException;
/**
- * Copy hit (doc,score) to hit slot.
+ * This method is called when a new hit is competitive.
+ * You should copy any state associated with this document
+ * that will be required for future comparisons, into the
+ * specified slot.
*
* @param slot which slot to copy the hit to
* @param doc docID relative to current reader
@@ -834,22 +849,21 @@ public abstract class FieldComparator {
* @throws IOException
* @throws IOException
*/
- public abstract void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException;
+ public abstract void setNextReader(IndexReader reader, int docBase) throws IOException;
- /** Sets the Scorer to use in case a document's score is needed. */
+ /** Sets the Scorer to use in case a document's score is
+ * needed.
+ *
+ * @param scorer Scorer instance that you should use to
+ * obtain the current hit's score, if necessary. */
public void setScorer(Scorer scorer) {
// Empty implementation since most comparators don't need the score. This
// can be overridden by those that need it.
}
/**
- * @return SortField.TYPE
- */
- public abstract int sortType();
-
- /**
- * Return the actual value at slot.
- *
+ * Return the actual value in the slot.
+ *
* @param slot the value
* @return value in this slot upgraded to Comparable
*/
diff --git a/src/java/org/apache/lucene/search/FieldValueHitQueue.java b/src/java/org/apache/lucene/search/FieldValueHitQueue.java
index cd4da555eeb..45c3a49917d 100644
--- a/src/java/org/apache/lucene/search/FieldValueHitQueue.java
+++ b/src/java/org/apache/lucene/search/FieldValueHitQueue.java
@@ -76,7 +76,7 @@ public abstract class FieldValueHitQueue extends PriorityQueue {
SortField field = fields[0];
// AUTO is resolved before we are called
assert field.getType() != SortField.AUTO;
- comparator = field.getComparator(size, 0, field.reverse);
+ comparator = field.getComparator(size, 0);
oneReverseMul = field.reverse ? -1 : 1;
comparators[0] = comparator;
@@ -127,7 +127,7 @@ public abstract class FieldValueHitQueue extends PriorityQueue {
assert field.getType() != SortField.AUTO;
reverseMul[i] = field.reverse ? -1 : 1;
- comparators[i] = field.getComparator(size, i, field.reverse);
+ comparators[i] = field.getComparator(size, i);
}
initialize(size);
diff --git a/src/java/org/apache/lucene/search/SortField.java b/src/java/org/apache/lucene/search/SortField.java
index 24706c76144..356bf1f861f 100644
--- a/src/java/org/apache/lucene/search/SortField.java
+++ b/src/java/org/apache/lucene/search/SortField.java
@@ -443,16 +443,20 @@ implements Serializable {
}
- /** Returns the {@link FieldComparator} to use for sorting.
+ /** Returns the {@link FieldComparator} to use for
+ * sorting.
+ *
+ * <b>NOTE:</b> This API is experimental and might change in
+ * incompatible ways in the next release.
+ *
* @param numHits number of top hits the queue will store
* @param sortPos position of this SortField within {@link
* Sort}. The comparator is primary if sortPos==0,
* secondary if sortPos==1, etc. Some comparators can
* optimize themselves when they are the primary sort.
- * @param reversed True if the SortField is reversed
* @return {@link FieldComparator} to use when sorting
*/
- public FieldComparator getComparator(final int numHits, final int sortPos, final boolean reversed) throws IOException {
+ public FieldComparator getComparator(final int numHits, final int sortPos) throws IOException {
if (locale != null) {
// TODO: it'd be nice to allow FieldCache.getStringIndex
@@ -488,10 +492,10 @@ implements Serializable {
case SortField.CUSTOM:
assert factory == null && comparatorSource != null;
- return comparatorSource.newComparator(field, numHits, sortPos, reversed);
+ return comparatorSource.newComparator(field, numHits, sortPos, reverse);
case SortField.STRING:
- return new FieldComparator.StringOrdValComparator(numHits, field, sortPos, reversed);
+ return new FieldComparator.StringOrdValComparator(numHits, field, sortPos, reverse);
case SortField.STRING_VAL:
return new FieldComparator.StringValComparator(numHits, field);
diff --git a/src/java/org/apache/lucene/search/TopFieldCollector.java b/src/java/org/apache/lucene/search/TopFieldCollector.java
index d4d23d9cf49..82f870359d3 100644
--- a/src/java/org/apache/lucene/search/TopFieldCollector.java
+++ b/src/java/org/apache/lucene/search/TopFieldCollector.java
@@ -87,9 +87,8 @@ public abstract class TopFieldCollector extends TopDocsCollector {
}
public void setNextReader(IndexReader reader, int docBase) throws IOException {
- final int numSlotsFull = queueFull ? numHits : totalHits;
this.docBase = docBase;
- comparator.setNextReader(reader, docBase, numSlotsFull);
+ comparator.setNextReader(reader, docBase);
}
public void setScorer(Scorer scorer) throws IOException {
@@ -428,10 +427,9 @@ public abstract class TopFieldCollector extends TopDocsCollector {
}
public void setNextReader(IndexReader reader, int docBase) throws IOException {
- final int numSlotsFull = queueFull ? numHits : totalHits;
this.docBase = docBase;
for (int i = 0; i < comparators.length; i++) {
- comparators[i].setNextReader(reader, docBase, numSlotsFull);
+ comparators[i].setNextReader(reader, docBase);
}
}
diff --git a/src/test/org/apache/lucene/search/JustCompileSearch.java b/src/test/org/apache/lucene/search/JustCompileSearch.java
index 896ccb41b2b..50c98dbf486 100644
--- a/src/test/org/apache/lucene/search/JustCompileSearch.java
+++ b/src/test/org/apache/lucene/search/JustCompileSearch.java
@@ -221,15 +221,11 @@ final class JustCompileSearch {
throw new UnsupportedOperationException(UNSUPPORTED_MSG);
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull)
+ public void setNextReader(IndexReader reader, int docBase)
throws IOException {
throw new UnsupportedOperationException(UNSUPPORTED_MSG);
}
- public int sortType() {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
public Comparable value(int slot) {
throw new UnsupportedOperationException(UNSUPPORTED_MSG);
}
diff --git a/src/test/org/apache/lucene/search/TestElevationComparator.java b/src/test/org/apache/lucene/search/TestElevationComparator.java
index ba296895594..036ff65a443 100644
--- a/src/test/org/apache/lucene/search/TestElevationComparator.java
+++ b/src/test/org/apache/lucene/search/TestElevationComparator.java
@@ -21,7 +21,6 @@ import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.*;
-import org.apache.lucene.search.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.LuceneTestCase;
@@ -165,14 +164,10 @@ class ElevationComparatorSource extends FieldComparatorSource {
values[slot] = docVal(doc);
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
idIndex = FieldCache.DEFAULT.getStringIndex(reader, fieldname);
}
- public int sortType() {
- return SortField.CUSTOM;
- }
-
public Comparable value(int slot) {
return new Integer(values[slot]);
}
diff --git a/src/test/org/apache/lucene/search/TestSort.java b/src/test/org/apache/lucene/search/TestSort.java
index b5c97ee82cc..8707823de15 100644
--- a/src/test/org/apache/lucene/search/TestSort.java
+++ b/src/test/org/apache/lucene/search/TestSort.java
@@ -412,7 +412,7 @@ public class TestSort extends LuceneTestCase implements Serializable {
bottomValue = slotValues[bottom];
}
- public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
+ public void setNextReader(IndexReader reader, int docBase) throws IOException {
docValues = FieldCache.DEFAULT.getInts(reader, "parser", new FieldCache.IntParser() {
public final int parseInt(final String val) {
return (val.charAt(0)-'A') * 123456;
@@ -420,10 +420,6 @@ public class TestSort extends LuceneTestCase implements Serializable {
});
}
- public int sortType() {
- return 0;
- }
-
public Comparable value(int slot) {
return new Integer(slotValues[slot]);
}