/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace Lucene.Net.Search
{
/// Stores information about how to sort documents by terms in an individual
/// field. Fields must be indexed in order to sort by them.
///
/// lucene 1.4
///
/// $Id: SortField.java 598296 2007-11-26 14:52:01Z mikemccand $
///
///
///
[Serializable]
public class SortField
{
/// Sort by document score (relevancy). Sort values are Float and higher
/// values are at the front.
///
public const int SCORE = 0;
/// Sort by document number (index order). Sort values are Integer and lower
/// values are at the front.
///
public const int DOC = 1;
/// 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 const int AUTO = 2;
/// Sort using term values as Strings. Sort values are String and lower
/// values are at the front.
///
public const int STRING = 3;
/// Sort using term values as encoded Integers. Sort values are Integer and
/// lower values are at the front.
///
public const int INT = 4;
/// Sort using term values as encoded Floats. Sort values are Float and
/// lower values are at the front.
///
public const int FLOAT = 5;
/// Sort using term values as encoded Longs. Sort values are Long and
/// lower values are at the front.
///
public const int LONG = 6;
/// Sort using term values as encoded Doubles. Sort values are Double and
/// lower values are at the front.
///
public const int DOUBLE = 7;
///
/// Sort using term values as encoded shorts.
/// Sort values are shorts and lower values are at the front.
///
public const int SHORT = 8;
/// Sort using a custom Comparator. Sort values are any Comparable and
/// sorting is done according to natural order.
///
public const int CUSTOM = 9;
///
/// Sort using term values as encoded bytes.
/// Sort values are bytes and lower values are at the front.
///
public const int BYTE = 10;
// IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace"
// as the above static int values. Any new values must not have the same value
// as FieldCache.STRING_INDEX.
/// Represents sorting by document score (relevancy).
public static readonly SortField FIELD_SCORE = new SortField(null, SCORE);
/// Represents sorting by document number (index order).
public static readonly SortField FIELD_DOC = new SortField(null, DOC);
private System.String field;
private int type = AUTO; // defaults to determining type dynamically
private System.Globalization.CultureInfo locale; // defaults to "natural order" (no Locale)
internal bool reverse = false; // defaults to natural order
private SortComparatorSource factory;
/// Creates a sort by terms in the given field where the type of term value
/// is determined dynamically ({@link #AUTO AUTO}).
///
/// Name of field to sort by, cannot be null
.
///
public SortField(System.String field)
{
this.field = String.Intern(field);
}
/// Creates a sort, possibly in reverse, by terms in the given field where
/// the type of term value is determined dynamically ({@link #AUTO AUTO}).
///
/// Name of field to sort by, cannot be null
.
///
/// True if natural order should be reversed.
///
public SortField(System.String field, bool reverse)
{
this.field = String.Intern(field);
this.reverse = reverse;
}
/// Creates a sort by terms in the given field with the type of term
/// values explicitly given.
///
/// Name of field to sort by. Can be null
if
/// type
is SCORE or DOC.
///
/// Type of values in the terms.
///
public SortField(System.String field, int type)
{
this.field = (field != null) ? String.Intern(field) : field;
this.type = type;
}
/// Creates a sort, possibly in reverse, by terms in the given field with the
/// type of term values explicitly given.
///
/// Name of field to sort by. Can be null
if
/// type
is SCORE or DOC.
///
/// Type of values in the terms.
///
/// True if natural order should be reversed.
///
public SortField(System.String field, int type, bool reverse)
{
this.field = (field != null) ? String.Intern(field) : field;
this.type = type;
this.reverse = reverse;
}
/// Creates a sort by terms in the given field sorted
/// according to the given locale.
///
/// Name of field to sort by, cannot be null
.
///
/// Locale of values in the field.
///
public SortField(System.String field, System.Globalization.CultureInfo locale)
{
this.field = String.Intern(field);
this.type = STRING;
this.locale = locale;
}
/// Creates a sort, possibly in reverse, by terms in the given field sorted
/// according to the given locale.
///
/// Name of field to sort by, cannot be null
.
///
/// Locale of values in the field.
///
public SortField(System.String field, System.Globalization.CultureInfo locale, bool reverse)
{
this.field = String.Intern(field);
this.type = STRING;
this.locale = locale;
this.reverse = reverse;
}
/// Creates a sort with a custom comparison function.
/// Name of field to sort by; cannot be null
.
///
/// Returns a comparator for sorting hits.
///
public SortField(System.String field, SortComparatorSource comparator)
{
this.field = (field != null) ? String.Intern(field) : field;
this.type = CUSTOM;
this.factory = comparator;
}
/// Creates a sort, possibly in reverse, with a custom comparison function.
/// Name of field to sort by; cannot be null
.
///
/// Returns a comparator for sorting hits.
///
/// True if natural order should be reversed.
///
public SortField(System.String field, SortComparatorSource comparator, bool reverse)
{
this.field = (field != null) ? String.Intern(field) : field;
this.type = CUSTOM;
this.reverse = reverse;
this.factory = comparator;
}
/// Returns the name of the field. Could return null
/// if the sort is by SCORE or DOC.
///
/// Name of field, possibly null
.
///
public virtual System.String GetField()
{
return field;
}
/// Returns the type of contents in the field.
/// One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.
///
public new virtual int GetType()
{
return type;
}
/// Returns the Locale by which term values are interpreted.
/// May return null
if no Locale was specified.
///
/// Locale, or null
.
///
public virtual System.Globalization.CultureInfo GetLocale()
{
return locale;
}
/// Returns whether the sort should be reversed.
/// True if natural order should be reversed.
///
public virtual bool GetReverse()
{
return reverse;
}
public virtual SortComparatorSource GetFactory()
{
return factory;
}
public override System.String ToString()
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
switch (type)
{
case SCORE: buffer.Append("");
break;
case DOC: buffer.Append("");
break;
case CUSTOM:
buffer.Append("');
break;
default:
buffer.Append('\"').Append(field).Append('\"');
break;
}
if (locale != null)
buffer.Append('(').Append(locale).Append(')');
if (reverse)
buffer.Append('!');
return buffer.ToString();
}
}
}