Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / SortField.cs
blob4e9e41d373663199f01fe8b91a67b4161a44e197
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 namespace Lucene.Net.Search
20 /// <summary> Stores information about how to sort documents by terms in an individual
21 /// Field. Fields must be indexed in order to sort by them.
22 ///
23 /// <p>Created: Feb 11, 2004 1:25:29 PM
24 ///
25 /// </summary>
26 /// <author> Tim Jones (Nacimiento Software)
27 /// </author>
28 /// <since> lucene 1.4
29 /// </since>
30 /// <version> $Id: SortField.cs,v 1.1 2005/01/17 19:54:30 joeshaw Exp $
31 /// </version>
32 /// <seealso cref="Sort">
33 /// </seealso>
34 [Serializable]
35 public class SortField
38 /// <summary>Sort by document score (relevancy). Sort values are Float and higher
39 /// values are at the front.
40 /// </summary>
41 public const int SCORE = 0;
43 /// <summary>Sort by document number (index order). Sort values are Integer and lower
44 /// values are at the front.
45 /// </summary>
46 public const int DOC = 1;
48 /// <summary>Guess type of sort based on Field contents. A regular expression is used
49 /// to look at the first term indexed for the Field and determine if it
50 /// represents an integer number, a floating point number, or just arbitrary
51 /// string characters.
52 /// </summary>
53 public const int AUTO = 2;
55 /// <summary>Sort using term values as Strings. Sort values are String and lower
56 /// values are at the front.
57 /// </summary>
58 public const int STRING = 3;
60 /// <summary>Sort using term values as encoded Integers. Sort values are Integer and
61 /// lower values are at the front.
62 /// </summary>
63 public const int INT = 4;
65 /// <summary>Sort using term values as encoded Floats. Sort values are Float and
66 /// lower values are at the front.
67 /// </summary>
68 public const int FLOAT = 5;
70 /// <summary>Sort using a custom Comparator. Sort values are any Comparable and
71 /// sorting is done according to natural order.
72 /// </summary>
73 public const int CUSTOM = 9;
75 // IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace"
76 // as the above static int values. Any new values must not have the same value
77 // as FieldCache.STRING_INDEX.
80 /// <summary>Represents sorting by document score (relevancy). </summary>
81 public static readonly SortField FIELD_SCORE = new SortField(null, SCORE);
83 /// <summary>Represents sorting by document number (index order). </summary>
84 public static readonly SortField FIELD_DOC = new SortField(null, DOC);
87 private System.String field;
88 private int type = AUTO; // defaults to determining type dynamically
89 private System.Globalization.CultureInfo locale; // defaults to "natural order" (no Locale)
90 internal bool reverse = false; // defaults to natural order
91 private SortComparatorSource factory;
93 /// <summary>Creates a sort by terms in the given Field where the type of term value
94 /// is determined dynamically ({@link #AUTO AUTO}).
95 /// </summary>
96 /// <param name="Field">Name of Field to sort by, cannot be <code>null</code>.
97 /// </param>
98 public SortField(System.String field)
100 this.field = String.Intern(field);
103 /// <summary>Creates a sort, possibly in reverse, by terms in the given Field where
104 /// the type of term value is determined dynamically ({@link #AUTO AUTO}).
105 /// </summary>
106 /// <param name="Field">Name of Field to sort by, cannot be <code>null</code>.
107 /// </param>
108 /// <param name="reverse">True if natural order should be reversed.
109 /// </param>
110 public SortField(System.String field, bool reverse)
112 this.field = String.Intern(field);
113 this.reverse = reverse;
116 /// <summary>Creates a sort by terms in the given Field with the type of term
117 /// values explicitly given.
118 /// </summary>
119 /// <param name="Field"> Name of Field to sort by. Can be <code>null</code> if
120 /// <code>type</code> is SCORE or DOC.
121 /// </param>
122 /// <param name="type"> Type of values in the terms.
123 /// </param>
124 public SortField(System.String field, int type)
126 this.field = (field != null)?String.Intern(field):field;
127 this.type = type;
130 /// <summary>Creates a sort, possibly in reverse, by terms in the given Field with the
131 /// type of term values explicitly given.
132 /// </summary>
133 /// <param name="Field"> Name of Field to sort by. Can be <code>null</code> if
134 /// <code>type</code> is SCORE or DOC.
135 /// </param>
136 /// <param name="type"> Type of values in the terms.
137 /// </param>
138 /// <param name="reverse">True if natural order should be reversed.
139 /// </param>
140 public SortField(System.String field, int type, bool reverse)
142 this.field = (field != null) ? String.Intern(field) : field;
143 this.type = type;
144 this.reverse = reverse;
147 /// <summary>Creates a sort by terms in the given Field sorted
148 /// according to the given locale.
149 /// </summary>
150 /// <param name="Field"> Name of Field to sort by, cannot be <code>null</code>.
151 /// </param>
152 /// <param name="locale">Locale of values in the Field.
153 /// </param>
154 public SortField(System.String field, System.Globalization.CultureInfo locale)
156 this.field = String.Intern(field);
157 this.type = STRING;
158 this.locale = locale;
161 /// <summary>Creates a sort, possibly in reverse, by terms in the given Field sorted
162 /// according to the given locale.
163 /// </summary>
164 /// <param name="Field"> Name of Field to sort by, cannot be <code>null</code>.
165 /// </param>
166 /// <param name="locale">Locale of values in the Field.
167 /// </param>
168 public SortField(System.String field, System.Globalization.CultureInfo locale, bool reverse)
170 this.field = String.Intern(field);
171 this.type = STRING;
172 this.locale = locale;
173 this.reverse = reverse;
176 /// <summary>Creates a sort with a custom comparison function.</summary>
177 /// <param name="Field">Name of Field to sort by; cannot be <code>null</code>.
178 /// </param>
179 /// <param name="comparator">Returns a comparator for sorting hits.
180 /// </param>
181 public SortField(System.String field, SortComparatorSource comparator)
183 this.field = (field != null)?String.Intern(field):field;
184 this.type = CUSTOM;
185 this.factory = comparator;
188 /// <summary>Creates a sort, possibly in reverse, with a custom comparison function.</summary>
189 /// <param name="Field">Name of Field to sort by; cannot be <code>null</code>.
190 /// </param>
191 /// <param name="comparator">Returns a comparator for sorting hits.
192 /// </param>
193 /// <param name="reverse">True if natural order should be reversed.
194 /// </param>
195 public SortField(System.String field, SortComparatorSource comparator, bool reverse)
197 this.field = (field != null)?String.Intern(field):field;
198 this.type = CUSTOM;
199 this.reverse = reverse;
200 this.factory = comparator;
203 /// <summary>Returns the name of the Field. Could return <code>null</code>
204 /// if the sort is by SCORE or DOC.
205 /// </summary>
206 /// <returns> Name of Field, possibly <code>null</code>.
207 /// </returns>
208 public virtual System.String GetField()
210 return field;
213 /// <summary>Returns the type of contents in the Field.</summary>
214 /// <returns> One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.
215 /// </returns>
216 new public virtual int GetType()
218 return type;
221 /// <summary>Returns the Locale by which term values are interpreted.
222 /// May return <code>null</code> if no Locale was specified.
223 /// </summary>
224 /// <returns> Locale, or <code>null</code>.
225 /// </returns>
226 public virtual System.Globalization.CultureInfo GetLocale()
228 return locale;
231 /// <summary>Returns whether the sort should be reversed.</summary>
232 /// <returns> True if natural order should be reversed.
233 /// </returns>
234 public virtual bool GetReverse()
236 return reverse;
239 public virtual SortComparatorSource GetFactory()
241 return factory;
244 public override System.String ToString()
246 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
247 switch (type)
250 case SCORE: buffer.Append("<score>");
251 break;
254 case DOC: buffer.Append("<doc>");
255 break;
258 case CUSTOM:
259 buffer.Append("<custom:\"" + field + "\": " + factory + ">");
260 break;
263 default: buffer.Append("\"" + field + "\"");
264 break;
268 if (locale != null)
269 buffer.Append("(" + locale + ")");
270 if (reverse)
271 buffer.Append('!');
273 return buffer.ToString();