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,2017 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/deprecated.h>
32 #include <xapian/intrusive_ptr.h>
33 #include <xapian/query.h>
34 #include <xapian/termiterator.h>
35 #include <xapian/visibility.h>
36 
37 #include <set>
38 #include <string>
39 
40 namespace Xapian {
41 
42 class Database;
43 class Stem;
44 
49  void operator=(const Stopper &);
50 
52  Stopper(const Stopper &);
53 
54  public:
56  Stopper() { }
57 
62  virtual bool operator()(const std::string & term) const = 0;
63 
65  virtual ~Stopper() { }
66 
68  virtual std::string get_description() const;
69 
78  opt_intrusive_base::release();
79  return this;
80  }
81 
89  const Stopper * release() const {
90  opt_intrusive_base::release();
91  return this;
92  }
93 };
94 
97  std::set<std::string> stop_words;
98 
99  public:
102 
112  template <class Iterator>
113  SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
114 
116  void add(const std::string & word) { stop_words.insert(word); }
117 
118  virtual bool operator()(const std::string & term) const {
119  return stop_words.find(term) != stop_words.end();
120  }
121 
122  virtual std::string get_description() const;
123 };
124 
125 enum {
129 };
130 
135  void operator=(const RangeProcessor &);
136 
139 
140  protected:
146 
148  std::string str;
149 
161  unsigned flags;
162 
163  public:
165  RangeProcessor() : slot(Xapian::BAD_VALUENO), flags(0) { }
166 
184  const std::string& str_ = std::string(),
185  unsigned flags_ = 0)
186  : slot(slot_), str(str_), flags(flags_) { }
187 
189  virtual ~RangeProcessor();
190 
196  Xapian::Query check_range(const std::string& b, const std::string& e);
197 
210  virtual Xapian::Query
211  operator()(const std::string &begin, const std::string &end);
212 
221  opt_intrusive_base::release();
222  return this;
223  }
224 
232  const RangeProcessor * release() const {
233  opt_intrusive_base::release();
234  return this;
235  }
236 };
237 
244 
245  public:
260  unsigned flags_ = 0,
261  int epoch_year_ = 1970)
262  : RangeProcessor(slot_, std::string(), flags_),
263  epoch_year(epoch_year_) { }
264 
306  DateRangeProcessor(Xapian::valueno slot_, const std::string &str_,
307  unsigned flags_ = 0, int epoch_year_ = 1970)
308  : RangeProcessor(slot_, str_, flags_),
309  epoch_year(epoch_year_) { }
310 
322  Xapian::Query operator()(const std::string& begin, const std::string& end);
323 };
324 
333  public:
373  const std::string &str_ = std::string(),
374  unsigned flags_ = 0)
375  : RangeProcessor(slot_, str_, flags_) { }
376 
389  Xapian::Query operator()(const std::string& begin, const std::string& end);
390 };
391 
396  void operator=(const ValueRangeProcessor &);
397 
400 
401  public:
404 
406  virtual ~ValueRangeProcessor();
407 
422  virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
423 
432  opt_intrusive_base::release();
433  return this;
434  }
435 
443  const ValueRangeProcessor * release() const {
444  opt_intrusive_base::release();
445  return this;
446  }
447 };
448 
456  protected:
459 
461  bool prefix;
462 
464  std::string str;
465 
466  public:
472  : valno(slot_), str() { }
473 
482  StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
483  bool prefix_ = true)
484  : valno(slot_), prefix(prefix_), str(str_) { }
485 
500  Xapian::valueno operator()(std::string &begin, std::string &end);
501 };
502 
512 
513  public:
524  DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
525  int epoch_year_ = 1970)
526  : StringValueRangeProcessor(slot_),
527  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
528 
566  DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
567  bool prefix_ = true,
568  bool prefer_mdy_ = false, int epoch_year_ = 1970)
569  : StringValueRangeProcessor(slot_, str_, prefix_),
570  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
571 
572 #ifndef SWIG
573 
617  DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
618  bool prefix_ = true,
619  bool prefer_mdy_ = false, int epoch_year_ = 1970)
620  : StringValueRangeProcessor(slot_, str_, prefix_),
621  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
622 #endif
623 
639  Xapian::valueno operator()(std::string &begin, std::string &end);
640 };
641 
652  public:
658  : StringValueRangeProcessor(slot_) { }
659 
692  NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
693  bool prefix_ = true)
694  : StringValueRangeProcessor(slot_, str_, prefix_) { }
695 
713  Xapian::valueno operator()(std::string &begin, std::string &end);
714 };
715 
721  void operator=(const FieldProcessor &);
722 
725 
726  public:
729 
731  virtual ~FieldProcessor();
732 
739  virtual Xapian::Query operator()(const std::string &str) = 0;
740 
749  opt_intrusive_base::release();
750  return this;
751  }
752 
760  const FieldProcessor * release() const {
761  opt_intrusive_base::release();
762  return this;
763  }
764 };
765 
768  public:
770  class Internal;
773 
775  typedef enum {
777  FLAG_BOOLEAN = 1,
779  FLAG_PHRASE = 2,
781  FLAG_LOVEHATE = 4,
783  FLAG_BOOLEAN_ANY_CASE = 8,
798  FLAG_WILDCARD = 16,
805  FLAG_PURE_NOT = 32,
826  FLAG_PARTIAL = 64,
827 
841  FLAG_SPELLING_CORRECTION = 128,
842 
847  FLAG_SYNONYM = 256,
848 
853  FLAG_AUTO_SYNONYMS = 512,
854 
860  FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
861 
874  FLAG_CJK_NGRAM = 2048,
875 
883  FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
884  } feature_flag;
885 
887  typedef enum { STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z } stem_strategy;
888 
890  QueryParser(const QueryParser & o);
891 
893  QueryParser & operator=(const QueryParser & o);
894 
896  QueryParser();
897 
899  ~QueryParser();
900 
913  void set_stemmer(const Xapian::Stem & stemmer);
914 
934  void set_stemming_strategy(stem_strategy strategy);
935 
941  void set_stopper(const Stopper *stop = NULL);
942 
957  void set_default_op(Query::op default_op);
958 
960  Query::op get_default_op() const;
961 
968  void set_database(const Database &db);
969 
996  void set_max_expansion(Xapian::termcount max_expansion,
998  unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
999 
1012  XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
1013 
1037  Query parse_query(const std::string &query_string,
1038  unsigned flags = FLAG_DEFAULT,
1039  const std::string &default_prefix = std::string());
1040 
1080  void add_prefix(const std::string& field, const std::string& prefix);
1081 
1084  void add_prefix(const std::string& field, Xapian::FieldProcessor * proc);
1085 
1139  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1140  const std::string* grouping = NULL);
1141 
1158  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1159  bool exclusive) {
1160  if (exclusive) {
1161  add_boolean_prefix(field, prefix);
1162  } else {
1163  std::string empty_grouping;
1164  add_boolean_prefix(field, prefix, &empty_grouping);
1165  }
1166  }
1167 
1170  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1171  const std::string* grouping = NULL);
1172 
1178  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1179  bool exclusive) {
1180  if (exclusive) {
1181  add_boolean_prefix(field, proc);
1182  } else {
1183  std::string empty_grouping;
1184  add_boolean_prefix(field, proc, &empty_grouping);
1185  }
1186  }
1187 
1189  TermIterator stoplist_begin() const;
1190 
1192  TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
1193  return TermIterator();
1194  }
1195 
1197  TermIterator unstem_begin(const std::string &term) const;
1198 
1200  TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
1201  return TermIterator();
1202  }
1203 
1205  void add_rangeprocessor(Xapian::RangeProcessor * range_proc,
1206  const std::string* grouping = NULL);
1207 
1215  class ShimRangeProcessor : public RangeProcessor {
1217 
1218  public:
1219  ShimRangeProcessor(Xapian::ValueRangeProcessor * vrp_)
1220  : RangeProcessor(Xapian::BAD_VALUENO), vrp(vrp_) { }
1221 
1223  operator()(const std::string &begin, const std::string &end)
1224  {
1225  std::string b = begin, e = end;
1226  slot = (*vrp)(b, e);
1227  if (slot == Xapian::BAD_VALUENO)
1229  return RangeProcessor::operator()(b, e);
1230  }
1231  };
1232 
1233  add_rangeprocessor((new ShimRangeProcessor(vrproc))->release());
1234  }
1235 
1243  std::string get_corrected_query_string() const;
1244 
1246  std::string get_description() const;
1247 };
1248 
1249 inline void
1251 {
1252  set_max_expansion(max_expansion,
1254  FLAG_WILDCARD);
1255 }
1256 
1259 size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
1260 
1287 inline std::string sortable_serialise(double value) {
1288  char buf[9];
1289  return std::string(buf, sortable_serialise_(value, buf));
1290 }
1291 
1307 double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
1308 
1309 }
1310 
1311 #endif // XAPIAN_INCLUDED_QUERYPARSER_H
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:81
Handle a date range.
Definition: queryparser.h:509
Stopper()
Default constructor.
Definition: queryparser.h:56
void set_max_wildcard_expansion(Xapian::termcount)
Specify the maximum expansion of a wildcard.
Definition: queryparser.h:1250
RangeProcessor()
Default constructor.
Definition: queryparser.h:165
DateRangeProcessor(Xapian::valueno slot_, unsigned flags_=0, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:259
Simple implementation of Stopper class - this will suit most users.
Definition: queryparser.h:96
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:461
Class representing a stemming algorithm.
Definition: stem.h:62
void add_valuerangeprocessor(Xapian::ValueRangeProcessor *vrproc)
Register a ValueRangeProcessor.
Definition: queryparser.h:1213
Xapian::valueno valno
The value slot to process.
Definition: queryparser.h:458
Throw an error if OP_WILDCARD exceeds its expansion limit.
Definition: query.h:149
op
Query operators.
Definition: query.h:66
FieldProcessor * release()
Start reference counting this object.
Definition: queryparser.h:748
Compiler attribute macros.
Handle a number range.
Definition: queryparser.h:332
virtual bool operator()(const std::string &term) const
Is term a stop-word?
Definition: queryparser.h:118
RangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:183
TermIterator stoplist_end() const
End iterator over terms omitted from the query as stopwords.
Definition: queryparser.h:1192
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:767
Define XAPIAN_DEPRECATED() and related macros.
ValueRangeProcessor()
Default constructor.
Definition: queryparser.h:403
SimpleStopper()
Default constructor.
Definition: queryparser.h:101
SimpleStopper(Iterator begin, Iterator end)
Initialise from a pair of iterators.
Definition: queryparser.h:113
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:89
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1287
ValueRangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:431
void add(const std::string &word)
Add a single stop word.
Definition: queryparser.h:116
Base class for value range processors.
Definition: queryparser.h:393
#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:77
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:145
Handle a date range.
Definition: queryparser.h:242
Base class for field processors.
Definition: queryparser.h:718
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:1200
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:1178
DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:566
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:1158
std::string str
The prefix (or suffix if prefix==false) string to look for.
Definition: queryparser.h:464
NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:692
NumberRangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:372
Base class for objects managed by opt_intrusive_ptr.
unsigned flags
Flags.
Definition: queryparser.h:161
Handle a string range.
Definition: queryparser.h:455
Base class for range processors.
Definition: queryparser.h:132
std::string str
The prefix (or suffix with RP_SUFFIX) string to look for.
Definition: queryparser.h:148
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:471
const FieldProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:760
NumberValueRangeProcessor(Xapian::valueno slot_)
Constructor.
Definition: queryparser.h:657
virtual ~Stopper()
Class has virtual methods, so provide a virtual destructor.
Definition: queryparser.h:65
Handle a number range.
Definition: queryparser.h:651
const RangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:232
RangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:220
StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:482
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
FieldProcessor()
Default constructor.
Definition: queryparser.h:728
Base class for stop-word decision functor.
Definition: queryparser.h:46
const ValueRangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:443
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:306
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:524
std::set< std::string > stop_words
Definition: queryparser.h:97
DateValueRangeProcessor(Xapian::valueno slot_, const char *str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:617

Documentation for Xapian (version 1.4.5).
Generated on Mon Oct 16 2017 by Doxygen 1.8.8.