Package org.apache.lucene.search
Class SortField
- java.lang.Object
-
- org.apache.lucene.search.SortField
-
- Direct Known Subclasses:
SortedNumericSortField
,SortedSetSortField
public class SortField extends Object
Stores 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 class
SortField.Type
Specifies the type of the terms to be sorted, or special types such as CUSTOM
-
Field Summary
Fields Modifier and Type Field Description static SortField
FIELD_DOC
Represents sorting by document number (index order).static SortField
FIELD_SCORE
Represents sorting by document score (relevance).protected Object
missingValue
static Object
STRING_FIRST
Pass this tosetMissingValue(java.lang.Object)
to have missing string values sort first.static Object
STRING_LAST
Pass this tosetMissingValue(java.lang.Object)
to have missing string values sort last.
-
Constructor Summary
Constructors Constructor Description SortField(String field, FieldComparatorSource comparator)
Creates a sort with a custom comparison function.SortField(String field, FieldComparatorSource comparator, boolean reverse)
Creates a sort, possibly in reverse, with a custom comparison function.SortField(String field, SortField.Type type)
Creates a sort by terms in the given field with the type of term values explicitly given.SortField(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 Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
Returns true ifo
is equal to this.Comparator<BytesRef>
getBytesComparator()
FieldComparator<?>
getComparator(int numHits, int sortPos)
Returns theFieldComparator
to use for sorting.FieldComparatorSource
getComparatorSource()
Returns theFieldComparatorSource
used for custom sortingString
getField()
Returns the name of the field.Object
getMissingValue()
Return the value to use for documents that don't have a value.boolean
getReverse()
Returns whether the sort should be reversed.SortField.Type
getType()
Returns the type of contents in the field.int
hashCode()
Returns a hash code for thisSortField
instance.boolean
needsScores()
Whether the relevance score is needed to sort documents.SortField
rewrite(IndexSearcher searcher)
Rewrites this SortField, returning a new SortField if a change is made.void
setBytesComparator(Comparator<BytesRef> b)
void
setMissingValue(Object missingValue)
Set the value to use for documents that don't have a value.String
toString()
-
-
-
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).
-
missingValue
protected Object missingValue
-
STRING_FIRST
public static final Object STRING_FIRST
Pass this tosetMissingValue(java.lang.Object)
to have missing string values sort first.
-
STRING_LAST
public static final Object STRING_LAST
Pass this tosetMissingValue(java.lang.Object)
to have missing string values sort last.
-
-
Constructor Detail
-
SortField
public SortField(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 benull
iftype
is SCORE or DOC.type
- Type of values in the terms.
-
SortField
public SortField(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 benull
iftype
is SCORE or DOC.type
- Type of values in the terms.reverse
- True if natural order should be reversed.
-
SortField
public SortField(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(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
-
getMissingValue
public Object getMissingValue()
Return the value to use for documents that don't have a value. A value ofnull
indicates that default should be used.
-
setMissingValue
public void setMissingValue(Object missingValue)
Set the value to use for documents that don't have a value.
-
getField
public String getField()
Returns the name of the field. Could returnnull
if 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 theFieldComparatorSource
used for custom sorting
-
equals
public boolean equals(Object o)
Returns true ifo
is equal to this. If aFieldComparatorSource
was provided, it must properly implement equals (unless a singleton is always used).
-
hashCode
public int hashCode()
Returns a hash code for thisSortField
instance. If aFieldComparatorSource
was provided, it must properly implement hashCode (unless a singleton is always used).
-
setBytesComparator
public void setBytesComparator(Comparator<BytesRef> b)
-
getBytesComparator
public Comparator<BytesRef> getBytesComparator()
-
getComparator
public FieldComparator<?> getComparator(int numHits, int sortPos)
Returns theFieldComparator
to 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:
FieldComparator
to use when sorting- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
rewrite
public SortField rewrite(IndexSearcher searcher) throws 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
this
if nothing has changed. - Throws:
IOException
- Can be thrown by the rewriting- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
needsScores
public boolean needsScores()
Whether the relevance score is needed to sort documents.
-
-