queryparser.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Olly Betts
5  * Copyright (C) 2010 Adam Sj√łgren
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 
23 #ifndef XAPIAN_INCLUDED_QUERYPARSER_H
24 #define XAPIAN_INCLUDED_QUERYPARSER_H
25 
26 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
27 # error "Never use <xapian/queryparser.h> directly; include <xapian.h> instead."
28 #endif
29 
30 #include <xapian/attributes.h>
31 #include <xapian/intrusive_ptr.h>
32 #include <xapian/query.h>
33 #include <xapian/termiterator.h>
34 #include <xapian/visibility.h>
35 
36 #include <set>
37 #include <string>
38 
39 namespace Xapian {
40 
41 class Database;
42 class Stem;
43 
48  void operator=(const Stopper &);
49 
51  Stopper(const Stopper &);
52 
53  public:
55  Stopper() { }
56 
61  virtual bool operator()(const std::string & term) const = 0;
62 
64  virtual ~Stopper() { }
65 
67  virtual std::string get_description() const;
68 
70  opt_intrusive_base::release();
71  return this;
72  }
73 
74  const Stopper * release() const {
75  opt_intrusive_base::release();
76  return this;
77  }
78 };
79 
82  std::set<std::string> stop_words;
83 
84  public:
87 
97  template <class Iterator>
98  SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
99 
101  void add(const std::string & word) { stop_words.insert(word); }
102 
103  virtual bool operator()(const std::string & term) const {
104  return stop_words.find(term) != stop_words.end();
105  }
106 
107  virtual std::string get_description() const;
108 };
109 
110 enum {
114 };
115 
120  void operator=(const RangeProcessor &);
121 
124 
125  protected:
127 
128  std::string str;
129 
130  unsigned flags;
131 
132  public:
134  RangeProcessor() : slot(Xapian::BAD_VALUENO), flags(0) { }
135 
153  const std::string& str_ = std::string(),
154  unsigned flags_ = 0)
155  : slot(slot_), str(str_), flags(flags_) { }
156 
158  virtual ~RangeProcessor();
159 
160  Xapian::Query check_range(const std::string& b, const std::string& e);
161 
172  virtual Xapian::Query
173  operator()(const std::string &begin, const std::string &end);
174 
176  opt_intrusive_base::release();
177  return this;
178  }
179 
180  const RangeProcessor * release() const {
181  opt_intrusive_base::release();
182  return this;
183  }
184 };
185 
192 
193  public:
208  unsigned flags_ = 0,
209  int epoch_year_ = 1970)
210  : RangeProcessor(slot_, std::string(), flags_),
211  epoch_year(epoch_year_) { }
212 
254  DateRangeProcessor(Xapian::valueno slot_, const std::string &str_,
255  unsigned flags_ = 0, int epoch_year_ = 1970)
256  : RangeProcessor(slot_, str_, flags_),
257  epoch_year(epoch_year_) { }
258 
270  Xapian::Query operator()(const std::string& begin, const std::string& end);
271 };
272 
281  public:
321  const std::string &str_ = std::string(),
322  unsigned flags_ = 0)
323  : RangeProcessor(slot_, str_, flags_) { }
324 
337  Xapian::Query operator()(const std::string& begin, const std::string& end);
338 };
339 
344  void operator=(const ValueRangeProcessor &);
345 
348 
349  public:
352 
354  virtual ~ValueRangeProcessor();
355 
370  virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
371 
373  opt_intrusive_base::release();
374  return this;
375  }
376 
377  const ValueRangeProcessor * release() const {
378  opt_intrusive_base::release();
379  return this;
380  }
381 };
382 
390  protected:
392 
393  bool prefix;
394 
395  std::string str;
396 
397  public:
403  : valno(slot_), str() { }
404 
413  StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
414  bool prefix_ = true)
415  : valno(slot_), prefix(prefix_), str(str_) { }
416 
431  Xapian::valueno operator()(std::string &begin, std::string &end);
432 };
433 
443 
444  public:
455  DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
456  int epoch_year_ = 1970)
457  : StringValueRangeProcessor(slot_),
458  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
459 
497  DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
498  bool prefix_ = true,
499  bool prefer_mdy_ = false, int epoch_year_ = 1970)
500  : StringValueRangeProcessor(slot_, str_, prefix_),
501  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
502 
503 #ifndef SWIG
504 
548  DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
549  bool prefix_ = true,
550  bool prefer_mdy_ = false, int epoch_year_ = 1970)
551  : StringValueRangeProcessor(slot_, str_, prefix_),
552  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
553 #endif
554 
570  Xapian::valueno operator()(std::string &begin, std::string &end);
571 };
572 
583  public:
589  : StringValueRangeProcessor(slot_) { }
590 
623  NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
624  bool prefix_ = true)
625  : StringValueRangeProcessor(slot_, str_, prefix_) { }
626 
644  Xapian::valueno operator()(std::string &begin, std::string &end);
645 };
646 
652  void operator=(const FieldProcessor &);
653 
656 
657  public:
660 
662  virtual ~FieldProcessor();
663 
670  virtual Xapian::Query operator()(const std::string &str) = 0;
671 
673  opt_intrusive_base::release();
674  return this;
675  }
676 
677  const FieldProcessor * release() const {
678  opt_intrusive_base::release();
679  return this;
680  }
681 };
682 
685  public:
687  class Internal;
690 
692  typedef enum {
694  FLAG_BOOLEAN = 1,
696  FLAG_PHRASE = 2,
698  FLAG_LOVEHATE = 4,
700  FLAG_BOOLEAN_ANY_CASE = 8,
715  FLAG_WILDCARD = 16,
722  FLAG_PURE_NOT = 32,
743  FLAG_PARTIAL = 64,
744 
758  FLAG_SPELLING_CORRECTION = 128,
759 
764  FLAG_SYNONYM = 256,
765 
770  FLAG_AUTO_SYNONYMS = 512,
771 
777  FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
778 
791  FLAG_CJK_NGRAM = 2048,
792 
800  FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
801  } feature_flag;
802 
804  typedef enum { STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z } stem_strategy;
805 
807  QueryParser(const QueryParser & o);
808 
810  QueryParser & operator=(const QueryParser & o);
811 
813  QueryParser();
814 
816  ~QueryParser();
817 
830  void set_stemmer(const Xapian::Stem & stemmer);
831 
851  void set_stemming_strategy(stem_strategy strategy);
852 
858  void set_stopper(const Stopper *stop = NULL);
859 
874  void set_default_op(Query::op default_op);
875 
877  Query::op get_default_op() const;
878 
885  void set_database(const Database &db);
886 
911  void set_max_expansion(Xapian::termcount max_expansion,
913  unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
914 
927  XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
928 
952  Query parse_query(const std::string &query_string,
953  unsigned flags = FLAG_DEFAULT,
954  const std::string &default_prefix = std::string());
955 
995  void add_prefix(const std::string& field, const std::string& prefix);
996 
999  void add_prefix(const std::string& field, Xapian::FieldProcessor * proc);
1000 
1054  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1055  const std::string* grouping = NULL);
1056 
1057  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1058  bool exclusive) {
1059  if (exclusive) {
1060  add_boolean_prefix(field, prefix);
1061  } else {
1062  std::string empty_grouping;
1063  add_boolean_prefix(field, prefix, &empty_grouping);
1064  }
1065  }
1066 
1069  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1070  const std::string* grouping = NULL);
1071 
1074  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1075  bool exclusive) {
1076  if (exclusive) {
1077  add_boolean_prefix(field, proc);
1078  } else {
1079  std::string empty_grouping;
1080  add_boolean_prefix(field, proc, &empty_grouping);
1081  }
1082  }
1083 
1085  TermIterator stoplist_begin() const;
1086  TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
1087  return TermIterator();
1088  }
1089 
1091  TermIterator unstem_begin(const std::string &term) const;
1092  TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
1093  return TermIterator();
1094  }
1095 
1097  void add_rangeprocessor(Xapian::RangeProcessor * range_proc,
1098  const std::string* grouping = NULL);
1099 
1107  class ShimRangeProcessor : public RangeProcessor {
1109 
1110  public:
1111  ShimRangeProcessor(Xapian::ValueRangeProcessor * vrp_)
1112  : RangeProcessor(Xapian::BAD_VALUENO), vrp(vrp_) { }
1113 
1115  operator()(const std::string &begin, const std::string &end)
1116  {
1117  std::string b = begin, e = end;
1118  slot = (*vrp)(b, e);
1119  if (slot == Xapian::BAD_VALUENO)
1121  return RangeProcessor::operator()(b, e);
1122  }
1123  };
1124 
1125  add_rangeprocessor((new ShimRangeProcessor(vrproc))->release());
1126  }
1127 
1135  std::string get_corrected_query_string() const;
1136 
1138  std::string get_description() const;
1139 };
1140 
1141 inline void
1143 {
1144  set_max_expansion(max_expansion,
1146  FLAG_WILDCARD);
1147 }
1148 
1151 size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
1152 
1179 inline std::string sortable_serialise(double value) {
1180  char buf[9];
1181  return std::string(buf, sortable_serialise_(value, buf));
1182 }
1183 
1199 double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
1200 
1201 }
1202 
1203 #endif // XAPIAN_INCLUDED_QUERYPARSER_H
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
Handle a date range.
Definition: queryparser.h:440
Stopper()
Default constructor.
Definition: queryparser.h:55
void set_max_wildcard_expansion(Xapian::termcount)
Specify the maximum expansion of a wildcard.
Definition: queryparser.h:1142
RangeProcessor()
Default constructor.
Definition: queryparser.h:134
DateRangeProcessor(Xapian::valueno slot_, unsigned flags_=0, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:207
Simple implementation of Stopper class - this will suit most users.
Definition: queryparser.h:81
This class is used to access a database, or a group of databases.
Definition: database.h:65
Class representing a stemming algorithm.
Definition: stem.h:62
void add_valuerangeprocessor(Xapian::ValueRangeProcessor *vrproc)
Register a ValueRangeProcessor.
Definition: queryparser.h:1105
Throw an error if OP_WILDCARD exceeds its expansion limit.
Definition: query.h:128
op
Query operators.
Definition: query.h:57
FieldProcessor * release()
Definition: queryparser.h:672
Compiler attribute macros.
Handle a number range.
Definition: queryparser.h:280
virtual bool operator()(const std::string &term) const
Is term a stop-word?
Definition: queryparser.h:103
RangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:152
TermIterator stoplist_end() const
Definition: queryparser.h:1086
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:684
ValueRangeProcessor()
Default constructor.
Definition: queryparser.h:351
SimpleStopper()
Default constructor.
Definition: queryparser.h:86
SimpleStopper(Iterator begin, Iterator end)
Initialise from a pair of iterators.
Definition: queryparser.h:98
STL namespace.
virtual Xapian::Query operator()(const std::string &begin, const std::string &end)
Check for a valid range of this type.
const Stopper * release() const
Definition: queryparser.h:74
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1179
ValueRangeProcessor * release()
Definition: queryparser.h:372
void add(const std::string &word)
Add a single stop word.
Definition: queryparser.h:101
Base class for value range processors.
Definition: queryparser.h:341
#define XAPIAN_DEPRECATED(X)
void set_max_expansion(Xapian::termcount max_expansion, int max_type=Xapian::Query::WILDCARD_LIMIT_ERROR, unsigned flags=FLAG_WILDCARD|FLAG_PARTIAL)
Specify the maximum expansion of a wildcard and/or partial term.
Definition: queryparser.cc:141
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:31
Xapian::Query API class.
Stopper * release()
Definition: queryparser.h:69
Class for iterating over a list of terms.
Definition: termiterator.h:41
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
Xapian::valueno slot
Definition: queryparser.h:126
Handle a date range.
Definition: queryparser.h:190
Base class for field processors.
Definition: queryparser.h:649
size_t sortable_serialise_(double value, char *buf)
TermIterator unstem_end(const std::string &) const
Definition: queryparser.h:1092
static Stem stemmer
Define XAPIAN_VISIBILITY macro.
#define XAPIAN_DEPRECATED_CLASS_EX
Definition: deprecated.h:39
void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc, bool exclusive)
Register a FieldProcessor for a boolean prefix.
Definition: queryparser.h:1074
DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:497
void add_boolean_prefix(const std::string &field, const std::string &prefix, bool exclusive)
Definition: queryparser.h:1057
NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:623
NumberRangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:320
Base class for objects managed by opt_intrusive_ptr.
Handle a string range.
Definition: queryparser.h:389
Base class for range processors.
Definition: queryparser.h:117
double sortable_unserialise(const std::string &serialised)
Convert a string encoded using sortable_serialise back to a floating point number.
StringValueRangeProcessor(Xapian::valueno slot_)
Constructor.
Definition: queryparser.h:402
const FieldProcessor * release() const
Definition: queryparser.h:677
NumberValueRangeProcessor(Xapian::valueno slot_)
Constructor.
Definition: queryparser.h:588
virtual ~Stopper()
Class has virtual methods, so provide a virtual destructor.
Definition: queryparser.h:64
Handle a number range.
Definition: queryparser.h:582
const RangeProcessor * release() const
Definition: queryparser.h:180
RangeProcessor * release()
Definition: queryparser.h:175
StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:413
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
FieldProcessor()
Default constructor.
Definition: queryparser.h:659
Base class for stop-word decision functor.
Definition: queryparser.h:45
const ValueRangeProcessor * release() const
Definition: queryparser.h:377
Class representing a query.
Definition: query.h:46
const valueno BAD_VALUENO
Reserved value to indicate "no valueno".
Definition: types.h:125
DateRangeProcessor(Xapian::valueno slot_, const std::string &str_, unsigned flags_=0, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:254
A smart pointer that optionally uses intrusive reference counting.
A smart pointer that uses intrusive reference counting.
Definition: intrusive_ptr.h:81
Class for iterating over a list of terms.
DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:455
std::set< std::string > stop_words
Definition: queryparser.h:82
DateValueRangeProcessor(Xapian::valueno slot_, const char *str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:548

Documentation for Xapian (version 1.4.0).
Generated on Wed Sep 28 2016 by Doxygen 1.8.8.