| 1 | package org.apache.lucene.search; | |
| 2 | ||
| 3 | /** | |
| 4 | * Copyright 2004 The Apache Software Foundation | |
| 5 | * | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 7 | * you may not use this file except in compliance with the License. | |
| 8 | * You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, software | |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 15 | * See the License for the specific language governing permissions and | |
| 16 | * limitations under the License. | |
| 17 | */ | |
| 18 | ||
| 19 | import java.io.Serializable; | |
| 20 | import java.util.Locale; | |
| 21 | ||
| 22 | /** | |
| 23 | * Stores information about how to sort documents by terms in an individual | |
| 24 | * field. Fields must be indexed in order to sort by them. | |
| 25 | * | |
| 26 | * <p>Created: Feb 11, 2004 1:25:29 PM | |
| 27 | * | |
| 28 | * @author Tim Jones (Nacimiento Software) | |
| 29 | * @since lucene 1.4 | |
| 30 | * @version $Id: SortField.java,v 1.9 2004/05/24 22:51:42 tjones Exp $ | |
| 31 | * @see Sort | |
| 32 | */ | |
| 33 | public class SortField | |
| 34 | implements Serializable { | |
| 35 | ||
| 36 | /** Sort by document score (relevancy). Sort values are Float and higher | |
| 37 | * values are at the front. */ | |
| 38 | public static final int SCORE = 0; | |
| 39 | ||
| 40 | /** Sort by document number (index order). Sort values are Integer and lower | |
| 41 | * values are at the front. */ | |
| 42 | public static final int DOC = 1; | |
| 43 | ||
| 44 | /** Guess type of sort based on field contents. A regular expression is used | |
| 45 | * to look at the first term indexed for the field and determine if it | |
| 46 | * represents an integer number, a floating point number, or just arbitrary | |
| 47 | * string characters. */ | |
| 48 | public static final int AUTO = 2; | |
| 49 | ||
| 50 | /** Sort using term values as Strings. Sort values are String and lower | |
| 51 | * values are at the front. */ | |
| 52 | public static final int STRING = 3; | |
| 53 | ||
| 54 | /** Sort using term values as encoded Integers. Sort values are Integer and | |
| 55 | * lower values are at the front. */ | |
| 56 | public static final int INT = 4; | |
| 57 | ||
| 58 | /** Sort using term values as encoded Floats. Sort values are Float and | |
| 59 | * lower values are at the front. */ | |
| 60 | public static final int FLOAT = 5; | |
| 61 | ||
| 62 | /** Sort using a custom Comparator. Sort values are any Comparable and | |
| 63 | * sorting is done according to natural order. */ | |
| 64 | public static final int CUSTOM = 9; | |
| 65 | ||
| 66 | // IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace" | |
| 67 | // as the above static int values. Any new values must not have the same value | |
| 68 | // as FieldCache.STRING_INDEX. | |
| 69 | ||
| 70 | ||
| 71 | /** Represents sorting by document score (relevancy). */ | |
| 72 | public static final SortField FIELD_SCORE = new SortField (null, SCORE); | |
| 73 | ||
| 74 | /** Represents sorting by document number (index order). */ | |
| 75 | public static final SortField FIELD_DOC = new SortField (null, DOC); | |
| 76 | ||
| 77 | ||
| 78 | private String field; | |
| 79 | 493x | private int type = AUTO; // defaults to determining type dynamically |
| 80 | private Locale locale; // defaults to "natural order" (no Locale) | |
| 81 | 493x | boolean reverse = false; // defaults to natural order |
| 82 | private SortComparatorSource factory; | |
| 83 | ||
| 84 | /** Creates a sort by terms in the given field where the type of term value | |
| 85 | * is determined dynamically ({@link #AUTO AUTO}). | |
| 86 | * @param field Name of field to sort by, cannot be <code>null</code>. | |
| 87 | */ | |
| 88 | 0x | public SortField (String field) { |
| 89 | 0x | this.field = field.intern(); |
| 90 | 0x | } |
| 91 | ||
| 92 | /** Creates a sort, possibly in reverse, by terms in the given field where | |
| 93 | * the type of term value is determined dynamically ({@link #AUTO AUTO}). | |
| 94 | * @param field Name of field to sort by, cannot be <code>null</code>. | |
| 95 | * @param reverse True if natural order should be reversed. | |
| 96 | */ | |
| 97 | 2x | public SortField (String field, boolean reverse) { |
| 98 | 2x | this.field = field.intern(); |
| 99 | 2x | this.reverse = reverse; |
| 100 | 2x | } |
| 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 | 41x | public SortField (String field, int type) { |
| 109 | 2/2 41x | this.field = (field != null) ? field.intern() : field; |
| 110 | 41x | this.type = type; |
| 111 | 41x | } |
| 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 | 493x | public SortField (String field, int type, boolean reverse) { |
| 121 | 2/2 493x | this.field = (field != null) ? field.intern() : field; |
| 122 | 493x | this.type = type; |
| 123 | 493x | this.reverse = reverse; |
| 124 | 493x | } |
| 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 | 4x | public SortField (String field, Locale locale) { |
| 132 | 4x | this.field = field.intern(); |
| 133 | 4x | this.type = STRING; |
| 134 | 4x | this.locale = locale; |
| 135 | 4x | } |
| 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 | 4x | public SortField (String field, Locale locale, boolean reverse) { |
| 143 | 4x | this.field = field.intern(); |
| 144 | 4x | this.type = STRING; |
| 145 | 4x | this.locale = locale; |
| 146 | 4x | this.reverse = reverse; |
| 147 | 4x | } |
| 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 | 4x | public SortField (String field, SortComparatorSource comparator) { |
| 154 | 1/2 4x | this.field = (field != null) ? field.intern() : field; |
| 155 | 4x | this.type = CUSTOM; |
| 156 | 4x | this.factory = comparator; |
| 157 | 4x | } |
| 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 | 4x | public SortField (String field, SortComparatorSource comparator, boolean reverse) { |
| 165 | 1/2 4x | this.field = (field != null) ? field.intern() : field; |
| 166 | 4x | this.type = CUSTOM; |
| 167 | 4x | this.reverse = reverse; |
| 168 | 4x | this.factory = comparator; |
| 169 | 4x | } |
| 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 | 439x | 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 | 4958x | 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 | 1478x | 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 | 4958x | return reverse; |
| 199 | } | |
| 200 | ||
| 201 | public SortComparatorSource getFactory() { | |
| 202 | 439x | return factory; |
| 203 | } | |
| 204 | ||
| 205 | public String toString() { | |
| 206 | 0x | StringBuffer buffer = new StringBuffer(); |
| 207 | 0x | switch (type) { |
| 208 | 0x | case SCORE: buffer.append("<score>"); |
| 209 | 0x | break; |
| 210 | ||
| 211 | 0x | case DOC: buffer.append("<doc>"); |
| 212 | 0x | break; |
| 213 | ||
| 214 | 0x | case CUSTOM: buffer.append ("<custom:\"" + field + "\": " |
| 215 | + factory + ">"); | |
| 216 | 0x | break; |
| 217 | ||
| 218 | 0x | default: buffer.append("\"" + field + "\""); |
| 219 | break; | |
| 220 | } | |
| 221 | ||
| 222 | 0/2 0x | if (locale != null) buffer.append ("("+locale+")"); |
| 223 | 0/2 0x | if (reverse) buffer.append('!'); |
| 224 | ||
| 225 | 0x | return buffer.toString(); |
| 226 | } | |
| 227 | } |