00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef XAPIAN_INCLUDED_QUERYPARSER_H
00024 #define XAPIAN_INCLUDED_QUERYPARSER_H
00025
00026 #include <xapian/base.h>
00027 #include <xapian/query.h>
00028 #include <xapian/termiterator.h>
00029 #include <xapian/visibility.h>
00030
00031 #include <set>
00032 #include <string>
00033
00034 namespace Xapian {
00035
00036 class Database;
00037 class Stem;
00038
00040 class XAPIAN_VISIBILITY_DEFAULT Stopper {
00041 public:
00046 virtual bool operator()(const std::string & term) const = 0;
00047
00049 virtual ~Stopper() { }
00050
00052 virtual std::string get_description() const;
00053 };
00054
00056 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
00057 std::set<std::string> stop_words;
00058
00059 public:
00061 SimpleStopper() { }
00062
00064 #if ! defined __SUNPRO_CC || __SUNPRO_CC - 0 >= 0x580
00065 template <class Iterator>
00066 SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
00067 #else
00068
00069
00070 template <class Iterator>
00071 SimpleStopper(Iterator begin, Iterator end) {
00072 while (begin != end) stop_words.insert(*begin++);
00073 }
00074 #endif
00075
00077 void add(const std::string & word) { stop_words.insert(word); }
00078
00079 virtual bool operator()(const std::string & term) const {
00080 return stop_words.find(term) != stop_words.end();
00081 }
00082
00083 virtual std::string get_description() const;
00084 };
00085
00087 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00089 virtual ~ValueRangeProcessor();
00090
00105 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00106 };
00107
00112 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00113 protected:
00114 Xapian::valueno valno;
00115
00116 private:
00117 bool prefix;
00118 std::string str;
00119
00120 public:
00125 StringValueRangeProcessor(Xapian::valueno slot_)
00126 : valno(slot_), str() { }
00127
00136 StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
00137 bool prefix_ = true)
00138 : valno(slot_), prefix(prefix_), str(str_) { }
00139
00154 Xapian::valueno operator()(std::string &begin, std::string &end);
00155 };
00156
00161 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public StringValueRangeProcessor {
00162 bool prefer_mdy;
00163 int epoch_year;
00164
00165 public:
00176 DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
00177 int epoch_year_ = 1970)
00178 : StringValueRangeProcessor(slot_),
00179 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00180
00218 DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
00219 bool prefix_ = true,
00220 bool prefer_mdy_ = false, int epoch_year_ = 1970)
00221 : StringValueRangeProcessor(slot_, str_, prefix_),
00222 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00223
00239 Xapian::valueno operator()(std::string &begin, std::string &end);
00240 };
00241
00249 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public StringValueRangeProcessor {
00250 public:
00255 NumberValueRangeProcessor(Xapian::valueno slot_)
00256 : StringValueRangeProcessor(slot_) { }
00257
00290 NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
00291 bool prefix_ = true)
00292 : StringValueRangeProcessor(slot_, str_, prefix_) { }
00293
00311 Xapian::valueno operator()(std::string &begin, std::string &end);
00312 };
00313
00315 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00316 public:
00318 class Internal;
00320 Xapian::Internal::RefCntPtr<Internal> internal;
00321
00323 typedef enum {
00325 FLAG_BOOLEAN = 1,
00327 FLAG_PHRASE = 2,
00329 FLAG_LOVEHATE = 4,
00331 FLAG_BOOLEAN_ANY_CASE = 8,
00341 FLAG_WILDCARD = 16,
00348 FLAG_PURE_NOT = 32,
00366 FLAG_PARTIAL = 64,
00367
00381 FLAG_SPELLING_CORRECTION = 128,
00382
00387 FLAG_SYNONYM = 256,
00388
00393 FLAG_AUTO_SYNONYMS = 512,
00394
00400 FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS,
00401
00409 FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
00410 } feature_flag;
00411
00412 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00413
00415 QueryParser(const QueryParser & o);
00416
00418 QueryParser & operator=(const QueryParser & o);
00419
00421 QueryParser();
00422
00424 ~QueryParser();
00425
00436 void set_stemmer(const Xapian::Stem & stemmer);
00437
00454 void set_stemming_strategy(stem_strategy strategy);
00455
00461 void set_stopper(const Stopper *stop = NULL);
00462
00475 void set_default_op(Query::op default_op);
00476
00478 Query::op get_default_op() const;
00479
00487 void set_database(const Database &db);
00488
00496 void set_max_wildcard_expansion(Xapian::termcount limit);
00497
00521 Query parse_query(const std::string &query_string,
00522 unsigned flags = FLAG_DEFAULT,
00523 const std::string &default_prefix = std::string());
00524
00564 void add_prefix(const std::string &field, const std::string &prefix);
00565
00618 void add_boolean_prefix(const std::string &field, const std::string &prefix,
00619 bool exclusive);
00620
00621
00622
00623 void add_boolean_prefix(const std::string &field, const std::string &prefix);
00624
00626 TermIterator stoplist_begin() const;
00627 TermIterator stoplist_end() const {
00628 return TermIterator();
00629 }
00630
00632 TermIterator unstem_begin(const std::string &term) const;
00633 TermIterator unstem_end(const std::string &) const {
00634 return TermIterator();
00635 }
00636
00638 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00639
00647 std::string get_corrected_query_string() const;
00648
00650 std::string get_description() const;
00651 };
00652
00679 XAPIAN_VISIBILITY_DEFAULT
00680 std::string sortable_serialise(double value);
00681
00696 XAPIAN_VISIBILITY_DEFAULT
00697 double sortable_unserialise(const std::string & value);
00698
00699 }
00700
00701 #endif // XAPIAN_INCLUDED_QUERYPARSER_H