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 Stem;
00036
00038 class XAPIAN_VISIBILITY_DEFAULT Stopper {
00039 public:
00041 virtual bool operator()(const std::string & term) const = 0;
00042
00044 virtual ~Stopper() { }
00045
00047 virtual std::string get_description() const;
00048 };
00049
00051 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
00052 std::set<std::string> stop_words;
00053
00054 public:
00056 SimpleStopper() { }
00057
00059 #ifndef __SUNPRO_CC
00060 template <class Iterator>
00061 SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
00062 #else
00063
00064 template <class Iterator>
00065 SimpleStopper(Iterator begin, Iterator end) {
00066 while (begin != end) stop_words.insert(*begin++);
00067 }
00068 #endif
00069
00071 void add(const std::string & word) { stop_words.insert(word); }
00072
00074 virtual bool operator()(const std::string & term) const {
00075 return stop_words.find(term) != stop_words.end();
00076 }
00077
00079 virtual std::string get_description() const;
00080 };
00081
00083 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00085 virtual ~ValueRangeProcessor();
00086
00093 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00094 };
00095
00100 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00101 protected:
00102 Xapian::valueno valno;
00103
00104 private:
00105 bool prefix;
00106 std::string str;
00107
00108 public:
00113 StringValueRangeProcessor(Xapian::valueno valno_)
00114 : valno(valno_), str() { }
00115
00124 StringValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00125 bool prefix_ = true)
00126 : valno(valno_), prefix(prefix_), str(str_) { }
00127
00136 Xapian::valueno operator()(std::string &, std::string &);
00137 };
00138
00143 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public StringValueRangeProcessor {
00144 bool prefer_mdy;
00145 int epoch_year;
00146
00147 public:
00158 DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
00159 int epoch_year_ = 1970)
00160 : StringValueRangeProcessor(valno_),
00161 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00162
00200 DateValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00201 bool prefix_ = true,
00202 bool prefer_mdy_ = false, int epoch_year_ = 1970)
00203 : StringValueRangeProcessor(valno_, str_, prefix_),
00204 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00205
00212 Xapian::valueno operator()(std::string &begin, std::string &end);
00213 };
00214
00222 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public StringValueRangeProcessor {
00223 public:
00228 NumberValueRangeProcessor(Xapian::valueno valno_)
00229 : StringValueRangeProcessor(valno_) { }
00230
00263 NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00264 bool prefix_ = true)
00265 : StringValueRangeProcessor(valno_, str_, prefix_) { }
00266
00276 Xapian::valueno operator()(std::string &begin, std::string &end);
00277 };
00278
00280 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00281 public:
00283 class Internal;
00285 Xapian::Internal::RefCntPtr<Internal> internal;
00286
00288 typedef enum {
00290 FLAG_BOOLEAN = 1,
00292 FLAG_PHRASE = 2,
00294 FLAG_LOVEHATE = 4,
00296 FLAG_BOOLEAN_ANY_CASE = 8,
00302 FLAG_WILDCARD = 16,
00309 FLAG_PURE_NOT = 32,
00321 FLAG_PARTIAL = 64,
00322
00336 FLAG_SPELLING_CORRECTION = 128,
00337
00342 FLAG_SYNONYM = 256,
00343
00348 FLAG_AUTO_SYNONYMS = 512,
00349
00355 FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS,
00356
00363 FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
00364 } feature_flag;
00365
00366 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00367
00369 QueryParser(const QueryParser & o);
00370
00372 QueryParser & operator=(const QueryParser & o);
00373
00375 QueryParser();
00376
00378 ~QueryParser();
00379
00388 void set_stemmer(const Xapian::Stem & stemmer);
00389
00407 void set_stemming_strategy(stem_strategy strategy);
00408
00410 void set_stopper(const Stopper *stop = NULL);
00411
00413 void set_default_op(Query::op default_op);
00414
00416 Query::op get_default_op() const;
00417
00419 void set_database(const Database &db);
00420
00430 Query parse_query(const std::string &query_string,
00431 unsigned flags = FLAG_DEFAULT,
00432 const std::string &default_prefix = std::string());
00433
00469 void add_prefix(const std::string &field, const std::string &prefix);
00470
00516 void add_boolean_prefix(const std::string & field, const std::string &prefix);
00517
00519 TermIterator stoplist_begin() const;
00520 TermIterator stoplist_end() const {
00521 return TermIterator(NULL);
00522 }
00523
00525 TermIterator unstem_begin(const std::string &term) const;
00526 TermIterator unstem_end(const std::string &) const {
00527 return TermIterator(NULL);
00528 }
00529
00531 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00532
00540 std::string get_corrected_query_string() const;
00541
00543 std::string get_description() const;
00544 };
00545
00570 XAPIAN_VISIBILITY_DEFAULT
00571 std::string sortable_serialise(double value);
00572
00585 XAPIAN_VISIBILITY_DEFAULT
00586 double sortable_unserialise(const std::string & value);
00587
00588 }
00589
00590 #endif // XAPIAN_INCLUDED_QUERYPARSER_H