2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
23 /// <p>Created: Feb 11, 2004 1:25:29 PM
26 /// <author> Tim Jones (Nacimiento Software)
28 /// <since> lucene 1.4
30 /// <version> $Id: SortField.cs,v 1.1 2005/01/17 19:54:30 joeshaw Exp $
32 /// <seealso cref="Sort">
35 public class SortField
38 /// <summary>Sort by document score (relevancy). Sort values are Float and higher
39 /// values are at the front.
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.
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.
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.
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.
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.
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.
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}).
96 /// <param name="Field">Name of Field to sort by, cannot be <code>null</code>.
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}).
106 /// <param name="Field">Name of Field to sort by, cannot be <code>null</code>.
108 /// <param name="reverse">True if natural order should be reversed.
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.
119 /// <param name="Field"> Name of Field to sort by. Can be <code>null</code> if
120 /// <code>type</code> is SCORE or DOC.
122 /// <param name="type"> Type of values in the terms.
124 public SortField(System
.String field
, int type
)
126 this.field
= (field
!= null)?String
.Intern(field
):field
;
130 /// <summary>Creates a sort, possibly in reverse, by terms in the given Field with the
131 /// type of term values explicitly given.
133 /// <param name="Field"> Name of Field to sort by. Can be <code>null</code> if
134 /// <code>type</code> is SCORE or DOC.
136 /// <param name="type"> Type of values in the terms.
138 /// <param name="reverse">True if natural order should be reversed.
140 public SortField(System
.String field
, int type
, bool reverse
)
142 this.field
= (field
!= null) ? String
.Intern(field
) : field
;
144 this.reverse
= reverse
;
147 /// <summary>Creates a sort by terms in the given Field sorted
148 /// according to the given locale.
150 /// <param name="Field"> Name of Field to sort by, cannot be <code>null</code>.
152 /// <param name="locale">Locale of values in the Field.
154 public SortField(System
.String field
, System
.Globalization
.CultureInfo locale
)
156 this.field
= String
.Intern(field
);
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.
164 /// <param name="Field"> Name of Field to sort by, cannot be <code>null</code>.
166 /// <param name="locale">Locale of values in the Field.
168 public SortField(System
.String field
, System
.Globalization
.CultureInfo locale
, bool reverse
)
170 this.field
= String
.Intern(field
);
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>.
179 /// <param name="comparator">Returns a comparator for sorting hits.
181 public SortField(System
.String field
, SortComparatorSource comparator
)
183 this.field
= (field
!= null)?String
.Intern(field
):field
;
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>.
191 /// <param name="comparator">Returns a comparator for sorting hits.
193 /// <param name="reverse">True if natural order should be reversed.
195 public SortField(System
.String field
, SortComparatorSource comparator
, bool reverse
)
197 this.field
= (field
!= null)?String
.Intern(field
):field
;
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.
206 /// <returns> Name of Field, possibly <code>null</code>.
208 public virtual System
.String
GetField()
213 /// <summary>Returns the type of contents in the Field.</summary>
214 /// <returns> One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.
216 new public virtual int GetType()
221 /// <summary>Returns the Locale by which term values are interpreted.
222 /// May return <code>null</code> if no Locale was specified.
224 /// <returns> Locale, or <code>null</code>.
226 public virtual System
.Globalization
.CultureInfo
GetLocale()
231 /// <summary>Returns whether the sort should be reversed.</summary>
232 /// <returns> True if natural order should be reversed.
234 public virtual bool GetReverse()
239 public virtual SortComparatorSource
GetFactory()
244 public override System
.String
ToString()
246 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
250 case SCORE
: buffer
.Append("<score>");
254 case DOC
: buffer
.Append("<doc>");
259 buffer
.Append("<custom:\"" + field
+ "\": " + factory
+ ">");
263 default: buffer
.Append("\"" + field
+ "\"");
269 buffer
.Append("(" + locale
+ ")");
273 return buffer
.ToString();