Class SortField


  • public class SortField
    extends java.lang.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
    • 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

        public java.lang.Object missingValue
      • sortMissingLast

        public boolean sortMissingLast
    • 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 be null if type is 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 be null if type is 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,
                         FieldCache.Parser parser)
        Creates a sort by terms in the given field, parsed to numeric values using a custom FieldCache.Parser.
        Parameters:
        field - Name of field to sort by. Must not be null.
        parser - Instance of a FieldCache.Parser, which must subclass one of the existing numeric parsers from FieldCache. Sort type is inferred by testing which numeric parser the parser subclasses.
        Throws:
        java.lang.IllegalArgumentException - if the parser fails to subclass an existing numeric parser, or field is null
      • SortField

        public SortField​(java.lang.String field,
                         FieldCache.Parser parser,
                         boolean reverse)
        Creates a sort, possibly in reverse, by terms in the given field, parsed to numeric values using a custom FieldCache.Parser.
        Parameters:
        field - Name of field to sort by. Must not be null.
        parser - Instance of a FieldCache.Parser, which must subclass one of the existing numeric parsers from FieldCache. Sort type is inferred by testing which numeric parser the parser subclasses.
        reverse - True if natural order should be reversed.
        Throws:
        java.lang.IllegalArgumentException - if the parser fails to subclass an existing numeric parser, or field is null
      • 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 be null.
        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 be null.
        comparator - Returns a comparator for sorting hits.
        reverse - True if natural order should be reversed.
    • Method Detail

      • setMissingValue

        public void setMissingValue​(java.lang.Object missingValue)
      • getField

        public java.lang.String getField()
        Returns the name of the field. Could return null 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.
      • getParser

        public FieldCache.Parser getParser()
        Returns the instance of a FieldCache parser that fits to the given sort type. May return null if no parser was specified. Sorting is using the default parser then.
        Returns:
        An instance of a FieldCache parser, or null.
      • getReverse

        public boolean getReverse()
        Returns whether the sort should be reversed.
        Returns:
        True if natural order should be reversed.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Returns true if o is equal to this. If a FieldComparatorSource or FieldCache.Parser was provided, it must properly implement equals (unless a singleton is always used).
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Returns true if o is equal to this. If a FieldComparatorSource or FieldCache.Parser was provided, it must properly implement hashCode (unless a singleton is always used).
        Overrides:
        hashCode in class java.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)
                                         throws java.io.IOException
        Returns the FieldComparator to use for sorting.
        Parameters:
        numHits - number of top hits the queue will store
        sortPos - position of this SortField within Sort. 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
        Throws:
        java.io.IOException
      • 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 type SortField.Type.REWRITEABLE
        Parameters:
        searcher - IndexSearcher to use during rewriting
        Returns:
        New rewritten SortField, or this if 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.