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
00085 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00086 virtual ~ValueRangeProcessor();
00087 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00088 };
00089
00090 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00091 Xapian::valueno valno;
00092
00093 public:
00094 StringValueRangeProcessor(Xapian::valueno valno_)
00095 : valno(valno_) { }
00096
00097 Xapian::valueno operator()(std::string &, std::string &) {
00098 return valno;
00099 }
00100 };
00101
00102 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public ValueRangeProcessor {
00103 Xapian::valueno valno;
00104 bool prefer_mdy;
00105 int epoch_year;
00106
00107 public:
00108 DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
00109 int epoch_year_ = 1970)
00110 : valno(valno_), prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00111
00112 Xapian::valueno operator()(std::string &begin, std::string &end);
00113 };
00114
00115 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00116 Xapian::valueno valno;
00117 bool prefix;
00118 std::string str;
00119
00120 public:
00121 NumberValueRangeProcessor(Xapian::valueno valno_)
00122 : valno(valno_), prefix(false) { }
00123
00124 NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00125 bool prefix_ = true)
00126 : valno(valno_), prefix(prefix_), str(str_) { }
00127
00128 Xapian::valueno operator()(std::string &begin, std::string &end);
00129 };
00130
00132 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00133 public:
00135 class Internal;
00137 Xapian::Internal::RefCntPtr<Internal> internal;
00138
00140 typedef enum {
00142 FLAG_BOOLEAN = 1,
00144 FLAG_PHRASE = 2,
00146 FLAG_LOVEHATE = 4,
00148 FLAG_BOOLEAN_ANY_CASE = 8,
00154 FLAG_WILDCARD = 16,
00161 FLAG_PURE_NOT = 32,
00170 FLAG_PARTIAL = 64
00171 } feature_flag;
00172
00173 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00174
00176 QueryParser(const QueryParser & o);
00177
00179 QueryParser & operator=(const QueryParser & o);
00180
00182 QueryParser();
00183
00185 ~QueryParser();
00186
00188 void set_stemmer(const Xapian::Stem & stemmer);
00189
00191 void set_stemming_strategy(stem_strategy strategy);
00192
00194 void set_stopper(const Stopper *stop = NULL);
00195
00197 void set_default_op(Query::op default_op);
00198
00200 Query::op get_default_op() const;
00201
00203 void set_database(const Database &db);
00204
00214 Query parse_query(const std::string &query_string,
00215 unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
00216 const std::string &default_prefix = "");
00217
00230 void add_prefix(const std::string &field, const std::string &prefix);
00231
00247 void add_boolean_prefix(const std::string & field, const std::string &prefix);
00248
00250 TermIterator stoplist_begin() const;
00251 TermIterator stoplist_end() const {
00252 return TermIterator(NULL);
00253 }
00254
00256 TermIterator unstem_begin(const std::string &term) const;
00257 TermIterator unstem_end(const std::string &) const {
00258 return TermIterator(NULL);
00259 }
00260
00262 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00263
00265 std::string get_description() const;
00266 };
00267
00268 }
00269
00270 #endif // XAPIAN_INCLUDED_QUERYPARSER_H