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 
77  opt_intrusive_base::release();
78  return this;
79  }
80 
88  const Stopper * release() const {
89  opt_intrusive_base::release();
90  return this;
91  }
92 };
93 
96  std::set<std::string> stop_words;
97 
98  public:
101 
111  template <class Iterator>
112  SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
113 
115  void add(const std::string & word) { stop_words.insert(word); }
116 
117  virtual bool operator()(const std::string & term) const {
118  return stop_words.find(term) != stop_words.end();
119  }
120 
121  virtual std::string get_description() const;
122 };
123 
124 enum {
128 };
129 
134  void operator=(const RangeProcessor &);
135 
138 
139  protected:
145 
147  std::string str;
148 
160  unsigned flags;
161 
162  public:
164  RangeProcessor() : slot(Xapian::BAD_VALUENO), flags(0) { }
165 
183  const std::string& str_ = std::string(),
184  unsigned flags_ = 0)
185  : slot(slot_), str(str_), flags(flags_) { }
186 
188  virtual ~RangeProcessor();
189 
195  Xapian::Query check_range(const std::string& b, const std::string& e);
196 
209  virtual Xapian::Query
210  operator()(const std::string &begin, const std::string &end);
211 
220  opt_intrusive_base::release();
221  return this;
222  }
223 
231  const RangeProcessor * release() const {
232  opt_intrusive_base::release();
233  return this;
234  }
235 };
236 
243 
244  public:
259  unsigned flags_ = 0,
260  int epoch_year_ = 1970)
261  : RangeProcessor(slot_, std::string(), flags_),
262  epoch_year(epoch_year_) { }
263 
305  DateRangeProcessor(Xapian::valueno slot_, const std::string &str_,
306  unsigned flags_ = 0, int epoch_year_ = 1970)
307  : RangeProcessor(slot_, str_, flags_),
308  epoch_year(epoch_year_) { }
309 
321  Xapian::Query operator()(const std::string& begin, const std::string& end);
322 };
323 
332  public:
372  const std::string &str_ = std::string(),
373  unsigned flags_ = 0)
374  : RangeProcessor(slot_, str_, flags_) { }
375 
388  Xapian::Query operator()(const std::string& begin, const std::string& end);
389 };
390 
395  void operator=(const ValueRangeProcessor &);
396 
399 
400  public:
403 
405  virtual ~ValueRangeProcessor();
406 
421  virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
422 
431  opt_intrusive_base::release();
432  return this;
433  }
434 
442  const ValueRangeProcessor * release() const {
443  opt_intrusive_base::release();
444  return this;
445  }
446 };
447 
455  protected:
458 
460  bool prefix;
461 
463  std::string str;
464 
465  public:
471  : valno(slot_), str() { }
472 
481  StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
482  bool prefix_ = true)
483  : valno(slot_), prefix(prefix_), str(str_) { }
484 
499  Xapian::valueno operator()(std::string &begin, std::string &end);
500 };
501 
511 
512  public:
523  DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
524  int epoch_year_ = 1970)
525  : StringValueRangeProcessor(slot_),
526  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
527 
565  DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
566  bool prefix_ = true,
567  bool prefer_mdy_ = false, int epoch_year_ = 1970)
568  : StringValueRangeProcessor(slot_, str_, prefix_),
569  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
570 
571 #ifndef SWIG
572 
616  DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
617  bool prefix_ = true,
618  bool prefer_mdy_ = false, int epoch_year_ = 1970)
619  : StringValueRangeProcessor(slot_, str_, prefix_),
620  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
621 #endif
622 
638  Xapian::valueno operator()(std::string &begin, std::string &end);
639 };
640 
651  public:
657  : StringValueRangeProcessor(slot_) { }
658 
691  NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
692  bool prefix_ = true)
693  : StringValueRangeProcessor(slot_, str_, prefix_) { }
694 
712  Xapian::valueno operator()(std::string &begin, std::string &end);
713 };
714 
720  void operator=(const FieldProcessor &);
721 
724 
725  public:
728 
730  virtual ~FieldProcessor();
731 
738  virtual Xapian::Query operator()(const std::string &str) = 0;
739 
748  opt_intrusive_base::release();
749  return this;
750  }
751 
759  const FieldProcessor * release() const {
760  opt_intrusive_base::release();
761  return this;
762  }
763 };
764 
767  public:
769  class Internal;
772 
774  typedef enum {
776  FLAG_BOOLEAN = 1,
778  FLAG_PHRASE = 2,
780  FLAG_LOVEHATE = 4,
782  FLAG_BOOLEAN_ANY_CASE = 8,
797  FLAG_WILDCARD = 16,
804  FLAG_PURE_NOT = 32,
825  FLAG_PARTIAL = 64,
826 
840  FLAG_SPELLING_CORRECTION = 128,
841 
846  FLAG_SYNONYM = 256,
847 
852  FLAG_AUTO_SYNONYMS = 512,
853 
859  FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
860 
873  FLAG_CJK_NGRAM = 2048,
874 
882  FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
883  } feature_flag;
884 
886  typedef enum { STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z } stem_strategy;
887 
889  QueryParser(const QueryParser & o);
890 
892  QueryParser & operator=(const QueryParser & o);
893 
895  QueryParser();
896 
898  ~QueryParser();
899 
912  void set_stemmer(const Xapian::Stem & stemmer);
913 
933  void set_stemming_strategy(stem_strategy strategy);
934 
940  void set_stopper(const Stopper *stop = NULL);
941 
956  void set_default_op(Query::op default_op);
957 
959  Query::op get_default_op() const;
960 
967  void set_database(const Database &db);
968 
993  void set_max_expansion(Xapian::termcount max_expansion,
995  unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
996 
1009  XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
1010 
1034  Query parse_query(const std::string &query_string,
1035  unsigned flags = FLAG_DEFAULT,
1036  const std::string &default_prefix = std::string());
1037 
1077  void add_prefix(const std::string& field, const std::string& prefix);
1078 
1081  void add_prefix(const std::string& field, Xapian::FieldProcessor * proc);
1082 
1136  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1137  const std::string* grouping = NULL);
1138 
1155  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1156  bool exclusive) {
1157  if (exclusive) {
1158  add_boolean_prefix(field, prefix);
1159  } else {
1160  std::string empty_grouping;
1161  add_boolean_prefix(field, prefix, &empty_grouping);
1162  }
1163  }
1164 
1167  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1168  const std::string* grouping = NULL);
1169 
1175  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1176  bool exclusive) {
1177  if (exclusive) {
1178  add_boolean_prefix(field, proc);
1179  } else {
1180  std::string empty_grouping;
1181  add_boolean_prefix(field, proc, &empty_grouping);
1182  }
1183  }
1184 
1186  TermIterator stoplist_begin() const;
1187 
1189  TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
1190  return TermIterator();
1191  }
1192 
1194  TermIterator unstem_begin(const std::string &term) const;
1195 
1197  TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
1198  return TermIterator();
1199  }
1200 
1202  void add_rangeprocessor(Xapian::RangeProcessor * range_proc,
1203  const std::string* grouping = NULL);
1204 
1212  class ShimRangeProcessor : public RangeProcessor {
1214 
1215  public:
1216  ShimRangeProcessor(Xapian::ValueRangeProcessor * vrp_)
1217  : RangeProcessor(Xapian::BAD_VALUENO), vrp(vrp_) { }
1218 
1220  operator()(const std::string &begin, const std::string &end)
1221  {
1222  std::string b = begin, e = end;
1223  slot = (*vrp)(b, e);
1224  if (slot == Xapian::BAD_VALUENO)
1226  return RangeProcessor::operator()(b, e);
1227  }
1228  };
1229 
1230  add_rangeprocessor((new ShimRangeProcessor(vrproc))->release());
1231  }
1232 
1240  std::string get_corrected_query_string() const;
1241 
1243  std::string get_description() const;
1244 };
1245 
1246 inline void
1248 {
1249  set_max_expansion(max_expansion,
1251  FLAG_WILDCARD);
1252 }
1253 
1256 size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
1257 
1284 inline std::string sortable_serialise(double value) {
1285  char buf[9];
1286  return std::string(buf, sortable_serialise_(value, buf));
1287 }
1288 
1304 double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
1305 
1306 }
1307 
1308 #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:508
Stopper()
Default constructor.
Definition: queryparser.h:55
void set_max_wildcard_expansion(Xapian::termcount)
Specify the maximum expansion of a wildcard.
Definition: queryparser.h:1247
RangeProcessor()
Default constructor.
Definition: queryparser.h:164
DateRangeProcessor(Xapian::valueno slot_, unsigned flags_=0, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:258
Simple implementation of Stopper class - this will suit most users.
Definition: queryparser.h:95
This class is used to access a database, or a group of databases.
Definition: database.h:65
bool prefix
Whether to look for str as a prefix or suffix.
Definition: queryparser.h:460
Class representing a stemming algorithm.
Definition: stem.h:62
void add_valuerangeprocessor(Xapian::ValueRangeProcessor *vrproc)
Register a ValueRangeProcessor.
Definition: queryparser.h:1210
Xapian::valueno valno
The value slot to process.
Definition: queryparser.h:457
Throw an error if OP_WILDCARD exceeds its expansion limit.
Definition: query.h:137
op
Query operators.
Definition: query.h:66
FieldProcessor * release()
Start reference counting this object.
Definition: queryparser.h:747
Compiler attribute macros.
Handle a number range.
Definition: queryparser.h:331
virtual bool operator()(const std::string &term) const
Is term a stop-word?
Definition: queryparser.h:117
RangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:182
TermIterator stoplist_end() const
End iterator over terms omitted from the query as stopwords.
Definition: queryparser.h:1189
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:766
ValueRangeProcessor()
Default constructor.
Definition: queryparser.h:402
SimpleStopper()
Default constructor.
Definition: queryparser.h:100
SimpleStopper(Iterator begin, Iterator end)
Initialise from a pair of iterators.
Definition: queryparser.h:112
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
Start reference counting this object.
Definition: queryparser.h:88
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1284
ValueRangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:430
void add(const std::string &word)
Add a single stop word.
Definition: queryparser.h:115
Base class for value range processors.
Definition: queryparser.h:392
#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()
Start reference counting this object.
Definition: queryparser.h:76
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
The value slot to process.
Definition: queryparser.h:144
Handle a date range.
Definition: queryparser.h:241
Base class for field processors.
Definition: queryparser.h:717
size_t sortable_serialise_(double value, char *buf)
TermIterator unstem_end(const std::string &) const
End iterator over unstemmed forms of the given stemmed query term.
Definition: queryparser.h:1197
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:1175
DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:565
void add_boolean_prefix(const std::string &field, const std::string &prefix, bool exclusive)
Add a boolean term prefix allowing the user to restrict a search with a boolean filter specified in t...
Definition: queryparser.h:1155
std::string str
The prefix (or suffix if prefix==false) string to look for.
Definition: queryparser.h:463
NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:691
NumberRangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:371
Base class for objects managed by opt_intrusive_ptr.
unsigned flags
Flags.
Definition: queryparser.h:160
Handle a string range.
Definition: queryparser.h:454
Base class for range processors.
Definition: queryparser.h:131
std::string str
The prefix (or suffix with RP_SUFFIX) string to look for.
Definition: queryparser.h:147
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:470
const FieldProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:759
NumberValueRangeProcessor(Xapian::valueno slot_)
Constructor.
Definition: queryparser.h:656
virtual ~Stopper()
Class has virtual methods, so provide a virtual destructor.
Definition: queryparser.h:64
Handle a number range.
Definition: queryparser.h:650
const RangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:231
RangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:219
StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:481
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
FieldProcessor()
Default constructor.
Definition: queryparser.h:727
Base class for stop-word decision functor.
Definition: queryparser.h:45
const ValueRangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:442
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:305
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:523
std::set< std::string > stop_words
Definition: queryparser.h:96
DateValueRangeProcessor(Xapian::valueno slot_, const char *str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:616

Documentation for Xapian (version 1.4.1).
Generated on Sun Oct 23 2016 by Doxygen 1.8.8.