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


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();
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>
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:
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 
140 
141 protected:
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 
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 
172 
173 protected:
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 
189 
190 protected:
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 
206 
207 protected:
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);
223 
225 
226 protected:
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 
239 public:
240  StringComparatorLocale(int32_t numHits, const String& field, const std::locale& locale);
242 
244 
245 protected:
248  String field;
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 
267 public:
268  StringOrdValComparator(int32_t numHits, const String& field, int32_t sortPos, bool reversed);
270 
272 
273 protected:
277 
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);
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);
311 
313 
314 protected:
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
#define LUCENE_CLASS(Name)
Definition: LuceneObject.h:24
Parses field's values as byte (using FieldCache#getBytes and sorts by ascending value.
Definition: FieldComparator.h:134
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
ByteComparator(int32_t numHits, const String &field, const ParserPtr &parser)
ByteParserPtr parser
Definition: FieldComparator.h:139
Utility template class to handle collections that can be safely copied and shared.
Definition: Collection.h:17
static this_type newInstance(int32_t size=0)
Definition: Collection.h:33
Sorts by ascending docID.
Definition: FieldComparator.h:149
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...
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
int32_t docBase
Definition: FieldComparator.h:154
DocComparator(int32_t numHits)
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
Parses field's values as double (using FieldCache#getDoubles and sorts by ascending value.
Definition: FieldComparator.h:166
DoubleComparator(int32_t numHits, const String &field, const ParserPtr &parser)
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
DoubleParserPtr parser
Definition: FieldComparator.h:171
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
A FieldComparator compares hits so as to determine their sort order when collecting the top results w...
Definition: FieldComparator.h:42
virtual void setScorer(const ScorerPtr &scorer)
Sets the Scorer to use in case a document's score is needed.
virtual int32_t compareBottom(int32_t doc)=0
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
virtual void copy(int32_t slot, int32_t doc)=0
This method is called when a new hit is competitive. You should copy any state associated with this d...
virtual void setBottom(int32_t slot)=0
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom is called,...
virtual ComparableValue value(int32_t slot)=0
Return the actual value in the slot.
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)=0
Set a new Reader. All doc correspond to the current Reader.
virtual int32_t compare(int32_t slot1, int32_t slot2)=0
Compare hit at slot1 with hit at slot2.
Parses field's values as int (using FieldCache#getInts and sorts by ascending value.
Definition: FieldComparator.h:183
IntParserPtr parser
Definition: FieldComparator.h:188
IntComparator(int32_t numHits, const String &field, const ParserPtr &parser)
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
Parses field's values as long (using FieldCache#getLongs and sorts by ascending value.
Definition: FieldComparator.h:200
LongParserPtr parser
Definition: FieldComparator.h:205
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
LongComparator(int32_t numHits, const String &field, const ParserPtr &parser)
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
Base class for all Lucene classes.
Definition: LuceneObject.h:31
Definition: FieldComparator.h:94
Collection< TYPE > values
Definition: FieldComparator.h:106
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
Definition: FieldComparator.h:112
NumericComparator(int32_t numHits, const String &field=EmptyString)
Definition: FieldComparator.h:96
virtual ~NumericComparator()
Definition: FieldComparator.h:102
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
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
Collection< TYPE > currentReaderValues
Definition: FieldComparator.h:107
virtual ComparableValue value(int32_t slot)
Return the actual value in the slot.
Definition: FieldComparator.h:128
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
TYPE bottom
Definition: FieldComparator.h:109
String field
Definition: FieldComparator.h:108
Sorts by descending relevance. NOTE: if you are sorting only by descending relevance and then seconda...
Definition: FieldComparator.h:219
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
ScorerPtr scorer
Definition: FieldComparator.h:224
virtual void setScorer(const ScorerPtr &scorer)
Sets the Scorer to use in case a document's score is needed.
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...
RelevanceComparator(int32_t numHits)
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
Sorts by a field's value using the Collator for a given Locale.
Definition: FieldComparator.h:238
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
virtual void setBottom(int32_t slot)
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom is called,...
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
String bottom
Definition: FieldComparator.h:250
virtual ComparableValue value(int32_t slot)
Return the actual value in the slot.
String field
Definition: FieldComparator.h:248
CollatorPtr collator
Definition: FieldComparator.h:249
Collection< String > values
Definition: FieldComparator.h:243
Collection< String > currentReaderValues
Definition: FieldComparator.h:247
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
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...
StringComparatorLocale(int32_t numHits, const String &field, const std::locale &locale)
Sorts by field's natural String sort order, using ordinals. This is functionally equivalent to {},...
Definition: FieldComparator.h:266
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
int32_t binarySearch(Collection< String > lookup, const String &key, int32_t low, int32_t high)
int32_t sortPos
Definition: FieldComparator.h:287
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...
virtual int32_t getBottomSlot()
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
bool reversed
Definition: FieldComparator.h:286
Collection< int32_t > order
Definition: FieldComparator.h:280
Collection< String > lookup
Definition: FieldComparator.h:279
String field
Definition: FieldComparator.h:281
void convert(int32_t slot)
String bottomValue
Definition: FieldComparator.h:285
virtual Collection< String > getValues()
StringOrdValComparator(int32_t numHits, const String &field, int32_t sortPos, bool reversed)
virtual void setBottom(int32_t slot)
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom is called,...
Collection< String > values
Definition: FieldComparator.h:275
int32_t bottomSlot
Definition: FieldComparator.h:283
virtual ComparableValue value(int32_t slot)
Return the actual value in the slot.
int32_t bottomOrd
Definition: FieldComparator.h:284
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
Collection< int32_t > ords
Definition: FieldComparator.h:271
Collection< int32_t > readerGen
Definition: FieldComparator.h:276
int32_t currentReaderGen
Definition: FieldComparator.h:278
Sorts by field's natural String sort order. All comparisons are done using String....
Definition: FieldComparator.h:307
virtual void setBottom(int32_t slot)
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom is called,...
String bottom
Definition: FieldComparator.h:318
virtual ComparableValue value(int32_t slot)
Return the actual value in the slot.
virtual int32_t compare(int32_t slot1, int32_t slot2)
Compare hit at slot1 with hit at slot2.
virtual void setNextReader(const IndexReaderPtr &reader, int32_t docBase)
Set a new Reader. All doc correspond to the current Reader.
Collection< String > currentReaderValues
Definition: FieldComparator.h:316
virtual int32_t compareBottom(int32_t doc)
Compare the bottom of the queue with doc. This will only invoked after setBottom has been called....
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...
StringValComparator(int32_t numHits, const String &field)
Collection< String > values
Definition: FieldComparator.h:312
String field
Definition: FieldComparator.h:317
Definition: AbstractAllTermDocs.h:12
boost::shared_ptr< LongParser > LongParserPtr
Definition: LuceneTypes.h:378
boost::shared_ptr< ByteParser > ByteParserPtr
Definition: LuceneTypes.h:289
boost::shared_ptr< IntParser > IntParserPtr
Definition: LuceneTypes.h:376
boost::shared_ptr< DoubleParser > DoubleParserPtr
Definition: LuceneTypes.h:328
boost::shared_ptr< Parser > ParserPtr
Definition: LuceneTypes.h:401
boost::shared_ptr< Collator > CollatorPtr
Definition: LuceneTypes.h:525
boost::shared_ptr< IndexReader > IndexReaderPtr
Definition: LuceneTypes.h:157
boost::shared_ptr< Scorer > ScorerPtr
Definition: LuceneTypes.h:429

clucene.sourceforge.net