xapian-core  1.4.18
queryparser.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2006,2007,2008,2010,2011,2012,2013,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 #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 
33 #include <cstring>
34 
35 using namespace Xapian;
36 
37 using namespace std;
38 
39 // Default implementation in case the user hasn't implemented it.
40 string
42 {
43  return "Xapian::Stopper subclass";
44 }
45 
46 string
48 {
49  string desc("Xapian::SimpleStopper(");
50  set<string>::const_iterator i;
51  for (i = stop_words.begin(); i != stop_words.end(); ++i) {
52  if (i != stop_words.begin()) desc += ' ';
53  desc += *i;
54  }
55  desc += ')';
56  return desc;
57 }
58 
60 
62 
64 
65 QueryParser::QueryParser(const QueryParser & o) : internal(o.internal) { }
66 
69 {
70  internal = o.internal;
71  return *this;
72 }
73 
75 
78 
80 
82 
83 void
85 {
86  internal->stemmer = stemmer;
87 }
88 
89 void
91 {
92  internal->stem_action = strategy;
93 }
94 
95 void
97 {
98  internal->stopper = stopper;
99 }
100 
101 void
103 {
104  switch (default_op) {
105  case Query::OP_AND:
106  case Query::OP_OR:
107  case Query::OP_NEAR:
108  case Query::OP_PHRASE:
109  case Query::OP_ELITE_SET:
110  case Query::OP_SYNONYM:
111  case Query::OP_MAX:
112  // These are OK.
113  break;
114  default:
116  "QueryParser::set_default_op() only accepts "
117  "OP_AND"
118  ", "
119  "OP_OR"
120  ", "
121  "OP_NEAR"
122  ", "
123  "OP_PHRASE"
124  ", "
125  "OP_ELITE_SET"
126  ", "
127  "OP_SYNONYM"
128  " or "
129  "OP_MAX");
130  }
131  internal->default_op = default_op;
132 }
133 
134 Query::op
136 {
137  return internal->default_op;
138 }
139 
140 void
142  internal->db = db;
143 }
144 
145 void
147  int max_type,
148  unsigned flags)
149 {
150  if (flags & FLAG_WILDCARD) {
151  internal->max_wildcard_expansion = max_expansion;
152  internal->max_wildcard_type = max_type;
153  }
154  if (flags & FLAG_PARTIAL) {
155  internal->max_partial_expansion = max_expansion;
156  internal->max_partial_type = max_type;
157  }
158 }
159 
160 Query
161 QueryParser::parse_query(const string &query_string, unsigned flags,
162  const string &default_prefix)
163 {
164  if (!(flags & FLAG_ACCUMULATE)) {
165  internal->stoplist.clear();
166  internal->unstem.clear();
167  }
168  internal->errmsg = NULL;
169 
170  if (query_string.empty()) return Query();
171 
172  Query result = internal->parse_query(query_string, flags, default_prefix);
173  if (internal->errmsg && strcmp(internal->errmsg, "parse error") == 0) {
174  flags &= FLAG_CJK_NGRAM;
175  result = internal->parse_query(query_string, flags, default_prefix);
176  }
177 
178  if (internal->errmsg) throw Xapian::QueryParserError(internal->errmsg);
179  return result;
180 }
181 
182 void
183 QueryParser::add_prefix(const string &field, const string &prefix)
184 {
185  Assert(internal.get());
186  internal->add_prefix(field, prefix);
187 }
188 
189 void
191 {
192  Assert(internal.get());
193  internal->add_prefix(field, proc);
194 }
195 
196 void
197 QueryParser::add_boolean_prefix(const string &field, const string &prefix,
198  const string* grouping)
199 {
200  Assert(internal.get());
201  internal->add_boolean_prefix(field, prefix, grouping);
202 }
203 
204 void
206  Xapian::FieldProcessor * proc,
207  const string* grouping)
208 {
209  Assert(internal.get());
210  internal->add_boolean_prefix(field, proc, grouping);
211 }
212 
215 {
216  const list<string> & sl = internal->stoplist;
217  return TermIterator(new VectorTermList(sl.begin(), sl.end()));
218 }
219 
221 QueryParser::unstem_begin(const string &term) const
222 {
223  struct range_adaptor : public multimap<string, string>::iterator {
224  range_adaptor(multimap<string, string>::iterator i) :
225  multimap<string, string>::iterator(i) {}
226  const string & operator*() const { return (*this)->second; }
227  };
228  auto range = internal->unstem.equal_range(term);
229  return TermIterator(new VectorTermList(range_adaptor(range.first),
230  range_adaptor(range.second)));
231 }
232 
233 void
235  const std::string* grouping)
236 {
237  Assert(internal.get());
238  internal->rangeprocs.push_back(RangeProc(range_proc, grouping));
239 }
240 
241 string
243 {
244  return internal->corrected_query;
245 }
246 
247 string
249 {
250  // FIXME : describe better!
251  return "Xapian::QueryParser()";
252 }
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
#define Assert(COND)
Definition: omassert.h:122
void set_default_op(Query::op default_op)
Set the default operator.
Definition: queryparser.cc:102
This class is used to access a database, or a group of databases.
Definition: database.h:68
virtual ~ValueRangeProcessor()
Destructor.
Definition: queryparser.cc:61
Class representing a stemming algorithm.
Definition: stem.h:62
void set_stopper(const Stopper *stop=NULL)
Set the stopper.
Definition: queryparser.cc:96
op
Query operators.
Definition: query.h:78
std::string get_corrected_query_string() const
Get the spelling-corrected query string.
Definition: queryparser.cc:242
TermIterator unstem_begin(const std::string &term) const
Begin iterator over unstemmed forms of the given stemmed query term.
Definition: queryparser.cc:221
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:770
STL namespace.
Pick the maximum weight of any subquery.
Definition: query.h:240
Indicates a query string can&#39;t be parsed.
Definition: error.h:887
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:146
The non-lemon-generated parts of the QueryParser class.
QueryParser()
Default constructor.
Definition: queryparser.cc:79
void add_rangeprocessor(Xapian::RangeProcessor *range_proc, const std::string *grouping=NULL)
Register a RangeProcessor.
Definition: queryparser.cc:234
Enable partial matching.
Definition: queryparser.h:829
void set_stemmer(const Xapian::Stem &stemmer)
Set the stemmer.
Definition: queryparser.cc:84
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:214
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:63
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:241
Base class for field processors.
Definition: queryparser.h:721
Pick the best N subqueries and combine with OP_OR.
Definition: query.h:206
void set_stemming_strategy(stem_strategy strategy)
Set the stemming strategy.
Definition: queryparser.cc:90
Match only documents where all subqueries match near and in order.
Definition: query.h:152
~QueryParser()
Destructor.
Definition: queryparser.cc:81
stem_strategy
Stemming strategies, for use with set_stemming_strategy().
Definition: queryparser.h:906
virtual ~RangeProcessor()
Destructor.
Definition: queryparser.cc:59
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:197
Xapian::Internal::intrusive_ptr< Internal > internal
Definition: queryparser.h:773
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:161
std::string get_description() const
Return a string describing this object.
Definition: queryparser.cc:248
Base class for range processors.
Definition: queryparser.h:132
Match like OP_OR but weighting as if a single term.
Definition: query.h:230
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:141
Accumulate unstem and stoplist results.
Definition: queryparser.h:893
const Query operator*(double factor, const Query &q)
Scale a Xapian::Query object using OP_SCALE_WEIGHT.
Definition: query.h:661
virtual std::string get_description() const
Return a string describing this object.
Definition: queryparser.cc:41
QueryParser & operator=(const QueryParser &o)
Assignment.
Definition: queryparser.cc:68
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:47
Match documents which at least one subquery matches.
Definition: query.h:92
Base class for stop-word decision functor.
Definition: queryparser.h:46
Enable generation of n-grams from CJK text.
Definition: queryparser.h:878
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:183
Class for iterating over a list of terms.
Query::op get_default_op() const
Get the current default operator.
Definition: queryparser.cc:135
parsing a user query string to build a Xapian::Query object