## org.apache.lucene.search
Class SortField

    java.lang.Object
      org.apache.lucene.search.SortField

All Implemented Interfaces:
  java.io.Serializable

    public class SortField

extends java.lang.Object
implements java.io.Serializable

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
Version:
  $Id: SortField.java 833298 2009-11-06 04:32:24Z uschindler $
See Also:
  `Sort`, Serialized Form

Field Summary
`static int` `AUTO` Deprecated. Please specify the exact type, instead. Especially, guessing does not work with the new `NumericField` type.
`static int` `BYTE` Sort using term values as encoded Bytes.
`static int` `CUSTOM` Sort using a custom Comparator.
`static int` `DOC` Sort by document number (index order).
`static int` `DOUBLE` Sort using term values as encoded Doubles.
`static SortField` `FIELD_DOC` Represents sorting by document number (index order).
`static SortField` `FIELD_SCORE` Represents sorting by document score (relevancy).
`static int` `FLOAT` Sort using term values as encoded Floats.
`static int` `INT` Sort using term values as encoded Integers.
`static int` `LONG` Sort using term values as encoded Longs.
`static int` `SCORE` Sort by document score (relevancy).
`static int` `SHORT` Sort using term values as encoded Shorts.
`static int` `STRING` Sort using term values as Strings.
`static int` `STRING_VAL` Sort using term values as Strings, but comparing by value (using String.compareTo) for all comparisons.

Constructor Summary
`SortField(java.lang.String field)` Deprecated. Please specify the exact type instead.
`SortField(java.lang.String field, boolean reverse)` Deprecated. Please specify the exact type instead.
`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`.
`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`.
`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, int type)` Creates a sort by terms in the given field with the type of term values explicitly given.
`SortField(java.lang.String field, int type, boolean reverse)` Creates a sort, possibly in reverse, by terms in the given field with the type of term values explicitly given.
`SortField(java.lang.String field, java.util.Locale locale)` Creates a sort by terms in the given field sorted according to the given locale.
`SortField(java.lang.String field, java.util.Locale locale, boolean reverse)` Creates a sort, possibly in reverse, by terms in the given field sorted according to the given locale.
`SortField(java.lang.String field, SortComparatorSource comparator)` Deprecated. use SortField (String field, FieldComparatorSource comparator)
`SortField(java.lang.String field, SortComparatorSource comparator, boolean reverse)` Deprecated. use SortField (String field, FieldComparatorSource comparator, boolean reverse)

Method Summary
`boolean` `equals(java.lang.Object o)` Returns true if `o` is equal to this.
`FieldComparator` `getComparator(int numHits, int sortPos)` Returns the `FieldComparator` to use for sorting.
`FieldComparatorSource` `getComparatorSource()`
`SortComparatorSource` `getFactory()` Deprecated. use `getComparatorSource()`
`java.lang.String` `getField()` Returns the name of the field.
`java.util.Locale` `getLocale()` Returns the Locale by which term values are interpreted.
`FieldCache.Parser` `getParser()` Returns the instance of a `FieldCache` parser that fits to the given sort type.
`boolean` `getReverse()` Returns whether the sort should be reversed.
`int` `getType()` Returns the type of contents in the field.
`boolean` `getUseLegacySearch()` Deprecated. will be removed in Lucene 3.0.
`int` `hashCode()` Returns true if `o` is equal to this.
`void` `setUseLegacySearch(boolean legacy)` Deprecated. will be removed in Lucene 3.0.
`java.lang.String` `toString()`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### SCORE

    public static final int SCORE

  Sort by document score (relevancy). Sort values are Float and higher values are at the front.

  See Also:
    Constant Field Values

### DOC

    public static final int DOC

  Sort by document number (index order). Sort values are Integer and lower values are at the front.

  See Also:
    Constant Field Values

### AUTO

    public static final int AUTO

  Deprecated. Please specify the exact type, instead. Especially, guessing does not work with the new `NumericField` type.
  Guess type of sort based on field contents. A regular expression is used to look at the first term indexed for the field and determine if it represents an integer number, a floating point number, or just arbitrary string characters.

  See Also:
    Constant Field Values

### STRING

    public static final int STRING

  Sort using term values as Strings. Sort values are String and lower values are at the front.

  See Also:
    Constant Field Values

### INT

    public static final int INT

  Sort using term values as encoded Integers. Sort values are Integer and lower values are at the front.

  See Also:
    Constant Field Values

### FLOAT

    public static final int FLOAT

  Sort using term values as encoded Floats. Sort values are Float and lower values are at the front.

  See Also:
    Constant Field Values

### LONG

    public static final int LONG

  Sort using term values as encoded Longs. Sort values are Long and lower values are at the front.

  See Also:
    Constant Field Values

### DOUBLE

    public static final int DOUBLE

  Sort using term values as encoded Doubles. Sort values are Double and lower values are at the front.

  See Also:
    Constant Field Values

### SHORT

    public static final int SHORT

  Sort using term values as encoded Shorts. Sort values are Short and lower values are at the front.

  See Also:
    Constant Field Values

### CUSTOM

    public static final int CUSTOM

  Sort using a custom Comparator. Sort values are any Comparable and sorting is done according to natural order.

  See Also:
    Constant Field Values

### BYTE

    public static final int BYTE

  Sort using term values as encoded Bytes. Sort values are Byte and lower values are at the front.

  See Also:
    Constant Field Values

### STRING_VAL

    public static final int STRING_VAL

  Sort using term values as Strings, but comparing by value (using String.compareTo) for all comparisons. This is typically slower than `STRING`, which uses ordinals to do the sorting.

  See Also:
    Constant Field Values

### FIELD_SCORE

    public static final SortField FIELD_SCORE

  Represents sorting by document score (relevancy).

### FIELD_DOC

    public static final SortField FIELD_DOC

  Represents sorting by document number (index order).

Constructor Detail

### SortField

    public SortField(java.lang.String field)

  Deprecated. Please specify the exact type instead.

  Creates a sort by terms in the given field where the type of term value is determined dynamically (`AUTO`).

  Parameters:
    `field` - Name of field to sort by, cannot be `null`.

### SortField

    public SortField(java.lang.String field,
                     boolean reverse)

  Deprecated. Please specify the exact type instead.

  Creates a sort, possibly in reverse, by terms in the given field where the type of term value is determined dynamically (`AUTO`).

  Parameters:
    `field` - Name of field to sort by, cannot be `null`.
    `reverse` - True if natural order should be reversed.

### SortField

    public SortField(java.lang.String field,
                     int 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,
                     int 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,
                     java.util.Locale locale)

  Creates a sort by terms in the given field sorted according to the given locale.

  Parameters:
    `field` - Name of field to sort by, cannot be `null`.
    `locale` - Locale of values in the field.

### SortField

    public SortField(java.lang.String field,
                     java.util.Locale locale,
                     boolean reverse)

  Creates a sort, possibly in reverse, by terms in the given field sorted according to the given locale.

  Parameters:
    `field` - Name of field to sort by, cannot be `null`.
    `locale` - Locale of values in the field.

### SortField

    public SortField(java.lang.String field,
                     SortComparatorSource comparator)

  Deprecated. use SortField (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)

  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,
                     SortComparatorSource comparator,
                     boolean reverse)

  Deprecated. use 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 be `null`.
    `comparator` - Returns a comparator for sorting hits.
    `reverse` - True if natural order should be reversed.

### 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

### 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 int getType()

  Returns the type of contents in the field.

  Returns:
    One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.

### getLocale

    public java.util.Locale getLocale()

  Returns the Locale by which term values are interpreted. May return `null` if no Locale was specified.

  Returns:
    Locale, or `null`.

### 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.

### getFactory

    public SortComparatorSource getFactory()

  Deprecated. use `getComparatorSource()`

### getComparatorSource

    public FieldComparatorSource getComparatorSource()

### setUseLegacySearch

    public void setUseLegacySearch(boolean legacy)

  Deprecated. will be removed in Lucene 3.0.

  Use legacy IndexSearch implementation: search with a DirectoryReader rather than passing a single hit collector to multiple SegmentReaders.

  Parameters:
    `legacy` - true for legacy behavior

### getUseLegacySearch

    public boolean getUseLegacySearch()

  Deprecated. will be removed in Lucene 3.0.

  Returns:
    if true, IndexSearch will use legacy sorting search implementation. eg. multiple Priority Queues.

### 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 `SortComparatorSource` (deprecated) 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 `SortComparatorSource` (deprecated) or `FieldCache.Parser` was provided, it must properly implement hashCode (unless a singleton is always used).

  Overrides:
    `hashCode` in class `java.lang.Object`

### getComparator

    public FieldComparator getComparator(int numHits,
                                         int sortPos)
                                  throws java.io.IOException

  Returns the `FieldComparator` to use for sorting. NOTE: This API is experimental and might change in incompatible ways in the next release.

  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`

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.