001 package gate.creole.annic.apache.lucene.search;
002
003 /**
004 * Copyright 2004 The Apache Software Foundation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 import java.io.Serializable;
020 import java.util.Locale;
021
022 /**
023 * Stores information about how to sort documents by terms in an individual
024 * field. Fields must be indexed in order to sort by them.
025 *
026 * <p>Created: Feb 11, 2004 1:25:29 PM
027 *
028 * @author Tim Jones (Nacimiento Software)
029 * @since lucene 1.4
030 * @version $Id: SortField.java 529 2004-10-05 11:55:26Z niraj $
031 * @see Sort
032 */
033 public class SortField
034 implements Serializable {
035
036 /** Sort by document score (relevancy). Sort values are Float and higher
037 * values are at the front. */
038 public static final int SCORE = 0;
039
040 /** Sort by document number (index order). Sort values are Integer and lower
041 * values are at the front. */
042 public static final int DOC = 1;
043
044 /** Guess type of sort based on field contents. A regular expression is used
045 * to look at the first term indexed for the field and determine if it
046 * represents an integer number, a floating point number, or just arbitrary
047 * string characters. */
048 public static final int AUTO = 2;
049
050 /** Sort using term values as Strings. Sort values are String and lower
051 * values are at the front. */
052 public static final int STRING = 3;
053
054 /** Sort using term values as encoded Integers. Sort values are Integer and
055 * lower values are at the front. */
056 public static final int INT = 4;
057
058 /** Sort using term values as encoded Floats. Sort values are Float and
059 * lower values are at the front. */
060 public static final int FLOAT = 5;
061
062 /** Sort using a custom Comparator. Sort values are any Comparable and
063 * sorting is done according to natural order. */
064 public static final int CUSTOM = 9;
065
066 // IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace"
067 // as the above static int values. Any new values must not have the same value
068 // as FieldCache.STRING_INDEX.
069
070
071 /** Represents sorting by document score (relevancy). */
072 public static final SortField FIELD_SCORE = new SortField (null, SCORE);
073
074 /** Represents sorting by document number (index order). */
075 public static final SortField FIELD_DOC = new SortField (null, DOC);
076
077
078 private String field;
079 private int type = AUTO; // defaults to determining type dynamically
080 private Locale locale; // defaults to "natural order" (no Locale)
081 boolean reverse = false; // defaults to natural order
082 private SortComparatorSource factory;
083
084 /** Creates a sort by terms in the given field where the type of term value
085 * is determined dynamically ({@link #AUTO AUTO}).
086 * @param field Name of field to sort by, cannot be <code>null</code>.
087 */
088 public SortField (String field) {
089 this.field = field.intern();
090 }
091
092 /** Creates a sort, possibly in reverse, by terms in the given field where
093 * the type of term value is determined dynamically ({@link #AUTO AUTO}).
094 * @param field Name of field to sort by, cannot be <code>null</code>.
095 * @param reverse True if natural order should be reversed.
096 */
097 public SortField (String field, boolean reverse) {
098 this.field = field.intern();
099 this.reverse = reverse;
100 }
101
102 /** Creates a sort by terms in the given field with the type of term
103 * values explicitly given.
104 * @param field Name of field to sort by. Can be <code>null</code> if
105 * <code>type</code> is SCORE or DOC.
106 * @param type Type of values in the terms.
107 */
108 public SortField (String field, int type) {
109 this.field = (field != null) ? field.intern() : field;
110 this.type = type;
111 }
112
113 /** Creates a sort, possibly in reverse, by terms in the given field with the
114 * type of term values explicitly given.
115 * @param field Name of field to sort by. Can be <code>null</code> if
116 * <code>type</code> is SCORE or DOC.
117 * @param type Type of values in the terms.
118 * @param reverse True if natural order should be reversed.
119 */
120 public SortField (String field, int type, boolean reverse) {
121 this.field = (field != null) ? field.intern() : field;
122 this.type = type;
123 this.reverse = reverse;
124 }
125
126 /** Creates a sort by terms in the given field sorted
127 * according to the given locale.
128 * @param field Name of field to sort by, cannot be <code>null</code>.
129 * @param locale Locale of values in the field.
130 */
131 public SortField (String field, Locale locale) {
132 this.field = field.intern();
133 this.type = STRING;
134 this.locale = locale;
135 }
136
137 /** Creates a sort, possibly in reverse, by terms in the given field sorted
138 * according to the given locale.
139 * @param field Name of field to sort by, cannot be <code>null</code>.
140 * @param locale Locale of values in the field.
141 */
142 public SortField (String field, Locale locale, boolean reverse) {
143 this.field = field.intern();
144 this.type = STRING;
145 this.locale = locale;
146 this.reverse = reverse;
147 }
148
149 /** Creates a sort with a custom comparison function.
150 * @param field Name of field to sort by; cannot be <code>null</code>.
151 * @param comparator Returns a comparator for sorting hits.
152 */
153 public SortField (String field, SortComparatorSource comparator) {
154 this.field = (field != null) ? field.intern() : field;
155 this.type = CUSTOM;
156 this.factory = comparator;
157 }
158
159 /** Creates a sort, possibly in reverse, with a custom comparison function.
160 * @param field Name of field to sort by; cannot be <code>null</code>.
161 * @param comparator Returns a comparator for sorting hits.
162 * @param reverse True if natural order should be reversed.
163 */
164 public SortField (String field, SortComparatorSource comparator, boolean reverse) {
165 this.field = (field != null) ? field.intern() : field;
166 this.type = CUSTOM;
167 this.reverse = reverse;
168 this.factory = comparator;
169 }
170
171 /** Returns the name of the field. Could return <code>null</code>
172 * if the sort is by SCORE or DOC.
173 * @return Name of field, possibly <code>null</code>.
174 */
175 public String getField() {
176 return field;
177 }
178
179 /** Returns the type of contents in the field.
180 * @return One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.
181 */
182 public int getType() {
183 return type;
184 }
185
186 /** Returns the Locale by which term values are interpreted.
187 * May return <code>null</code> if no Locale was specified.
188 * @return Locale, or <code>null</code>.
189 */
190 public Locale getLocale() {
191 return locale;
192 }
193
194 /** Returns whether the sort should be reversed.
195 * @return True if natural order should be reversed.
196 */
197 public boolean getReverse() {
198 return reverse;
199 }
200
201 public SortComparatorSource getFactory() {
202 return factory;
203 }
204
205 public String toString() {
206 StringBuffer buffer = new StringBuffer();
207 switch (type) {
208 case SCORE: buffer.append("<score>");
209 break;
210
211 case DOC: buffer.append("<doc>");
212 break;
213
214 case CUSTOM: buffer.append ("<custom:\"" + field + "\": "
215 + factory + ">");
216 break;
217
218 default: buffer.append("\"" + field + "\"");
219 break;
220 }
221
222 if (locale != null) buffer.append ("("+locale+")");
223 if (reverse) buffer.append('!');
224
225 return buffer.toString();
226 }
227 }
|