Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.lucene.search
Class Sort

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

All Implemented Interfaces:
  Serializable

    public class Sort

extends Object
implements Serializable

Encapsulates sort criteria for returned hits.

The fields used to determine sort order must be carefully chosen. Documents must contain a single term in such a field, and the value of the term should indicate the document's relative position in a given sort order. The field must be indexed, but should not be tokenized, and does not need to be stored (unless you happen to want it back with the rest of your document data). In other words:

`document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));`

### Valid Types of Values

There are four possible kinds of term values which may be put into sorting fields: Integers, Longs, Floats, or Strings. Unless `SortField` objects are specified, the type of value in the field is determined by parsing the first term in the field.

Integer term values should contain only digits and an optional preceding negative sign. Values must be base 10 and in the range `Integer.MIN_VALUE` and `Integer.MAX_VALUE` inclusive. Documents which should appear first in the sort should have low value integers, later documents high values (i.e. the documents should be numbered `1..n` where `1` is the first and `n` the last).

Long term values should contain only digits and an optional preceding negative sign. Values must be base 10 and in the range `Long.MIN_VALUE` and `Long.MAX_VALUE` inclusive. Documents which should appear first in the sort should have low value integers, later documents high values.

Float term values should conform to values accepted by `Float.valueOf(String)` (except that `NaN` and `Infinity` are not supported). Documents which should appear first in the sort should have low values, later documents high values.

String term values can contain any valid String, but should not be tokenized. The values are sorted according to their `natural order`. Note that using this type of term value has higher memory requirements than the other two types.

### Object Reuse

One of these objects can be used multiple times and the sort order changed between usages.

This class is thread safe.

### Memory Usage

Sorting uses of caches of term values maintained by the internal HitQueue(s). The cache is static and contains an integer or float array of length `IndexReader.maxDoc()` for each field name for which a sort is performed. In other words, the size of the cache in bytes is:

`4 * IndexReader.maxDoc() * (# of different fields actually used to sort)`

For String fields, the cache is larger: in addition to the above array, the value of every term in the field is kept in memory. If there are many unique terms in the field, this could be quite large.

Note that the size of the cache is not affected by how many fields are in the index and might be used to sort - only by the ones actually used to sort a result set.

Created: Feb 12, 2004 10:53:57 AM

Since:
  lucene 1.4
Version:
  $Id: Sort.java 695514 2008-09-15 15:42:11Z otis $
See Also:
  Serialized Form

Field Summary
`static Sort` | `INDEXORDER` Represents sorting by index order.
`static Sort` | `RELEVANCE` Represents sorting by computed relevance.

Constructor Summary
`Sort()` Sorts by computed relevance.
`Sort(SortField field)` Sorts by the criteria in the given SortField.
`Sort(SortField[] fields)` Sorts in succession by the criteria in each SortField.
`Sort(String field)` Sorts by the terms in `field` then by index order (document number).
`Sort(String[] fields)` Sorts in succession by the terms in each field.
`Sort(String field, boolean reverse)` Sorts possibly in reverse by the terms in `field` then by index order (document number).

Method Summary
`SortField[]` | `getSort()` Representation of the sort criteria.
`void` | `setSort(SortField field)` Sets the sort to the given criteria.
`void` | `setSort(SortField[] fields)` Sets the sort to the given criteria in succession.
`void` | `setSort(String field)` Sets the sort to the terms in `field` then by index order (document number).
`void` | `setSort(String[] fieldnames)` Sets the sort to the terms in each field in succession.
`void` | `setSort(String field, boolean reverse)` Sets the sort to the terms in `field` possibly in reverse, then by index order (document number).
`String` | `toString()`

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

Field Detail

### RELEVANCE

    public static final Sort RELEVANCE

  Represents sorting by computed relevance. Using this sort criteria returns the same results as calling `Searcher#search()`without a sort criteria, only with slightly more overhead.

### INDEXORDER

    public static final Sort INDEXORDER

  Represents sorting by index order.

Constructor Detail

### Sort

    public Sort()

  Sorts by computed relevance. This is the same sort criteria as calling `Searcher#search()`without a sort criteria, only with slightly more overhead.

### Sort

    public Sort(String field)

  Sorts by the terms in `field` then by index order (document number). The type of value in `field` is determined automatically.

  See Also:
    `SortField.AUTO`

### Sort

    public Sort(String field,
                boolean reverse)

  Sorts possibly in reverse by the terms in `field` then by index order (document number). The type of value in `field` is determined automatically.

  See Also:
    `SortField.AUTO`

### Sort

    public Sort(String[] fields)

  Sorts in succession by the terms in each field. The type of value in `field` is determined automatically.

  See Also:
    `SortField.AUTO`

### Sort

    public Sort(SortField field)

  Sorts by the criteria in the given SortField.

### Sort

    public Sort(SortField[] fields)

  Sorts in succession by the criteria in each SortField.

Method Detail

### setSort

    public final void setSort(String field)

  Sets the sort to the terms in `field` then by index order (document number).

### setSort

    public void setSort(String field,
                        boolean reverse)

  Sets the sort to the terms in `field` possibly in reverse, then by index order (document number).

### setSort

    public void setSort(String[] fieldnames)

  Sets the sort to the terms in each field in succession.

### setSort

    public void setSort(SortField field)

  Sets the sort to the given criteria.

### setSort

    public void setSort(SortField[] fields)

  Sets the sort to the given criteria in succession.

### getSort

    public SortField[] getSort()

  Representation of the sort criteria.

  Returns:
    Array of SortField objects used in this sort criteria

### toString

    public String toString()

  Overrides:
    `toString` in class `Object`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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