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