FileDocCategorySizeDatePackage
SortField.javaAPI DocApache Lucene 1.97836Mon Feb 20 09:20:04 GMT 2006org.apache.lucene.search

SortField

public class SortField extends Object implements 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

author
Tim Jones (Nacimiento Software)
since
lucene 1.4
version
$Id: SortField.java 150357 2004-05-24 22:51:42Z tjones $
see
Sort

Fields Summary
public static final int
SCORE
Sort by document score (relevancy). Sort values are Float and higher values are at the front.
public static final int
DOC
Sort by document number (index order). Sort values are Integer and lower values are at the front.
public static final int
AUTO
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.
public static final int
STRING
Sort using term values as Strings. Sort values are String and lower values are at the front.
public static final int
INT
Sort using term values as encoded Integers. Sort values are Integer and lower values are at the front.
public static final int
FLOAT
Sort using term values as encoded Floats. Sort values are Float and lower values are at the front.
public static final int
CUSTOM
Sort using a custom Comparator. Sort values are any Comparable and sorting is done according to natural order.
public static final SortField
FIELD_SCORE
Represents sorting by document score (relevancy).
public static final SortField
FIELD_DOC
Represents sorting by document number (index order).
private String
field
private int
type
private Locale
locale
boolean
reverse
private SortComparatorSource
factory
Constructors Summary
public SortField(String field)
Creates a sort by terms in the given field where the type of term value is determined dynamically ({@link #AUTO AUTO}).

param
field Name of field to sort by, cannot be null.


                                     
      
    this.field = field.intern();
  
public SortField(String field, boolean reverse)
Creates a sort, possibly in reverse, by terms in the given field where the type of term value is determined dynamically ({@link #AUTO AUTO}).

param
field Name of field to sort by, cannot be null.
param
reverse True if natural order should be reversed.

    this.field = field.intern();
    this.reverse = reverse;
  
public SortField(String field, int type)
Creates a sort by terms in the given field with the type of term values explicitly given.

param
field Name of field to sort by. Can be null if type is SCORE or DOC.
param
type Type of values in the terms.

    this.field = (field != null) ? field.intern() : field;
    this.type = type;
  
public SortField(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.

param
field Name of field to sort by. Can be null if type is SCORE or DOC.
param
type Type of values in the terms.
param
reverse True if natural order should be reversed.

    this.field = (field != null) ? field.intern() : field;
    this.type = type;
    this.reverse = reverse;
  
public SortField(String field, Locale locale)
Creates a sort by terms in the given field sorted according to the given locale.

param
field Name of field to sort by, cannot be null.
param
locale Locale of values in the field.

    this.field = field.intern();
    this.type = STRING;
    this.locale = locale;
  
public SortField(String field, Locale locale, boolean reverse)
Creates a sort, possibly in reverse, by terms in the given field sorted according to the given locale.

param
field Name of field to sort by, cannot be null.
param
locale Locale of values in the field.

    this.field = field.intern();
    this.type = STRING;
    this.locale = locale;
    this.reverse = reverse;
  
public SortField(String field, SortComparatorSource comparator)
Creates a sort with a custom comparison function.

param
field Name of field to sort by; cannot be null.
param
comparator Returns a comparator for sorting hits.

    this.field = (field != null) ? field.intern() : field;
    this.type = CUSTOM;
    this.factory = comparator;
  
public SortField(String field, SortComparatorSource comparator, boolean reverse)
Creates a sort, possibly in reverse, with a custom comparison function.

param
field Name of field to sort by; cannot be null.
param
comparator Returns a comparator for sorting hits.
param
reverse True if natural order should be reversed.

    this.field = (field != null) ? field.intern() : field;
    this.type = CUSTOM;
    this.reverse = reverse;
    this.factory = comparator;
  
Methods Summary
public org.apache.lucene.search.SortComparatorSourcegetFactory()

    return factory;
  
public java.lang.StringgetField()
Returns the name of the field. Could return null if the sort is by SCORE or DOC.

return
Name of field, possibly null.

    return field;
  
public java.util.LocalegetLocale()
Returns the Locale by which term values are interpreted. May return null if no Locale was specified.

return
Locale, or null.

    return locale;
  
public booleangetReverse()
Returns whether the sort should be reversed.

return
True if natural order should be reversed.

    return reverse;
  
public intgetType()
Returns the type of contents in the field.

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

    return type;
  
public java.lang.StringtoString()

    StringBuffer buffer = new StringBuffer();
    switch (type) {
      case SCORE: buffer.append("<score>");
                  break;

      case DOC: buffer.append("<doc>");
                break;

      case CUSTOM: buffer.append ("<custom:\"" + field + "\": "
                                               + factory + ">");
                break;

      default: buffer.append("\"" + field + "\"");
               break;
    }

    if (locale != null) buffer.append ("("+locale+")");
    if (reverse) buffer.append('!");

    return buffer.toString();