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/stem.h>
00028 #include <xapian/termiterator.h>
00029 #include <xapian/visibility.h>
00030
00031 #include <set>
00032 #include <string>
00033
00034 namespace Xapian {
00035
00037 class XAPIAN_VISIBILITY_DEFAULT Stopper {
00038 public:
00040 virtual bool operator()(const std::string & term) const = 0;
00041
00043 virtual ~Stopper() { }
00044
00046 virtual std::string get_description() const;
00047 };
00048
00050 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
00051 private:
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 ~SimpleStopper() { }
00080
00082 virtual std::string get_description() const;
00083 };
00084
00086 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00088 virtual ~ValueRangeProcessor();
00089
00096 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00097 };
00098
00103 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00104 Xapian::valueno valno;
00105
00106 public:
00111 StringValueRangeProcessor(Xapian::valueno valno_)
00112 : valno(valno_) { }
00113
00115 Xapian::valueno operator()(std::string &, std::string &) {
00116 return valno;
00117 }
00118 };
00119
00124 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public ValueRangeProcessor {
00125 Xapian::valueno valno;
00126 bool prefer_mdy;
00127 int epoch_year;
00128
00129 public:
00140 DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
00141 int epoch_year_ = 1970)
00142 : valno(valno_), prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00143
00150 Xapian::valueno operator()(std::string &begin, std::string &end);
00151 };
00152
00160 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00161 Xapian::valueno valno;
00162 bool prefix;
00163 std::string str;
00164
00165 public:
00166 NumberValueRangeProcessor(Xapian::valueno valno_)
00167 : valno(valno_), prefix(false) { }
00168
00169 NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00170 bool prefix_ = true)
00171 : valno(valno_), prefix(prefix_), str(str_) { }
00172
00173 Xapian::valueno operator()(std::string &begin, std::string &end);
00174 };
00175
00177 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00178 public:
00180 class Internal;
00182 Xapian::Internal::RefCntPtr<Internal> internal;
00183
00185 typedef enum {
00187 FLAG_BOOLEAN = 1,
00189 FLAG_PHRASE = 2,
00191 FLAG_LOVEHATE = 4,
00193 FLAG_BOOLEAN_ANY_CASE = 8,
00199 FLAG_WILDCARD = 16,
00206 FLAG_PURE_NOT = 32,
00215 FLAG_PARTIAL = 64
00216 } feature_flag;
00217
00218 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00219
00221 QueryParser(const QueryParser & o);
00222
00224 QueryParser & operator=(const QueryParser & o);
00225
00227 QueryParser();
00228
00230 ~QueryParser();
00231
00233 void set_stemmer(const Xapian::Stem & stemmer);
00234
00236 void set_stemming_strategy(stem_strategy strategy);
00237
00239 void set_stopper(const Stopper *stop = NULL);
00240
00242 void set_default_op(Query::op default_op);
00243
00245 Query::op get_default_op() const;
00246
00248 void set_database(const Database &db);
00249
00259 Query parse_query(const std::string &query_string,
00260 unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
00261 const std::string &default_prefix = "");
00262
00275 void add_prefix(const std::string &field, const std::string &prefix);
00276
00303 void add_boolean_prefix(const std::string & field, const std::string &prefix);
00304
00306 TermIterator stoplist_begin() const;
00307 TermIterator stoplist_end() const {
00308 return TermIterator(NULL);
00309 }
00310
00312 TermIterator unstem_begin(const std::string &term) const;
00313 TermIterator unstem_end(const std::string &) const {
00314 return TermIterator(NULL);
00315 }
00316
00318 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00319
00321 std::string get_description() const;
00322 };
00323
00324 }
00325
00326 #endif // XAPIAN_INCLUDED_QUERYPARSER_H