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
21 /// <summary> Encapsulates sort criteria for returned hits.
23 /// <p>The fields used to determine sort order must be carefully chosen.
24 /// Documents must contain a single term in such a Field,
25 /// and the value of the term should indicate the document's relative position in
26 /// a given sort order. The Field must be indexed, but should not be tokenized,
27 /// and does not need to be stored (unless you happen to want it back with the
28 /// rest of your document data). In other words:
30 /// <p><code>document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.UN_TOKENIZED));</code></p>
33 /// <p><h3>Valid Types of Values</h3>
35 /// <p>There are three possible kinds of term values which may be put into
36 /// sorting fields: Integers, Floats, or Strings. Unless
37 /// {@link SortField SortField} objects are specified, the type of value
38 /// in the Field is determined by parsing the first term in the Field.
40 /// <p>Integer term values should contain only digits and an optional
41 /// preceeding negative sign. Values must be base 10 and in the range
42 /// <code>Integer.MIN_VALUE</code> and <code>Integer.MAX_VALUE</code> inclusive.
43 /// Documents which should appear first in the sort
44 /// should have low value integers, later documents high values
45 /// (i.e. the documents should be numbered <code>1..n</code> where
46 /// <code>1</code> is the first and <code>n</code> the last).
48 /// <p>Float term values should conform to values accepted by
49 /// {@link Float Float.valueOf(String)} (except that <code>NaN</code>
50 /// and <code>Infinity</code> are not supported).
51 /// Documents which should appear first in the sort
52 /// should have low values, later documents high values.
54 /// <p>String term values can contain any valid String, but should
55 /// not be tokenized. The values are sorted according to their
56 /// {@link Comparable natural order}. Note that using this type
57 /// of term value has higher memory requirements than the other
60 /// <p><h3>Object Reuse</h3>
62 /// <p>One of these objects can be
63 /// used multiple times and the sort order changed between usages.
65 /// <p>This class is thread safe.
67 /// <p><h3>Memory Usage</h3>
69 /// <p>Sorting uses of caches of term values maintained by the
70 /// internal HitQueue(s). The cache is static and contains an integer
71 /// or float array of length <code>IndexReader.maxDoc()</code> for each Field
72 /// name for which a sort is performed. In other words, the size of the
73 /// cache in bytes is:
75 /// <p><code>4 * IndexReader.maxDoc() * (# of different fields actually used to sort)</code>
77 /// <p>For String fields, the cache is larger: in addition to the
78 /// above array, the value of every term in the Field is kept in memory.
79 /// If there are many unique terms in the Field, this could
82 /// <p>Note that the size of the cache is not affected by how many
83 /// fields are in the index and <i>might</i> be used to sort - only by
84 /// the ones actually used to sort a result set.
86 /// <p>The cache is cleared each time a new <code>IndexReader</code> is
87 /// passed in, or if the value returned by <code>maxDoc()</code>
88 /// changes for the current IndexReader. This class is not set up to
89 /// be able to efficiently sort hits from more than one index
92 /// <p>Created: Feb 12, 2004 10:53:57 AM
95 /// <author> Tim Jones (Nacimiento Software)
97 /// <since> lucene 1.4
99 /// <version> $Id: Sort.cs,v 1.2 2005/10/06 19:29:57 dsd Exp $
105 /// <summary> Represents sorting by computed relevance. Using this sort criteria returns
106 /// the same results as calling
107 /// {@link Searcher#Search(Query) Searcher#search()}without a sort criteria,
108 /// only with slightly more overhead.
110 public static readonly Sort RELEVANCE
= new Sort();
112 /// <summary>Represents sorting by index order. </summary>
113 public static readonly Sort INDEXORDER
;
115 // internal representation of the sort criteria
116 internal SortField
[] fields
;
118 /// <summary> Sorts by computed relevance. This is the same sort criteria as calling
119 /// {@link Searcher#Search(Query) Searcher#search()}without a sort criteria,
120 /// only with slightly more overhead.
122 public Sort() : this(new SortField
[]{SortField.FIELD_SCORE, SortField.FIELD_DOC}
)
126 /// <summary> Sorts by the terms in <code>field</code> then by index order (document
127 /// number). The type of value in <code>field</code> is determined
131 /// <seealso cref="SortField#AUTO">
133 public Sort(System
.String field
)
135 SetSort(field
, false);
138 /// <summary> Sorts possibly in reverse by the terms in <code>field</code> then by
139 /// index order (document number). The type of value in <code>field</code> is
140 /// determined automatically.
143 /// <seealso cref="SortField#AUTO">
145 public Sort(System
.String field
, bool reverse
)
147 SetSort(field
, reverse
);
150 /// <summary> Sorts in succession by the terms in each field. The type of value in
151 /// <code>field</code> is determined automatically.
154 /// <seealso cref="SortField#AUTO">
156 public Sort(System
.String
[] fields
)
161 /// <summary>Sorts by the criteria in the given SortField. </summary>
162 public Sort(SortField field
)
167 /// <summary>Sorts in succession by the criteria in each SortField. </summary>
168 public Sort(SortField
[] fields
)
173 /// <summary> Sets the sort to the terms in <code>Field</code> then by index order
174 /// (document number).
176 public void SetSort(System
.String field
)
178 SetSort(field
, false);
181 /// <summary> Sets the sort to the terms in <code>Field</code> possibly in reverse,
182 /// then by index order (document number).
184 public virtual void SetSort(System
.String field
, bool reverse
)
186 SortField
[] nfields
= new SortField
[]{new SortField(field, SortField.AUTO, reverse), SortField.FIELD_DOC}
;
190 /// <summary>Sets the sort to the terms in each Field in succession. </summary>
191 public virtual void SetSort(System
.String
[] fieldnames
)
193 int n
= fieldnames
.Length
;
194 SortField
[] nfields
= new SortField
[n
];
195 for (int i
= 0; i
< n
; ++i
)
197 nfields
[i
] = new SortField(fieldnames
[i
], SortField
.AUTO
);
202 /// <summary>Sets the sort to the given criteria. </summary>
203 public virtual void SetSort(SortField field
)
205 this.fields
= new SortField
[]{field}
;
208 /// <summary>Sets the sort to the given criteria in succession. </summary>
209 public virtual void SetSort(SortField
[] fields
)
211 this.fields
= fields
;
214 /// <summary> Representation of the sort criteria.</summary>
215 /// <returns> Array of SortField objects used in this sort criteria
217 public virtual SortField
[] GetSort()
222 public override System
.String
ToString()
224 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
226 for (int i
= 0; i
< fields
.Length
; i
++)
228 buffer
.Append(fields
[i
].ToString());
229 if ((i
+ 1) < fields
.Length
)
233 return buffer
.ToString();
237 INDEXORDER
= new Sort(SortField
.FIELD_DOC
);