xapian-core  1.4.26
queryinternal.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2011,2012,2013,2014,2015,2016,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef XAPIAN_INCLUDED_QUERYINTERNAL_H
22 #define XAPIAN_INCLUDED_QUERYINTERNAL_H
23 
24 #include "postlist.h"
25 #include "queryvector.h"
26 #include "xapian/intrusive_ptr.h"
27 #include "xapian/query.h"
28 
31 
32 class QueryOptimiser;
33 
34 namespace Xapian {
35 namespace Internal {
36 
37 class QueryTerm : public Query::Internal {
38  std::string term;
39 
41 
43 
44  public:
45  // Construct a "MatchAll" QueryTerm.
46  QueryTerm() : term(), wqf(1), pos(0) { }
47 
48  QueryTerm(const std::string & term_,
49  Xapian::termcount wqf_,
50  Xapian::termpos pos_)
51  : term(term_), wqf(wqf_), pos(pos_) { }
52 
54 
55  const std::string & get_term() const { return term; }
56 
57  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
58 
59  termcount get_length() const XAPIAN_NOEXCEPT XAPIAN_PURE_FUNCTION {
60  return wqf;
61  }
62 
63  void serialise(std::string & result) const;
64 
65  std::string get_description() const;
66 
67  void gather_terms(void * void_terms) const;
68 };
69 
72 
73  public:
74  explicit QueryPostingSource(PostingSource * source_);
75 
76  PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const;
77 
78  void serialise(std::string & result) const;
79 
81 
82  std::string get_description() const;
83 };
84 
85 class QueryScaleWeight : public Query::Internal {
86  double scale_factor;
87 
89 
90  public:
91  QueryScaleWeight(double factor, const Query & subquery_);
92 
93  PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const;
94 
95  termcount get_length() const XAPIAN_NOEXCEPT XAPIAN_PURE_FUNCTION {
96  return subquery.internal->get_length();
97  }
98 
99  void serialise(std::string & result) const;
100 
103  const Query get_subquery(size_t n) const;
104 
105  std::string get_description() const;
106 
107  void gather_terms(void * void_terms) const;
108 };
109 
111  protected:
113 
114  public:
116  : slot(slot_) { }
117 
118  Xapian::valueno get_slot() const { return slot; }
119 };
120 
122  std::string begin, end;
123 
124  public:
126  const std::string &begin_,
127  const std::string &end_)
128  : QueryValueBase(slot_), begin(begin_), end(end_) { }
129 
130  PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const;
131 
132  void serialise(std::string & result) const;
133 
135 
136  std::string get_description() const;
137 };
138 
139 class QueryValueLE : public QueryValueBase {
140  std::string limit;
141 
142  public:
143  QueryValueLE(Xapian::valueno slot_, const std::string &limit_)
144  : QueryValueBase(slot_), limit(limit_) { }
145 
146  PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const;
147 
148  void serialise(std::string & result) const;
149 
151 
152  std::string get_description() const;
153 };
154 
155 class QueryValueGE : public QueryValueBase {
156  std::string limit;
157 
158  public:
159  QueryValueGE(Xapian::valueno slot_, const std::string &limit_)
160  : QueryValueBase(slot_), limit(limit_) { }
161 
162  PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const;
163 
164  void serialise(std::string & result) const;
165 
167 
168  std::string get_description() const;
169 };
170 
171 class QueryBranch : public Query::Internal {
172  virtual Xapian::Query::op get_op() const = 0;
173 
174  protected:
176 
177  explicit QueryBranch(size_t n_subqueries) : subqueries(n_subqueries) { }
178 
179  void serialise_(std::string & result, Xapian::termcount parameter = 0) const;
180 
181  void do_or_like(OrContext& ctx, QueryOptimiser * qopt, double factor,
182  Xapian::termcount elite_set_size = 0, size_t first = 0) const;
183 
184  PostList * do_synonym(QueryOptimiser * qopt, double factor) const;
185 
186  PostList * do_max(QueryOptimiser * qopt, double factor) const;
187 
188  const std::string get_description_helper(const char * op,
189  Xapian::termcount window = 0) const;
190 
191  public:
193 
194  void serialise(std::string & result) const;
195 
196  void gather_terms(void * void_terms) const;
197 
198  virtual void add_subquery(const Xapian::Query & subquery) = 0;
199 
202  const Query get_subquery(size_t n) const;
203 
204  virtual Query::Internal * done() = 0;
205 };
206 
207 class QueryAndLike : public QueryBranch {
208  protected:
209  explicit QueryAndLike(size_t num_subqueries_)
210  : QueryBranch(num_subqueries_) { }
211 
212  public:
213  void add_subquery(const Xapian::Query & subquery);
214 
215  Query::Internal * done();
216 
217  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
218 
219  void postlist_sub_and_like(AndContext& ctx, QueryOptimiser * qopt, double factor) const;
220 };
221 
222 class QueryOrLike : public QueryBranch {
223  protected:
224  explicit QueryOrLike(size_t num_subqueries_)
225  : QueryBranch(num_subqueries_) { }
226 
227  public:
228  void add_subquery(const Xapian::Query & subquery);
229 
230  Query::Internal * done();
231 };
232 
233 class QueryAnd : public QueryAndLike {
234  Xapian::Query::op get_op() const;
235 
236  public:
237  explicit QueryAnd(size_t n_subqueries) : QueryAndLike(n_subqueries) { }
238 
239  std::string get_description() const;
240 };
241 
242 class QueryOr : public QueryOrLike {
243  Xapian::Query::op get_op() const;
244 
245  public:
246  explicit QueryOr(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
247 
248  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
249 
250  void postlist_sub_or_like(OrContext& ctx, QueryOptimiser * qopt, double factor) const;
251 
252  std::string get_description() const;
253 };
254 
255 class QueryAndNot : public QueryBranch {
256  Xapian::Query::op get_op() const;
257 
258  public:
259  explicit QueryAndNot(size_t n_subqueries) : QueryBranch(n_subqueries) { }
260 
261  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
262 
264  QueryOptimiser* qopt,
265  double factor) const;
266 
267  void add_subquery(const Xapian::Query & subquery);
268 
269  Query::Internal * done();
270 
271  std::string get_description() const;
272 };
273 
274 class QueryXor : public QueryOrLike {
275  Xapian::Query::op get_op() const;
276 
277  public:
278  explicit QueryXor(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
279 
280  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
281 
282  void postlist_sub_xor(XorContext& ctx, QueryOptimiser * qopt, double factor) const;
283 
284  std::string get_description() const;
285 };
286 
287 class QueryAndMaybe : public QueryBranch {
288  Xapian::Query::op get_op() const;
289 
290  public:
291  explicit QueryAndMaybe(size_t n_subqueries) : QueryBranch(n_subqueries) { }
292 
293  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
294 
296  QueryOptimiser* qopt,
297  double factor) const;
298 
299  void add_subquery(const Xapian::Query & subquery);
300 
301  Query::Internal * done();
302 
303  std::string get_description() const;
304 };
305 
306 class QueryFilter : public QueryAndLike {
307  Xapian::Query::op get_op() const;
308 
309  public:
310  explicit QueryFilter(size_t n_subqueries) : QueryAndLike(n_subqueries) { }
311 
312  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
313 
314  void postlist_sub_and_like(AndContext& ctx, QueryOptimiser * qopt, double factor) const;
315 
316  std::string get_description() const;
317 };
318 
319 class QueryWindowed : public QueryAndLike {
320  protected:
322 
323  QueryWindowed(size_t n_subqueries, Xapian::termcount window_)
324  : QueryAndLike(n_subqueries), window(window_) { }
325 
326  void postlist_windowed(Xapian::Query::op op, AndContext& ctx,
327  QueryOptimiser * qopt, double factor) const;
328 
329  public:
330  size_t get_window() const { return window; }
331 
332  Query::Internal * done();
333 };
334 
335 class QueryNear : public QueryWindowed {
336  Xapian::Query::op get_op() const;
337 
338  public:
339  QueryNear(size_t n_subqueries, Xapian::termcount window_)
340  : QueryWindowed(n_subqueries, window_) { }
341 
342  void serialise(std::string & result) const;
343 
344  void postlist_sub_and_like(AndContext& ctx, QueryOptimiser * qopt, double factor) const;
345 
346  std::string get_description() const;
347 };
348 
349 class QueryPhrase : public QueryWindowed {
350  Xapian::Query::op get_op() const;
351 
352  public:
353  QueryPhrase(size_t n_subqueries, Xapian::termcount window_)
354  : QueryWindowed(n_subqueries, window_) { }
355 
356  void serialise(std::string & result) const;
357 
358  void postlist_sub_and_like(AndContext& ctx, QueryOptimiser * qopt, double factor) const;
359 
360  std::string get_description() const;
361 };
362 
363 class QueryEliteSet : public QueryOrLike {
364  Xapian::Query::op get_op() const;
365 
367 
368  public:
369  QueryEliteSet(size_t n_subqueries, Xapian::termcount set_size_)
370  : QueryOrLike(n_subqueries),
371  set_size(set_size_ ? set_size_ : DEFAULT_ELITE_SET_SIZE) { }
372 
373  void serialise(std::string & result) const;
374 
375  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
376 
377  void postlist_sub_or_like(OrContext& ctx, QueryOptimiser * qopt, double factor) const;
378 
379  std::string get_description() const;
380 };
381 
382 class QuerySynonym : public QueryOrLike {
383  Xapian::Query::op get_op() const;
384 
385  public:
386  explicit QuerySynonym(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
387 
388  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
389 
390  Query::Internal * done();
391 
392  std::string get_description() const;
393 };
394 
395 class QueryMax : public QueryOrLike {
396  Xapian::Query::op get_op() const;
397 
398  public:
399  explicit QueryMax(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
400 
401  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
402 
403  std::string get_description() const;
404 };
405 
407  std::string pattern;
408 
410 
411  int max_type;
412 
414 
415  Xapian::Query::op get_op() const;
416 
417  public:
418  QueryWildcard(const std::string &pattern_,
419  Xapian::termcount max_expansion_,
420  int max_type_,
421  Query::op combiner_)
422  : pattern(pattern_),
423  max_expansion(max_expansion_),
424  max_type(max_type_),
425  combiner(combiner_)
426  { }
427 
429 
430  const std::string & get_pattern() const { return pattern; }
431 
432  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
433 
435 
436  void serialise(std::string & result) const;
437 
445  if (_refs == 1) {
446  combiner = new_op;
447  return this;
448  }
449  return new QueryWildcard(pattern,
450  max_expansion,
451  max_type,
452  new_op);
453  }
454 
455  std::string get_description() const;
456 };
457 
459  public:
461 
463 
464  PostingIterator::Internal * postlist(QueryOptimiser * qopt, double factor) const;
465 
466  void serialise(std::string & result) const;
467 
468  std::string get_description() const;
469 };
470 
471 }
472 
473 }
474 
475 #endif // XAPIAN_INCLUDED_QUERYINTERNAL_H
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
QueryXor(size_t n_subqueries)
QueryValueGE(Xapian::valueno slot_, const std::string &limit_)
Abstract base class for postlists.
Definition: postlist.h:37
QueryAnd(size_t n_subqueries)
#define XAPIAN_PURE_FUNCTION
Like XAPIAN_CONST_FUNCTION, but such a function can also examine global memory, perhaps via pointer o...
Definition: attributes.h:67
virtual void postlist_sub_and_like(Xapian::Internal::AndContext &ctx, QueryOptimiser *qopt, double factor) const
QueryValueBase(Xapian::valueno slot_)
QuerySynonym(size_t n_subqueries)
op
Query operators.
Definition: query.h:78
virtual const Query get_subquery(size_t n) const
unsigned _refs
Reference count.
Definition: intrusive_ptr.h:73
virtual void postlist_sub_xor(Xapian::Internal::XorContext &ctx, QueryOptimiser *qopt, double factor) const
QueryNear(size_t n_subqueries, Xapian::termcount window_)
virtual void postlist_sub_or_like(Xapian::Internal::OrContext &ctx, QueryOptimiser *qopt, double factor) const
STL namespace.
QueryOrLike(size_t num_subqueries_)
Xapian::Internal::intrusive_ptr< Internal > internal
Definition: query.h:49
Abstract base class for postlists.
Xapian::Query::op get_type() const
Xapian::Query API class.
QueryMax(size_t n_subqueries)
QueryEliteSet(size_t n_subqueries, Xapian::termcount set_size_)
QueryFilter(size_t n_subqueries)
QueryAndNot(size_t n_subqueries)
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
QueryWildcard(const std::string &pattern_, Xapian::termcount max_expansion_, int max_type_, Query::op combiner_)
const std::string & get_pattern() const
const std::string & get_term() const
Definition: queryinternal.h:55
Xapian::Internal::opt_intrusive_ptr< PostingSource > source
Definition: queryinternal.h:71
virtual size_t get_num_subqueries() const
QueryAndMaybe(size_t n_subqueries)
QueryValueRange(Xapian::valueno slot_, const std::string &begin_, const std::string &end_)
void gather_terms(void *void_terms) const
QueryTerm(const std::string &term_, Xapian::termcount wqf_, Xapian::termpos pos_)
Definition: queryinternal.h:48
void serialise(std::string &result) const
QueryValueLE(Xapian::valueno slot_, const std::string &limit_)
Base class which provides an "external" source of postings.
Definition: postingsource.h:47
QueryOr(size_t n_subqueries)
QueryBranch(size_t n_subqueries)
QueryPhrase(size_t n_subqueries, Xapian::termcount window_)
QueryAndLike(size_t num_subqueries_)
Append only vector of Query objects.
std::string get_description() const
PostingIterator::Internal * postlist(QueryOptimiser *qopt, double factor) const
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:83
QueryWindowed(size_t n_subqueries, Xapian::termcount window_)
Xapian::valueno get_slot() const
termcount get_length() const
Definition: queryinternal.h:59
const Xapian::termcount DEFAULT_ELITE_SET_SIZE
Default set_size for OP_ELITE_SET:
Definition: queryinternal.h:30
Class representing a query.
Definition: query.h:46
A smart pointer that optionally uses intrusive reference counting.
#define XAPIAN_NOEXCEPT
Definition: attributes.h:39
QueryWildcard * change_combiner(Xapian::Query::op new_op)
Change the combining operator.