xapian-core  1.4.18
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,2018 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 
213  virtual Xapian::Query
214  operator()(const std::string &begin, const std::string &end);
215 
224  opt_intrusive_base::release();
225  return this;
226  }
227 
235  const RangeProcessor * release() const {
236  opt_intrusive_base::release();
237  return this;
238  }
239 };
240 
247 
248  public:
263  unsigned flags_ = 0,
264  int epoch_year_ = 1970)
265  : RangeProcessor(slot_, std::string(), flags_),
266  epoch_year(epoch_year_) { }
267 
309  DateRangeProcessor(Xapian::valueno slot_, const std::string &str_,
310  unsigned flags_ = 0, int epoch_year_ = 1970)
311  : RangeProcessor(slot_, str_, flags_),
312  epoch_year(epoch_year_) { }
313 
325  Xapian::Query operator()(const std::string& begin, const std::string& end);
326 };
327 
336  public:
376  const std::string &str_ = std::string(),
377  unsigned flags_ = 0)
378  : RangeProcessor(slot_, str_, flags_) { }
379 
392  Xapian::Query operator()(const std::string& begin, const std::string& end);
393 };
394 
399  void operator=(const ValueRangeProcessor &);
400 
403 
404  public:
407 
409  virtual ~ValueRangeProcessor();
410 
425  virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
426 
435  opt_intrusive_base::release();
436  return this;
437  }
438 
446  const ValueRangeProcessor * release() const {
447  opt_intrusive_base::release();
448  return this;
449  }
450 };
451 
459  protected:
462 
464  bool prefix;
465 
467  std::string str;
468 
469  public:
475  : valno(slot_), str() { }
476 
485  StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
486  bool prefix_ = true)
487  : valno(slot_), prefix(prefix_), str(str_) { }
488 
503  Xapian::valueno operator()(std::string &begin, std::string &end);
504 };
505 
515 
516  public:
527  DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
528  int epoch_year_ = 1970)
529  : StringValueRangeProcessor(slot_),
530  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
531 
569  DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
570  bool prefix_ = true,
571  bool prefer_mdy_ = false, int epoch_year_ = 1970)
572  : StringValueRangeProcessor(slot_, str_, prefix_),
573  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
574 
575 #ifndef SWIG
576 
620  DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
621  bool prefix_ = true,
622  bool prefer_mdy_ = false, int epoch_year_ = 1970)
623  : StringValueRangeProcessor(slot_, str_, prefix_),
624  prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
625 #endif
626 
642  Xapian::valueno operator()(std::string &begin, std::string &end);
643 };
644 
655  public:
661  : StringValueRangeProcessor(slot_) { }
662 
695  NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
696  bool prefix_ = true)
697  : StringValueRangeProcessor(slot_, str_, prefix_) { }
698 
716  Xapian::valueno operator()(std::string &begin, std::string &end);
717 };
718 
724  void operator=(const FieldProcessor &);
725 
728 
729  public:
732 
734  virtual ~FieldProcessor();
735 
742  virtual Xapian::Query operator()(const std::string &str) = 0;
743 
752  opt_intrusive_base::release();
753  return this;
754  }
755 
763  const FieldProcessor * release() const {
764  opt_intrusive_base::release();
765  return this;
766  }
767 };
768 
771  public:
773  class Internal;
776 
778  typedef enum {
780  FLAG_BOOLEAN = 1,
782  FLAG_PHRASE = 2,
784  FLAG_LOVEHATE = 4,
786  FLAG_BOOLEAN_ANY_CASE = 8,
801  FLAG_WILDCARD = 16,
808  FLAG_PURE_NOT = 32,
829  FLAG_PARTIAL = 64,
830 
844  FLAG_SPELLING_CORRECTION = 128,
845 
850  FLAG_SYNONYM = 256,
851 
856  FLAG_AUTO_SYNONYMS = 512,
857 
863  FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
864 
878  FLAG_CJK_NGRAM = 2048,
879 
893  FLAG_ACCUMULATE = 65536,
894 
902  FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
903  } feature_flag;
904 
906  typedef enum {
907  STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z, STEM_SOME_FULL_POS
908  } stem_strategy;
909 
911  QueryParser(const QueryParser & o);
912 
914  QueryParser & operator=(const QueryParser & o);
915 
916 #ifdef XAPIAN_MOVE_SEMANTICS
917  QueryParser(QueryParser && o);
919 
921  QueryParser & operator=(QueryParser && o);
922 #endif
923 
925  QueryParser();
926 
928  ~QueryParser();
929 
942  void set_stemmer(const Xapian::Stem & stemmer);
943 
967  void set_stemming_strategy(stem_strategy strategy);
968 
974  void set_stopper(const Stopper *stop = NULL);
975 
990  void set_default_op(Query::op default_op);
991 
993  Query::op get_default_op() const;
994 
1001  void set_database(const Database &db);
1002 
1029  void set_max_expansion(Xapian::termcount max_expansion,
1030  int max_type = Xapian::Query::WILDCARD_LIMIT_ERROR,
1031  unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
1032 
1045  XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
1046 
1070  Query parse_query(const std::string &query_string,
1071  unsigned flags = FLAG_DEFAULT,
1072  const std::string &default_prefix = std::string());
1073 
1113  void add_prefix(const std::string& field, const std::string& prefix);
1114 
1117  void add_prefix(const std::string& field, Xapian::FieldProcessor * proc);
1118 
1172  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1173  const std::string* grouping = NULL);
1174 
1191  void add_boolean_prefix(const std::string &field, const std::string &prefix,
1192  bool exclusive) {
1193  if (exclusive) {
1194  add_boolean_prefix(field, prefix);
1195  } else {
1196  std::string empty_grouping;
1197  add_boolean_prefix(field, prefix, &empty_grouping);
1198  }
1199  }
1200 
1203  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1204  const std::string* grouping = NULL);
1205 
1211  void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
1212  bool exclusive) {
1213  if (exclusive) {
1214  add_boolean_prefix(field, proc);
1215  } else {
1216  std::string empty_grouping;
1217  add_boolean_prefix(field, proc, &empty_grouping);
1218  }
1219  }
1220 
1222  TermIterator stoplist_begin() const;
1223 
1225  TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
1226  return TermIterator();
1227  }
1228 
1230  TermIterator unstem_begin(const std::string &term) const;
1231 
1233  TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
1234  return TermIterator();
1235  }
1236 
1238  void add_rangeprocessor(Xapian::RangeProcessor * range_proc,
1239  const std::string* grouping = NULL);
1240 
1248  class ShimRangeProcessor : public RangeProcessor {
1250 
1251  public:
1252  ShimRangeProcessor(Xapian::ValueRangeProcessor * vrp_)
1253  : RangeProcessor(Xapian::BAD_VALUENO), vrp(vrp_) { }
1254 
1256  operator()(const std::string &begin, const std::string &end)
1257  {
1258  std::string b = begin, e = end;
1259  slot = (*vrp)(b, e);
1260  if (slot == Xapian::BAD_VALUENO)
1262  return RangeProcessor::operator()(b, e);
1263  }
1264  };
1265 
1266  add_rangeprocessor((new ShimRangeProcessor(vrproc))->release());
1267  }
1268 
1276  std::string get_corrected_query_string() const;
1277 
1279  std::string get_description() const;
1280 };
1281 
1282 inline void
1284 {
1285  set_max_expansion(max_expansion,
1287  FLAG_WILDCARD);
1288 }
1289 
1292 size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
1293 
1320 inline std::string sortable_serialise(double value) {
1321  char buf[9];
1322  return std::string(buf, sortable_serialise_(value, buf));
1323 }
1324 
1340 double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
1341 
1342 }
1343 
1344 #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:512
Stopper()
Default constructor.
Definition: queryparser.h:56
void set_max_wildcard_expansion(Xapian::termcount)
Specify the maximum expansion of a wildcard.
Definition: queryparser.h:1283
RangeProcessor()
Default constructor.
Definition: queryparser.h:165
DateRangeProcessor(Xapian::valueno slot_, unsigned flags_=0, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:262
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:68
bool prefix
Whether to look for str as a prefix or suffix.
Definition: queryparser.h:464
Class representing a stemming algorithm.
Definition: stem.h:62
void add_valuerangeprocessor(Xapian::ValueRangeProcessor *vrproc)
Register a ValueRangeProcessor.
Definition: queryparser.h:1246
Xapian::valueno valno
The value slot to process.
Definition: queryparser.h:461
op
Query operators.
Definition: query.h:78
FieldProcessor * release()
Start reference counting this object.
Definition: queryparser.h:751
Compiler attribute macros.
Handle a number range.
Definition: queryparser.h:335
RangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:183
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:770
Define XAPIAN_DEPRECATED() and related macros.
ValueRangeProcessor()
Default constructor.
Definition: queryparser.h:406
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.
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1320
ValueRangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:434
void add(const std::string &word)
Add a single stop word.
Definition: queryparser.h:116
const Stopper * release() const
Start reference counting this object.
Definition: queryparser.h:89
static Xapian::Stem stemmer
Definition: stemtest.cc:41
Base class for value range processors.
Definition: queryparser.h:396
#define XAPIAN_DEPRECATED(X)
virtual bool operator()(const std::string &term) const
Is term a stop-word?
Definition: queryparser.h:118
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
Xapian::Query API class.
Stopper * release()
Start reference counting this object.
Definition: queryparser.h:77
TermIterator unstem_end(const std::string &) const
End iterator over unstemmed forms of the given stemmed query term.
Definition: queryparser.h:1233
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:245
Base class for field processors.
Definition: queryparser.h:721
size_t sortable_serialise_(double value, char *buf)
Define XAPIAN_VISIBILITY_* macros.
stem_strategy
Stemming strategies, for use with set_stemming_strategy().
Definition: queryparser.h:906
#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:1211
TermIterator stoplist_end() const
End iterator over terms omitted from the query as stopwords.
Definition: queryparser.h:1225
DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true, bool prefer_mdy_=false, int epoch_year_=1970)
Constructor.
Definition: queryparser.h:569
Throw an error if OP_WILDCARD exceeds its expansion limit.
Definition: query.h:282
const ValueRangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:446
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:1191
std::string str
The prefix (or suffix if prefix==false) string to look for.
Definition: queryparser.h:467
Construct an invalid query.
Definition: query.h:254
NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:695
NumberRangeProcessor(Xapian::valueno slot_, const std::string &str_=std::string(), unsigned flags_=0)
Constructor.
Definition: queryparser.h:375
Base class for objects managed by opt_intrusive_ptr.
unsigned flags
Flags.
Definition: queryparser.h:161
Handle a string range.
Definition: queryparser.h:458
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:474
NumberValueRangeProcessor(Xapian::valueno slot_)
Constructor.
Definition: queryparser.h:660
virtual ~Stopper()
Class has virtual methods, so provide a virtual destructor.
Definition: queryparser.h:65
const RangeProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:235
Handle a number range.
Definition: queryparser.h:654
const FieldProcessor * release() const
Start reference counting this object.
Definition: queryparser.h:763
RangeProcessor * release()
Start reference counting this object.
Definition: queryparser.h:223
StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_, bool prefix_=true)
Constructor.
Definition: queryparser.h:485
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
FieldProcessor()
Default constructor.
Definition: queryparser.h:731
feature_flag
Enum of feature flags.
Definition: queryparser.h:778
Base class for stop-word decision functor.
Definition: queryparser.h:46
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:309
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:527
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:620