Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class Sort

    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", x.ToString(CultureInfo.InvariantCulture), 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: ints, 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.

    int term values should contain only digits and an optional preceding negative sign. Values must be base 10 and in the range MinValue and MaxValue 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 MinValue and MaxValue 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 (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 comparable natural order (Ordinal). 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 int 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
    Inheritance
    object
    Sort
    Inherited Members
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: Lucene.Net.Search
    Assembly: Lucene.Net.dll
    Syntax
    public class Sort

    Constructors

    Sort()

    Sorts by computed relevance. This is the same sort criteria as calling Search(Query, int) without a sort criteria, only with slightly more overhead.

    Declaration
    public Sort()

    Sort(SortField)

    Sorts by the criteria in the given SortField.

    Declaration
    public Sort(SortField field)
    Parameters
    Type Name Description
    SortField field

    Sort(params SortField[])

    Sorts in succession by the criteria in each SortField.

    Declaration
    public Sort(params SortField[] fields)
    Parameters
    Type Name Description
    SortField[] fields

    Fields

    INDEXORDER

    Represents sorting by index order.

    Declaration
    public static readonly Sort INDEXORDER
    Field Value
    Type Description
    Sort

    RELEVANCE

    Represents sorting by computed relevance. Using this sort criteria returns the same results as calling Search(Query, int)without a sort criteria, only with slightly more overhead.

    Declaration
    public static readonly Sort RELEVANCE
    Field Value
    Type Description
    Sort

    Properties

    NeedsScores

    Returns true if the relevance score is needed to sort documents.

    Declaration
    public virtual bool NeedsScores { get; }
    Property Value
    Type Description
    bool

    Methods

    Equals(object)

    Returns true if o is equal to this.

    Declaration
    public override bool Equals(object o)
    Parameters
    Type Name Description
    object o
    Returns
    Type Description
    bool
    Overrides
    object.Equals(object)

    GetHashCode()

    Returns a hash code value for this object.

    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    int
    Overrides
    object.GetHashCode()

    GetSort()

    Representation of the sort criteria.

    Declaration
    public virtual SortField[] GetSort()
    Returns
    Type Description
    SortField[]

    Array of SortField objects used in this sort criteria

    Rewrite(IndexSearcher)

    Rewrites the SortFields in this Sort, returning a new Sort if any of the fields changes during their rewriting.

    Note

    This API is experimental and might change in incompatible ways in the next release.

    Declaration
    public virtual Sort Rewrite(IndexSearcher searcher)
    Parameters
    Type Name Description
    IndexSearcher searcher

    IndexSearcher to use in the rewriting

    Returns
    Type Description
    Sort

    this if the Sort/Fields have not changed, or a new Sort if there is a change

    Exceptions
    Type Condition
    IOException

    Can be thrown by the rewriting

    SetSort(SortField)

    Sets the sort to the given criteria.

    NOTE: When overriding this method, be aware that the constructor of this class calls a private method and not this virtual method. So if you need to override the behavior during the initialization, call your own private method from the constructor with whatever custom behavior you need.

    Declaration
    public virtual void SetSort(SortField field)
    Parameters
    Type Name Description
    SortField field

    SetSort(params SortField[])

    Sets the sort to the given criteria in succession.

    NOTE: When overriding this method, be aware that the constructor of this class calls a private method and not this virtual method. So if you need to override the behavior during the initialization, call your own private method from the constructor with whatever custom behavior you need.

    Declaration
    public virtual void SetSort(params SortField[] fields)
    Parameters
    Type Name Description
    SortField[] fields

    ToString()

    Returns a string that represents the current object.

    Declaration
    public override string ToString()
    Returns
    Type Description
    string

    A string that represents the current object.

    Overrides
    object.ToString()
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.