Lucene++ - a full-featured, c++ search engine
API Documentation

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

FieldComparator.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6
7 #ifndef FIELDCOMPARATOR_H
8 #define FIELDCOMPARATOR_H
9
10 #include "LuceneObject.h"
11
12 namespace Lucene {
13
42 class LPPAPI FieldComparator : public LuceneObject {
43 public:
44 virtual ~FieldComparator();
45 LUCENE_CLASS(FieldComparator);
46
47 public:
53 virtual int32_t compare(int32_t slot1, int32_t slot2) = 0;
54
58 virtual void setBottom(int32_t slot) = 0;
59
69 virtual int32_t compareBottom(int32_t doc) = 0;
70
75 virtual void copy(int32_t slot, int32_t doc) = 0;
76
81 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase) = 0;
82
85 virtual void setScorer(const ScorerPtr& scorer);
86
90 virtual ComparableValue value(int32_t slot) = 0;
91 };
92
93 template <typename TYPE>
94 class NumericComparator : public FieldComparator {
95 public:
96 NumericComparator(int32_t numHits, const String& field = EmptyString) {
97 this->values = Collection<TYPE>::newInstance(numHits);
98 this->field = field;
99 this->bottom = 0;
100 }
101
102 virtual ~NumericComparator() {
103 }
104
105 protected:
106 Collection<TYPE> values;
107 Collection<TYPE> currentReaderValues;
108 String field;
109 TYPE bottom;
110
111 public:
112 virtual int32_t compare(int32_t slot1, int32_t slot2) {
113 return (int32_t)(values[slot1] - values[slot2]);
114 }
115
116 virtual int32_t compareBottom(int32_t doc) {
117 return (int32_t)(bottom - currentReaderValues[doc]);
118 }
119
120 virtual void copy(int32_t slot, int32_t doc) {
121 values[slot] = currentReaderValues[doc];
122 }
123
124 virtual void setBottom(int32_t slot) {
125 bottom = values[slot];
126 }
127
128 virtual ComparableValue value(int32_t slot) {
129 return ComparableValue(values[slot]);
130 }
131 };
132
134 class LPPAPI ByteComparator : public NumericComparator<uint8_t> {
135 public:
136 ByteComparator(int32_t numHits, const String& field, const ParserPtr& parser);
137 virtual ~ByteComparator();
138
139 LUCENE_CLASS(ByteComparator);
140
141 protected:
142 ByteParserPtr parser;
143
144 public:
145 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
146 };
147
149 class LPPAPI DocComparator : public NumericComparator<int32_t> {
150 public:
151 DocComparator(int32_t numHits);
152 virtual ~DocComparator();
153
154 LUCENE_CLASS(DocComparator);
155
156 protected:
157 int32_t docBase;
158
159 public:
160 virtual int32_t compareBottom(int32_t doc);
161 virtual void copy(int32_t slot, int32_t doc);
162 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
163 };
164
166 class LPPAPI DoubleComparator : public NumericComparator<double> {
167 public:
168 DoubleComparator(int32_t numHits, const String& field, const ParserPtr& parser);
169 virtual ~DoubleComparator();
170
171 LUCENE_CLASS(DoubleComparator);
172
173 protected:
174 DoubleParserPtr parser;
175
176 public:
177 virtual int32_t compare(int32_t slot1, int32_t slot2);
178 virtual int32_t compareBottom(int32_t doc);
179 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
180 };
181
183 class LPPAPI IntComparator : public NumericComparator<int32_t> {
184 public:
185 IntComparator(int32_t numHits, const String& field, const ParserPtr& parser);
186 virtual ~IntComparator();
187
188 LUCENE_CLASS(IntComparator);
189
190 protected:
191 IntParserPtr parser;
192
193 public:
194 virtual int32_t compare(int32_t slot1, int32_t slot2);
195 virtual int32_t compareBottom(int32_t doc);
196 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
197 };
198
200 class LPPAPI LongComparator : public NumericComparator<int64_t> {
201 public:
202 LongComparator(int32_t numHits, const String& field, const ParserPtr& parser);
203 virtual ~LongComparator();
204
205 LUCENE_CLASS(LongComparator);
206
207 protected:
208 LongParserPtr parser;
209
210 public:
211 virtual int32_t compare(int32_t slot1, int32_t slot2);
212 virtual int32_t compareBottom(int32_t doc);
213 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
214 };
215
219 class LPPAPI RelevanceComparator : public NumericComparator<double> {
220 public:
221 RelevanceComparator(int32_t numHits);
222 virtual ~RelevanceComparator();
223
224 LUCENE_CLASS(RelevanceComparator);
225
226 protected:
227 ScorerPtr scorer;
228
229 public:
230 virtual int32_t compare(int32_t slot1, int32_t slot2);
231 virtual int32_t compareBottom(int32_t doc);
232 virtual void copy(int32_t slot, int32_t doc);
233 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
234 virtual void setScorer(const ScorerPtr& scorer);
235 };
236
238 class LPPAPI StringComparatorLocale : public FieldComparator {
239 public:
240 StringComparatorLocale(int32_t numHits, const String& field, const std::locale& locale);
241 virtual ~StringComparatorLocale();
242
243 LUCENE_CLASS(StringComparatorLocale);
244
245 protected:
246 Collection<String> values;
247 Collection<String> currentReaderValues;
248 String field;
249 CollatorPtr collator;
250 String bottom;
251
252 public:
253 virtual int32_t compare(int32_t slot1, int32_t slot2);
254 virtual int32_t compareBottom(int32_t doc);
255 virtual void copy(int32_t slot, int32_t doc);
256 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
257 virtual void setBottom(int32_t slot);
258 virtual ComparableValue value(int32_t slot);
259 };
260
266 class LPPAPI StringOrdValComparator : public FieldComparator {
267 public:
268 StringOrdValComparator(int32_t numHits, const String& field, int32_t sortPos, bool reversed);
269 virtual ~StringOrdValComparator();
270
271 LUCENE_CLASS(StringOrdValComparator);
272
273 protected:
274 Collection<int32_t> ords;
275 Collection<String> values;
276 Collection<int32_t> readerGen;
277
278 int32_t currentReaderGen;
279 Collection<String> lookup;
280 Collection<int32_t> order;
281 String field;
282
283 int32_t bottomSlot;
284 int32_t bottomOrd;
285 String bottomValue;
286 bool reversed;
287 int32_t sortPos;
288
289 public:
290 virtual int32_t compare(int32_t slot1, int32_t slot2);
291 virtual int32_t compareBottom(int32_t doc);
292 virtual void copy(int32_t slot, int32_t doc);
293 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
294 virtual void setBottom(int32_t slot);
295 virtual ComparableValue value(int32_t slot);
296 virtual Collection<String> getValues();
297 virtual int32_t getBottomSlot();
298 virtual String getField();
299
300 protected:
301 void convert(int32_t slot);
302 int32_t binarySearch(Collection<String> lookup, const String& key, int32_t low, int32_t high);
303 };
304
307 class LPPAPI StringValComparator : public FieldComparator {
308 public:
309 StringValComparator(int32_t numHits, const String& field);
310 virtual ~StringValComparator();
311
312 LUCENE_CLASS(StringOrdValComparator);
313
314 protected:
315 Collection<String> values;
316 Collection<String> currentReaderValues;
317 String field;
318 String bottom;
319
320 public:
321 virtual int32_t compare(int32_t slot1, int32_t slot2);
322 virtual int32_t compareBottom(int32_t doc);
323 virtual void copy(int32_t slot, int32_t doc);
324 virtual void setNextReader(const IndexReaderPtr& reader, int32_t docBase);
325 virtual void setBottom(int32_t slot);
326 virtual ComparableValue value(int32_t slot);
327 };
328
329 }
330
331 #endif
Lucene::NumericComparator::NumericComparator
NumericComparator(int32_t numHits, const String &field=EmptyString)
Definition: FieldComparator.h:96
Lucene::StringComparatorLocale::currentReaderValues
Collection< String > currentReaderValues
Definition: FieldComparator.h:247
Lucene::StringOrdValComparator::bottomSlot
int32_t bottomSlot
Definition: FieldComparator.h:283
Lucene::StringValComparator
Sorts by field's natural String sort order. All comparisons are done using String.compare, which is slow for medium to large result sets but possibly very fast for very small results sets.
Definition: FieldComparator.h:307
Lucene::NumericComparator::copy
virtual void copy(int32_t slot, int32_t doc)
This method is called when a new hit is competitive. You should copy any state associated with this d...
Definition: FieldComparator.h:120
Lucene::LongComparator
Parses field's values as long (using FieldCache#getLongs and sorts by ascending value.
Definition: FieldComparator.h:200
Lucene::IntComparator
Parses field's values as int (using FieldCache#getInts and sorts by ascending value.
Definition: FieldComparator.h:183
Lucene::DoubleParserPtr
boost::shared_ptr< DoubleParser > DoubleParserPtr
Definition: LuceneTypes.h:328
Lucene::StringComparatorLocale
Sorts by a field's value using the Collator for a given Locale.
Definition: FieldComparator.h:238
Lucene::Collection::newInstance
static this_type newInstance(int32_t size=0)
Definition: Collection.h:33
Lucene::NumericComparator::value
virtual ComparableValue value(int32_t slot)
Return the actual value in the slot.
Definition: FieldComparator.h:128
Lucene::StringValComparator::values
Collection< String > values
Definition: FieldComparator.h:312
Lucene::StringValComparator::field
String field
Definition: FieldComparator.h:317
Lucene::NumericComparator::values
Collection< TYPE > values
Definition: FieldComparator.h:106
Lucene::ScorerPtr
boost::shared_ptr< Scorer > ScorerPtr
Definition: LuceneTypes.h:429
Lucene::StringOrdValComparator::order
Collection< int32_t > order
Definition: FieldComparator.h:280
Lucene::ByteComparator
Parses field's values as byte (using FieldCache#getBytes and sorts by ascending value.
Definition: FieldComparator.h:134
Lucene::DoubleComparator
Parses field's values as double (using FieldCache#getDoubles and sorts by ascending value...
Definition: FieldComparator.h:166
Lucene::RelevanceComparator
Sorts by descending relevance. NOTE: if you are sorting only by descending relevance and then seconda...
Definition: FieldComparator.h:219
LuceneObject.h
Lucene::NumericComparator::bottom
TYPE bottom
Definition: FieldComparator.h:109
Lucene::NumericComparator::compareBottom
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called...
Definition: FieldComparator.h:116
Lucene::ParserPtr
boost::shared_ptr< Parser > ParserPtr
Definition: LuceneTypes.h:401
Lucene::StringOrdValComparator::bottomValue
String bottomValue
Definition: FieldComparator.h:285
Lucene::StringComparatorLocale::bottom
String bottom
Definition: FieldComparator.h:250
Lucene::FieldComparator
A FieldComparator compares hits so as to determine their sort order when collecting the top results w...
Definition: FieldComparator.h:42
Lucene::NumericComparator::compare
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
Definition: FieldComparator.h:112
Lucene::ByteParserPtr
boost::shared_ptr< ByteParser > ByteParserPtr
Definition: LuceneTypes.h:289
Lucene::DocComparator
Sorts by ascending docID.
Definition: FieldComparator.h:149
Lucene::StringValComparator::currentReaderValues
Collection< String > currentReaderValues
Definition: FieldComparator.h:316
Lucene::StringOrdValComparator::currentReaderGen
int32_t currentReaderGen
Definition: FieldComparator.h:278
Lucene::IndexReaderPtr
boost::shared_ptr< IndexReader > IndexReaderPtr
Definition: LuceneTypes.h:157
Lucene::NumericComparator::currentReaderValues
Collection< TYPE > currentReaderValues
Definition: FieldComparator.h:107
Lucene::NumericComparator::field
String field
Definition: FieldComparator.h:108
Lucene::LuceneObject
Base class for all Lucene classes.
Definition: LuceneObject.h:31
Lucene::NumericComparator::~NumericComparator
virtual ~NumericComparator()
Definition: FieldComparator.h:102
LUCENE_CLASS
#define LUCENE_CLASS(Name)
Definition: LuceneObject.h:24
Lucene
Definition: AbstractAllTermDocs.h:12
Lucene::IntParserPtr
boost::shared_ptr< IntParser > IntParserPtr
Definition: LuceneTypes.h:376
Lucene::NumericComparator
Definition: FieldComparator.h:94
Lucene::StringOrdValComparator::lookup
Collection< String > lookup
Definition: FieldComparator.h:279
Lucene::StringOrdValComparator::bottomOrd
int32_t bottomOrd
Definition: FieldComparator.h:284
Lucene::StringOrdValComparator::values
Collection< String > values
Definition: FieldComparator.h:275
Lucene::StringComparatorLocale::field
String field
Definition: FieldComparator.h:248
Lucene::Collection
Utility template class to handle collections that can be safely copied and shared.
Definition: Collection.h:17
Lucene::LongParserPtr
boost::shared_ptr< LongParser > LongParserPtr
Definition: LuceneTypes.h:378
Lucene::StringOrdValComparator::reversed
bool reversed
Definition: FieldComparator.h:286
Lucene::StringOrdValComparator::field
String field
Definition: FieldComparator.h:281
Lucene::NumericComparator::setBottom
virtual void setBottom(int32_t slot)
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom is called...
Definition: FieldComparator.h:124
Lucene::StringOrdValComparator
Sorts by field's natural String sort order, using ordinals. This is functionally equivalent to String...
Definition: FieldComparator.h:266
Lucene::StringComparatorLocale::collator
CollatorPtr collator
Definition: FieldComparator.h:249
Lucene::StringOrdValComparator::sortPos
int32_t sortPos
Definition: FieldComparator.h:287
Lucene::StringValComparator::bottom
String bottom
Definition: FieldComparator.h:318
Lucene::CollatorPtr
boost::shared_ptr< Collator > CollatorPtr
Definition: LuceneTypes.h:525
Lucene::StringOrdValComparator::readerGen
Collection< int32_t > readerGen
Definition: FieldComparator.h:276
Lucene::StringComparatorLocale::values
Collection< String > values
Definition: FieldComparator.h:243

clucene.sourceforge.net