00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef XAPIAN_INCLUDED_QUERYPARSER_H
00023 #define XAPIAN_INCLUDED_QUERYPARSER_H
00024
00025 #include <xapian/base.h>
00026 #include <xapian/query.h>
00027 #include <xapian/termiterator.h>
00028 #include <xapian/visibility.h>
00029
00030 #include <set>
00031 #include <string>
00032
00033 namespace Xapian {
00034
00035 class Database;
00036 class Stem;
00037
00039 class XAPIAN_VISIBILITY_DEFAULT Stopper {
00040 public:
00042 virtual bool operator()(const std::string & term) const = 0;
00043
00045 virtual ~Stopper() { }
00046
00048 virtual std::string get_description() const;
00049 };
00050
00052 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
00053 private:
00054 std::set<std::string> stop_words;
00055
00056 public:
00058 SimpleStopper() { }
00059
00061 #ifndef __SUNPRO_CC
00062 template <class Iterator>
00063 SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
00064 #else
00065
00066 template <class Iterator>
00067 SimpleStopper(Iterator begin, Iterator end) {
00068 while (begin != end) stop_words.insert(*begin++);
00069 }
00070 #endif
00071
00073 void add(const std::string & word) { stop_words.insert(word); }
00074
00076 virtual bool operator()(const std::string & term) const {
00077 return stop_words.find(term) != stop_words.end();
00078 }
00079
00081 virtual ~SimpleStopper() { }
00082
00084 virtual std::string get_description() const;
00085 };
00086
00088 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00090 virtual ~ValueRangeProcessor();
00091
00098 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00099 };
00100
00105 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00106 Xapian::valueno valno;
00107
00108 public:
00113 StringValueRangeProcessor(Xapian::valueno valno_)
00114 : valno(valno_) { }
00115
00117 Xapian::valueno operator()(std::string &, std::string &) {
00118 return valno;
00119 }
00120 };
00121
00126 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public ValueRangeProcessor {
00127 Xapian::valueno valno;
00128 bool prefer_mdy;
00129 int epoch_year;
00130
00131 public:
00142 DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
00143 int epoch_year_ = 1970)
00144 : valno(valno_), prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00145
00152 Xapian::valueno operator()(std::string &begin, std::string &end);
00153 };
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 #ifndef DOXYGEN
00166
00167 namespace v102 {
00168 #endif
00169
00177 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00178 Xapian::valueno valno;
00179 bool prefix;
00180 std::string str;
00181
00182 public:
00187 NumberValueRangeProcessor(Xapian::valueno valno_)
00188 : valno(valno_), prefix(false) { }
00189
00222 NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00223 bool prefix_ = true)
00224 : valno(valno_), prefix(prefix_), str(str_) { }
00225
00235 Xapian::valueno operator()(std::string &begin, std::string &end);
00236 };
00237
00238 #ifndef DOXYGEN
00239 }
00240
00241 # ifndef XAPIAN_NO_V102_NUMBER_VRP
00242 using v102::NumberValueRangeProcessor;
00243 # endif
00244 #endif
00245
00247 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00248 public:
00250 class Internal;
00252 Xapian::Internal::RefCntPtr<Internal> internal;
00253
00255 typedef enum {
00257 FLAG_BOOLEAN = 1,
00259 FLAG_PHRASE = 2,
00261 FLAG_LOVEHATE = 4,
00263 FLAG_BOOLEAN_ANY_CASE = 8,
00269 FLAG_WILDCARD = 16,
00276 FLAG_PURE_NOT = 32,
00288 FLAG_PARTIAL = 64,
00289
00303 FLAG_SPELLING_CORRECTION = 128,
00304
00309 FLAG_SYNONYM = 256,
00310
00315 FLAG_AUTO_SYNONYMS = 512,
00316
00322 FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS,
00323
00330 FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
00331 } feature_flag;
00332
00333 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00334
00336 QueryParser(const QueryParser & o);
00337
00339 QueryParser & operator=(const QueryParser & o);
00340
00342 QueryParser();
00343
00345 ~QueryParser();
00346
00355 void set_stemmer(const Xapian::Stem & stemmer);
00356
00374 void set_stemming_strategy(stem_strategy strategy);
00375
00377 void set_stopper(const Stopper *stop = NULL);
00378
00390 void set_default_op(Query::op default_op);
00391
00393 Query::op get_default_op() const;
00394
00396 void set_database(const Database &db);
00397
00421 Query parse_query(const std::string &query_string,
00422 unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
00423 const std::string &default_prefix = "");
00424
00464 void add_prefix(const std::string &field, const std::string &prefix);
00465
00511 void add_boolean_prefix(const std::string & field, const std::string &prefix);
00512
00514 TermIterator stoplist_begin() const;
00515 TermIterator stoplist_end() const {
00516 return TermIterator(NULL);
00517 }
00518
00520 TermIterator unstem_begin(const std::string &term) const;
00521 TermIterator unstem_end(const std::string &) const {
00522 return TermIterator(NULL);
00523 }
00524
00526 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00527
00535 std::string get_corrected_query_string() const;
00536
00538 std::string get_description() const;
00539 };
00540
00565 XAPIAN_VISIBILITY_DEFAULT
00566 std::string sortable_serialise(double value);
00567
00580 XAPIAN_VISIBILITY_DEFAULT
00581 double sortable_unserialise(const std::string & value);
00582
00583 }
00584
00585 #endif // XAPIAN_INCLUDED_QUERYPARSER_H