Class SortField

    • 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
      • reverse

        boolean reverse
      • missingValue

        protected java.lang.Object missingValue
      • optimizeSortWithIndexedData

        @Deprecated
        private boolean optimizeSortWithIndexedData
        Deprecated.
      • 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 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,
                         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

      • 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 of null indicates 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 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.
      • 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 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 a hash code for this SortField instance. If a FieldComparatorSource 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()
      • 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.
      • getIndexSorter

        public IndexSorter getIndexSorter()
        Returns an IndexSorter used 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 return null

        SortFields that implement this method should also implement a companion SortFieldProvider to serialize and deserialize the sort in index segment headers

      • setOptimizeSortWithIndexedData

        @Deprecated
        public void setOptimizeSortWithIndexedData​(boolean optimizeSortWithIndexedData)
        Deprecated.
        should only be used for compatibility with 8.x indices that got created with inconsistent data across fields, or the wrong sort configuration in the index sort
        Enables/disables numeric sort optimization to use the indexed data.

        Enabled by default. By default, sorting on a numeric field activates point sort optimization that can efficiently skip over non-competitive hits. Sort optimization has a number of requirements, one of which is that SortField.Type matches the Point type with which the field was indexed (e.g. sort on IntPoint field should use SortField.Type.INT). Another requirement is that the same data is indexed with points and doc values for the field.

        By default, sorting on a SORTED(_SET) field activates sort optimization that can efficiently skip over non-competitive hits. Sort optimization requires that the same data is indexed with term index and doc values for the field.

        Parameters:
        optimizeSortWithIndexedData - providing false disables the optimization, in cases where these requirements can't be met.
      • getOptimizeSortWithIndexedData

        @Deprecated
        public boolean getOptimizeSortWithIndexedData()
        Deprecated.
        Returns whether sort optimization should be optimized with indexed data
        Returns:
        whether sort optimization should be optimized with indexed data
      • setOptimizeSortWithPoints

        @Deprecated
        public void setOptimizeSortWithPoints​(boolean optimizeSortWithPoints)
        Deprecated.
        should only be used for compatibility with 8.x indices that got created with inconsistent data across fields, or the wrong sort configuration in the index sort. This is a duplicate method for SortField#setOptimizeSortWithIndexedData.
        Enables/disables numeric sort optimization to use the Points index.

        Enabled by default. By default, sorting on a numeric field activates point sort optimization that can efficiently skip over non-competitive hits. Sort optimization has a number of requirements, one of which is that SortField.Type matches the Point type with which the field was indexed (e.g. sort on IntPoint field should use SortField.Type.INT). Another requirement is that the same data is indexed with points and doc values for the field.

        Parameters:
        optimizeSortWithPoints - providing false disables the optimization, in cases where these requirements can't be met.
      • getOptimizeSortWithPoints

        @Deprecated
        public boolean getOptimizeSortWithPoints()
        Deprecated.
        This is a duplicate method for SortField#getOptimizeSortWithIndexedData.
        Returns whether sort optimization should be optimized with points index
        Returns:
        whether sort optimization should be optimized with points index