xapian-core  1.4.27
queryparser.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2006,2007,2008,2010,2011,2012,2013,2015,2016,2024 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 #include <config.h>
24 
25 #include "xapian/error.h"
26 #include <xapian/queryparser.h>
27 #include <xapian/termiterator.h>
28 
29 #include "api/vectortermlist.h"
30 #include "omassert.h"
31 #include "queryparser_internal.h"
32 #include "stringutils.h"
33 
34 #include <cstring>
35 
36 using namespace Xapian;
37 
38 using namespace std;
39 
40 // Default implementation in case the user hasn't implemented it.
41 string
43 {
44  return "Xapian::Stopper subclass";
45 }
46 
47 string
49 {
50  string desc("Xapian::SimpleStopper(");
51  set<string>::const_iterator i;
52  for (i = stop_words.begin(); i != stop_words.end(); ++i) {
53  if (i != stop_words.begin()) desc += ' ';
54  desc += *i;
55  }
56  desc += ')';
57  return desc;
58 }
59 
61 
63 
65 
66 QueryParser::QueryParser(const QueryParser & o) : internal(o.internal) { }
67 
70 {
71  internal = o.internal;
72  return *this;
73 }
74 
76 
79 
81 
83 
84 void
86 {
87  internal->stemmer = stemmer;
88 }
89 
90 void
92 {
93  internal->stem_action = strategy;
94 }
95 
96 void
98 {
99  internal->stopper = stopper;
100 }
101 
102 void
104 {
105  switch (default_op) {
106  case Query::OP_AND:
107  case Query::OP_OR:
108  case Query::OP_NEAR:
109  case Query::OP_PHRASE:
110  case Query::OP_ELITE_SET:
111  case Query::OP_SYNONYM:
112  case Query::OP_MAX:
113  // These are OK.
114  break;
115  default:
117  "QueryParser::set_default_op() only accepts "
118  "OP_AND"
119  ", "
120  "OP_OR"
121  ", "
122  "OP_NEAR"
123  ", "
124  "OP_PHRASE"
125  ", "
126  "OP_ELITE_SET"
127  ", "
128  "OP_SYNONYM"
129  " or "
130  "OP_MAX");
131  }
132  internal->default_op = default_op;
133 }
134 
135 Query::op
137 {
138  return internal->default_op;
139 }
140 
141 void
143  internal->db = db;
144 }
145 
146 void
148  int max_type,
149  unsigned flags)
150 {
151  if (flags & FLAG_WILDCARD) {
152  internal->max_wildcard_expansion = max_expansion;
153  internal->max_wildcard_type = max_type;
154  }
155  if (flags & FLAG_PARTIAL) {
156  internal->max_partial_expansion = max_expansion;
157  internal->max_partial_type = max_type;
158  }
159 }
160 
161 Query
162 QueryParser::parse_query(const string &query_string, unsigned flags,
163  const string &default_prefix)
164 {
165  if (!(flags & FLAG_ACCUMULATE)) {
166  internal->stoplist.clear();
167  internal->unstem.clear();
168  }
169  internal->errmsg = NULL;
170 
171  if (query_string.empty()) return Query();
172 
173  Query result = internal->parse_query(query_string, flags, default_prefix);
174  if (internal->errmsg && strcmp(internal->errmsg, "parse error") == 0) {
175  flags &= FLAG_NGRAMS | FLAG_NO_POSITIONS;
176  result = internal->parse_query(query_string, flags, default_prefix);
177  }
178 
179  if (internal->errmsg) throw Xapian::QueryParserError(internal->errmsg);
180  return result;
181 }
182 
183 void
184 QueryParser::add_prefix(const string &field, const string &prefix)
185 {
186  Assert(internal.get());
187  if (endswith(field, ':')) {
188  internal->add_prefix(field.substr(0, field.size() - 1), prefix);
189  } else {
190  internal->add_prefix(field, prefix);
191  }
192 }
193 
194 void
196 {
197  Assert(internal.get());
198  if (endswith(field, ':')) {
199  internal->add_prefix(field.substr(0, field.size() - 1), proc);
200  } else {
201  internal->add_prefix(field, proc);
202  }
203 }
204 
205 void
206 QueryParser::add_boolean_prefix(const string &field, const string &prefix,
207  const string* grouping)
208 {
209  Assert(internal.get());
210  if (endswith(field, ':')) {
211  internal->add_boolean_prefix(field.substr(0, field.size() - 1),
212  prefix, grouping);
213  } else {
214  internal->add_boolean_prefix(field, prefix, grouping);
215  }
216 }
217 
218 void
220  Xapian::FieldProcessor * proc,
221  const string* grouping)
222 {
223  Assert(internal.get());
224  if (endswith(field, ':')) {
225  internal->add_boolean_prefix(field.substr(0, field.size() - 1),
226  proc, grouping);
227  } else {
228  internal->add_boolean_prefix(field, proc, grouping);
229  }
230 }
231 
234 {
235  const list<string> & sl = internal->stoplist;
236  return TermIterator(new VectorTermList(sl.begin(), sl.end()));
237 }
238 
240 QueryParser::unstem_begin(const string &term) const
241 {
242  struct range_adaptor : public multimap<string, string>::iterator {
243  range_adaptor(multimap<string, string>::iterator i) :
244  multimap<string, string>::iterator(i) {}
245  const string & operator*() const { return (*this)->second; }
246  };
247  auto range = internal->unstem.equal_range(term);
248  return TermIterator(new VectorTermList(range_adaptor(range.first),
249  range_adaptor(range.second)));
250 }
251 
252 void
254  const std::string* grouping)
255 {
256  Assert(internal.get());
257  internal->rangeprocs.push_back(RangeProc(range_proc, grouping));
258 }
259 
260 string
262 {
263  return internal->corrected_query;
264 }
265 
266 string
268 {
269  // FIXME : describe better!
270  return "Xapian::QueryParser()";
271 }
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
bool endswith(const std::string &s, char sfx)
Definition: stringutils.h:75
#define Assert(COND)
Definition: omassert.h:122
void set_default_op(Query::op default_op)
Set the default operator.
Definition: queryparser.cc:103
This class is used to access a database, or a group of databases.
Definition: database.h:68
virtual ~ValueRangeProcessor()
Destructor.
Definition: queryparser.cc:62
Class representing a stemming algorithm.
Definition: stem.h:62
void set_stopper(const Stopper *stop=NULL)
Set the stopper.
Definition: queryparser.cc:97
op
Query operators.
Definition: query.h:78
std::string get_corrected_query_string() const
Get the spelling-corrected query string.
Definition: queryparser.cc:261
TermIterator unstem_begin(const std::string &term) const
Begin iterator over unstemmed forms of the given stemmed query term.
Definition: queryparser.cc:240
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:778
STL namespace.
Pick the maximum weight of any subquery.
Definition: query.h:249
Indicates a query string can&#39;t be parsed.
Definition: error.h:887
Produce a query which doesn&#39;t use positional information.
Definition: queryparser.h:930
static Xapian::Stem stemmer
Definition: stemtest.cc:41
A vector-like container of terms which can be iterated.
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:147
The non-lemon-generated parts of the QueryParser class.
QueryParser()
Default constructor.
Definition: queryparser.cc:80
void add_rangeprocessor(Xapian::RangeProcessor *range_proc, const std::string *grouping=NULL)
Register a RangeProcessor.
Definition: queryparser.cc:253
Enable partial matching.
Definition: queryparser.h:837
void set_stemmer(const Xapian::Stem &stemmer)
Set the stemmer.
Definition: queryparser.cc:85
Hierarchy of classes which Xapian can throw as exceptions.
TermIterator stoplist_begin() const
Begin iterator over terms omitted from the query as stopwords.
Definition: queryparser.cc:233
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
virtual ~FieldProcessor()
Destructor.
Definition: queryparser.cc:64
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:241
Base class for field processors.
Definition: queryparser.h:729
Pick the best N subqueries and combine with OP_OR.
Definition: query.h:215
void set_stemming_strategy(stem_strategy strategy)
Set the stemming strategy.
Definition: queryparser.cc:91
Match only documents where all subqueries match near and in order.
Definition: query.h:152
~QueryParser()
Destructor.
Definition: queryparser.cc:82
stem_strategy
Stemming strategies, for use with set_stemming_strategy().
Definition: queryparser.h:943
virtual ~RangeProcessor()
Destructor.
Definition: queryparser.cc:60
void add_boolean_prefix(const std::string &field, const std::string &prefix, const std::string *grouping=NULL)
Add a boolean term prefix allowing the user to restrict a search with a boolean filter specified in t...
Definition: queryparser.cc:206
Xapian::Internal::intrusive_ptr< Internal > internal
Definition: queryparser.h:781
Query parse_query(const std::string &query_string, unsigned flags=FLAG_DEFAULT, const std::string &default_prefix=std::string())
Parse a query.
Definition: queryparser.cc:162
std::string get_description() const
Return a string describing this object.
Definition: queryparser.cc:267
Base class for range processors.
Definition: queryparser.h:140
Match like OP_OR but weighting as if a single term.
Definition: query.h:239
This class stores a list of terms.
Match only documents which all subqueries match.
Definition: query.h:84
void set_database(const Database &db)
Specify the database being searched.
Definition: queryparser.cc:142
Accumulate unstem and stoplist results.
Definition: queryparser.h:919
const Query operator*(double factor, const Query &q)
Scale a Xapian::Query object using OP_SCALE_WEIGHT.
Definition: query.h:670
virtual std::string get_description() const
Return a string describing this object.
Definition: queryparser.cc:42
QueryParser & operator=(const QueryParser &o)
Assignment.
Definition: queryparser.cc:69
Match only documents where all subqueries match near each other.
Definition: query.h:140
virtual std::string get_description() const
Return a string describing this object.
Definition: queryparser.cc:48
Match documents which at least one subquery matches.
Definition: query.h:92
Various handy helpers which std::string really should provide.
Abstract base class for stop-word decision functor.
Definition: queryparser.h:50
Various assertion macros.
Class representing a query.
Definition: query.h:46
void add_prefix(const std::string &field, const std::string &prefix)
Add a free-text field term prefix.
Definition: queryparser.cc:184
Class for iterating over a list of terms.
Generate n-grams for scripts without explicit word breaks.
Definition: queryparser.h:895
Query::op get_default_op() const
Get the current default operator.
Definition: queryparser.cc:136
parsing a user query string to build a Xapian::Query object