Package org.apache.lucene.search
Class SortField
- java.lang.Object
-
- org.apache.lucene.search.SortField
-
- Direct Known Subclasses:
DoubleValuesSource.DoubleValuesSortField,FeatureSortField,LatLonPointSortField,LongValuesSource.LongValuesSortField,SortedNumericSortField,SortedSetSortField,ToParentBlockJoinSortField,ValueSource.ValueSourceSortField,XYPointSortField
public class SortField extends java.lang.ObjectStores information about how to sort documents by terms in an individual field. Fields must be indexed in order to sort by them.Created: Feb 11, 2004 1:25:29 PM
- Since:
- lucene 1.4
- See Also:
Sort
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSortField.ProviderA SortFieldProvider for field sortsstatic classSortField.TypeSpecifies the type of the terms to be sorted, or special types such as CUSTOM
-
Field Summary
Fields Modifier and Type Field Description private java.util.Comparator<BytesRef>bytesComparatorprivate booleancanUsePointsprivate FieldComparatorSourcecomparatorSourceprivate java.lang.Stringfieldstatic SortFieldFIELD_DOCRepresents sorting by document number (index order).static SortFieldFIELD_SCORERepresents sorting by document score (relevance).protected java.lang.ObjectmissingValue(package private) booleanreversestatic java.lang.ObjectSTRING_FIRSTPass this tosetMissingValue(java.lang.Object)to have missing string values sort first.static java.lang.ObjectSTRING_LASTPass this tosetMissingValue(java.lang.Object)to have missing string values sort last.private SortField.Typetype
-
Constructor Summary
Constructors Constructor Description SortField(java.lang.String field, FieldComparatorSource comparator)Creates a sort with a custom comparison function.SortField(java.lang.String field, FieldComparatorSource comparator, boolean reverse)Creates a sort, possibly in reverse, with a custom comparison function.SortField(java.lang.String field, SortField.Type type)Creates a sort by terms in the given field with the type of term values explicitly given.SortField(java.lang.String field, SortField.Type type, boolean reverse)Creates a sort, possibly in reverse, by terms in the given field with the type of term values explicitly given.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object o)Returns true ifois equal to this.java.util.Comparator<BytesRef>getBytesComparator()booleangetCanUsePoints()FieldComparator<?>getComparator(int numHits, int sortPos)Returns theFieldComparatorto use for sorting.FieldComparatorSourcegetComparatorSource()Returns theFieldComparatorSourceused for custom sortingjava.lang.StringgetField()Returns the name of the field.IndexSortergetIndexSorter()Returns anIndexSorterused for sorting index segments by this SortField.java.lang.ObjectgetMissingValue()Return the value to use for documents that don't have a value.booleangetReverse()Returns whether the sort should be reversed.SortField.TypegetType()Returns the type of contents in the field.inthashCode()Returns a hash code for thisSortFieldinstance.private voidinitFieldType(java.lang.String field, SortField.Type type)booleanneedsScores()Whether the relevance score is needed to sort documents.protected static SortField.TypereadType(DataInput in)SortFieldrewrite(IndexSearcher searcher)Rewrites this SortField, returning a new SortField if a change is made.private voidserialize(DataOutput out)voidsetBytesComparator(java.util.Comparator<BytesRef> b)voidsetCanUsePoints()For numeric sort fields, setting this field, indicates that the same numeric data has been indexed with two fields: doc values and points and that these fields have the same name.voidsetMissingValue(java.lang.Object missingValue)Set the value to use for documents that don't have a value.java.lang.StringtoString()
-
-
-
Field Detail
-
FIELD_SCORE
public static final SortField FIELD_SCORE
Represents sorting by document score (relevance).
-
FIELD_DOC
public static final SortField FIELD_DOC
Represents sorting by document number (index order).
-
field
private java.lang.String field
-
type
private SortField.Type type
-
reverse
boolean reverse
-
comparatorSource
private FieldComparatorSource comparatorSource
-
missingValue
protected java.lang.Object missingValue
-
canUsePoints
private boolean canUsePoints
-
STRING_FIRST
public static final java.lang.Object STRING_FIRST
Pass this tosetMissingValue(java.lang.Object)to have missing string values sort first.
-
STRING_LAST
public static final java.lang.Object STRING_LAST
Pass this tosetMissingValue(java.lang.Object)to have missing string values sort last.
-
bytesComparator
private java.util.Comparator<BytesRef> bytesComparator
-
-
Constructor Detail
-
SortField
public SortField(java.lang.String field, SortField.Type type)Creates a sort by terms in the given field with the type of term values explicitly given.- Parameters:
field- Name of field to sort by. Can benulliftypeis SCORE or DOC.type- Type of values in the terms.
-
SortField
public SortField(java.lang.String field, SortField.Type type, boolean reverse)Creates a sort, possibly in reverse, by terms in the given field with the type of term values explicitly given.- Parameters:
field- Name of field to sort by. Can benulliftypeis SCORE or DOC.type- Type of values in the terms.reverse- True if natural order should be reversed.
-
SortField
public SortField(java.lang.String field, FieldComparatorSource comparator)Creates a sort with a custom comparison function.- Parameters:
field- Name of field to sort by; cannot benull.comparator- Returns a comparator for sorting hits.
-
SortField
public SortField(java.lang.String field, FieldComparatorSource comparator, boolean reverse)Creates a sort, possibly in reverse, with a custom comparison function.- Parameters:
field- Name of field to sort by; cannot benull.comparator- Returns a comparator for sorting hits.reverse- True if natural order should be reversed.
-
-
Method Detail
-
readType
protected static SortField.Type readType(DataInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
serialize
private void serialize(DataOutput out) throws java.io.IOException
- Throws:
java.io.IOException
-
getMissingValue
public java.lang.Object getMissingValue()
Return the value to use for documents that don't have a value. A value ofnullindicates that default should be used.
-
setMissingValue
public void setMissingValue(java.lang.Object missingValue)
Set the value to use for documents that don't have a value.
-
initFieldType
private void initFieldType(java.lang.String field, SortField.Type type)
-
getField
public java.lang.String getField()
Returns the name of the field. Could returnnullif the sort is by SCORE or DOC.- Returns:
- Name of field, possibly
null.
-
getType
public SortField.Type getType()
Returns the type of contents in the field.- Returns:
- One of the constants SCORE, DOC, STRING, INT or FLOAT.
-
getReverse
public boolean getReverse()
Returns whether the sort should be reversed.- Returns:
- True if natural order should be reversed.
-
getComparatorSource
public FieldComparatorSource getComparatorSource()
Returns theFieldComparatorSourceused for custom sorting
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
setCanUsePoints
public void setCanUsePoints()
For numeric sort fields, setting this field, indicates that the same numeric data has been indexed with two fields: doc values and points and that these fields have the same name. This allows to use sort optimization and skip non-competitive documents.
-
getCanUsePoints
public boolean getCanUsePoints()
-
equals
public boolean equals(java.lang.Object o)
Returns true ifois equal to this. If aFieldComparatorSourcewas provided, it must properly implement equals (unless a singleton is always used).canUsePointsfield is not part ofequalsandhasCodeintentionally, as it is only useful during search-time and using it in these functions prevents index sorting optimizations that rely on the equality of the index-time and search-time SortField instances.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
Returns a hash code for thisSortFieldinstance. If aFieldComparatorSourcewas provided, it must properly implement hashCode (unless a singleton is always used).- Overrides:
hashCodein classjava.lang.Object
-
setBytesComparator
public void setBytesComparator(java.util.Comparator<BytesRef> b)
-
getBytesComparator
public java.util.Comparator<BytesRef> getBytesComparator()
-
getComparator
public FieldComparator<?> getComparator(int numHits, int sortPos)
Returns theFieldComparatorto use for sorting.- Parameters:
numHits- number of top hits the queue will storesortPos- position of this SortField withinSort. The comparator is primary if sortPos==0, secondary if sortPos==1, etc. Some comparators can optimize themselves when they are the primary sort.- Returns:
FieldComparatorto use when sorting
-
rewrite
public SortField rewrite(IndexSearcher searcher) throws java.io.IOException
Rewrites this SortField, returning a new SortField if a change is made. Subclasses should override this define their rewriting behavior when this SortField is of typeSortField.Type.REWRITEABLE- Parameters:
searcher- IndexSearcher to use during rewriting- Returns:
- New rewritten SortField, or
thisif nothing has changed. - Throws:
java.io.IOException- Can be thrown by the rewriting
-
needsScores
public boolean needsScores()
Whether the relevance score is needed to sort documents.
-
getIndexSorter
public IndexSorter getIndexSorter()
Returns anIndexSorterused for sorting index segments by this SortField. If the SortField cannot be used for index sorting (for example, if it uses scores or other query-dependent values) then this method should returnnullSortFields that implement this method should also implement a companionSortFieldProviderto serialize and deserialize the sort in index segment headers
-
-