include/xapian/queryparser.h: Remove XAPIAN_CONST_FUNCTION marker
[xapian.git] / xapian-core / include / xapian / queryparser.h
blobe4ef7d35d6ce4aec3c671e26ee994c088de74512
1 /** @file queryparser.h
2 * @brief parsing a user query string to build a Xapian::Query object
3 */
4 /* Copyright (C) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015 Olly Betts
5 * Copyright (C) 2010 Adam Sjøgren
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.
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.
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
23 #ifndef XAPIAN_INCLUDED_QUERYPARSER_H
24 #define XAPIAN_INCLUDED_QUERYPARSER_H
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
30 #include <xapian/attributes.h>
31 #include <xapian/intrusive_ptr.h>
32 #include <xapian/query.h>
33 #include <xapian/termiterator.h>
34 #include <xapian/visibility.h>
36 #include <set>
37 #include <string>
39 namespace Xapian {
41 class Database;
42 class Stem;
44 /// Base class for stop-word decision functor.
45 class XAPIAN_VISIBILITY_DEFAULT Stopper {
46 public:
47 /** Is term a stop-word?
49 * @param term The term to test.
51 virtual bool operator()(const std::string & term) const = 0;
53 /// Class has virtual methods, so provide a virtual destructor.
54 virtual ~Stopper() { }
56 /// Return a string describing this object.
57 virtual std::string get_description() const;
60 /// Simple implementation of Stopper class - this will suit most users.
61 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
62 std::set<std::string> stop_words;
64 public:
65 /// Default constructor.
66 SimpleStopper() { }
68 /** Initialise from a pair of iterators.
70 * Xapian includes stop list files for many languages. You can initialise from a file like that:
71 * @code
72 * ifstream inFile ("stopwords/english/stop.txt");
73 * Xapian::SimplerStopper stopper(istream_iterator<string>(inFile), istream_iterator<string>());
74 * @endcode
77 template <class Iterator>
78 SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
80 /// Add a single stop word.
81 void add(const std::string & word) { stop_words.insert(word); }
83 virtual bool operator()(const std::string & term) const {
84 return stop_words.find(term) != stop_words.end();
87 virtual std::string get_description() const;
90 /// Base class for value range processors.
91 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor
92 : public Xapian::Internal::opt_intrusive_base {
93 /// Destructor.
94 virtual ~ValueRangeProcessor();
96 /** Check for a valid range of this type.
98 * @param[in,out] begin The start of the range as specified in the query
99 * string by the user. This parameter is a
100 * non-const reference so the ValueRangeProcessor
101 * can modify it to return the value to start the
102 * range with.
103 * @param[in,out] end The end of the range. This is also a non-const
104 * reference so it can be modified.
106 * @return If this ValueRangeProcessor recognises the range BEGIN..END it
107 * returns the value slot number to range filter on. Otherwise it
108 * returns Xapian::BAD_VALUENO.
110 virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
112 ValueRangeProcessor * release() {
113 opt_intrusive_base::release();
114 return this;
117 const ValueRangeProcessor * release() const {
118 opt_intrusive_base::release();
119 return this;
123 /** Handle a string range.
125 * The end points can be any strings.
127 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
128 protected:
129 Xapian::valueno valno;
131 private:
132 bool prefix;
133 std::string str;
135 public:
136 /** Constructor.
138 * @param slot_ The value number to return from operator().
140 explicit StringValueRangeProcessor(Xapian::valueno slot_)
141 : valno(slot_), str() { }
143 /** Constructor.
145 * @param slot_ The value number to return from operator().
146 * @param str_ A string to look for to recognise values as belonging
147 * to this range.
148 * @param prefix_ Flag specifying whether to check for str_ as a prefix
149 * or a suffix.
151 StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
152 bool prefix_ = true)
153 : valno(slot_), prefix(prefix_), str(str_) { }
155 /** Check for a valid string range.
157 * @param[in,out] begin The start of the range as specified in the
158 * query string by the user. This parameter is a
159 * non-const reference so the ValueRangeProcessor
160 * can modify it to return the value to start the
161 * range with.
162 * @param[in,out] end The end of the range. This is also a non-const
163 * reference so it can be modified.
165 * @return A StringValueRangeProcessor always accepts a range it is
166 * offered, and returns the value of slot_ passed at construction
167 * time. It doesn't modify @a begin or @a end.
169 Xapian::valueno operator()(std::string &begin, std::string &end);
172 /** Handle a date range.
174 * Begin and end must be dates in a recognised format.
176 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public StringValueRangeProcessor {
177 bool prefer_mdy;
178 int epoch_year;
180 public:
181 /** Constructor.
183 * @param slot_ The value number to return from operator().
184 * @param prefer_mdy_ Should ambiguous dates be interpreted as
185 * month/day/year rather than day/month/year?
186 * (default: false)
187 * @param epoch_year_ Year to use as the epoch for dates with 2 digit
188 * years (default: 1970, so 1/1/69 is 2069 while
189 * 1/1/70 is 1970).
191 DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
192 int epoch_year_ = 1970)
193 : StringValueRangeProcessor(slot_),
194 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
196 /** Constructor.
198 * @param slot_ The value number to return from operator().
200 * @param str_ A string to look for to recognise values as belonging
201 * to this date range.
203 * @param prefix_ Whether to look for the string at the start or end of
204 * the values. If true, the string is a prefix; if
205 * false, the string is a suffix (default: true).
207 * @param prefer_mdy_ Should ambiguous dates be interpreted as
208 * month/day/year rather than day/month/year?
209 * (default: false)
211 * @param epoch_year_ Year to use as the epoch for dates with 2 digit
212 * years (default: 1970, so 1/1/69 is 2069 while
213 * 1/1/70 is 1970).
215 * The string supplied in str_ is used by @a operator() to decide whether
216 * the pair of strings supplied to it constitute a valid range. If
217 * prefix_ is true, the first value in a range must begin with str_ (and
218 * the second value may optionally begin with str_);
219 * if prefix_ is false, the second value in a range must end with str_
220 * (and the first value may optionally end with str_).
222 * If str_ is empty, the setting of prefix_ is irrelevant, and no special
223 * strings are required at the start or end of the strings defining the
224 * range.
226 * The remainder of both strings defining the endpoints must be valid
227 * dates.
229 * For example, if str_ is "created:" and prefix_ is true, and the range
230 * processor has been added to the queryparser, the queryparser will
231 * accept "created:1/1/2000..31/12/2001".
233 DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
234 bool prefix_ = true,
235 bool prefer_mdy_ = false, int epoch_year_ = 1970)
236 : StringValueRangeProcessor(slot_, str_, prefix_),
237 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
239 #ifndef SWIG
240 /** Constructor.
242 * This is like the previous version, but with const char * instead of
243 * std::string - we need this overload as otherwise
244 * DateValueRangeProcessor(1, "date:") quietly interprets the second
245 * argument as a boolean in preference to std::string. If you want to
246 * be compatible with 1.2.12 and earlier, then explicitly convert to
247 * std::string, i.e.: DateValueRangeProcessor(1, std::string("date:"))
249 * @param slot_ The value number to return from operator().
251 * @param str_ A string to look for to recognise values as belonging
252 * to this date range.
254 * @param prefix_ Whether to look for the string at the start or end of
255 * the values. If true, the string is a prefix; if
256 * false, the string is a suffix (default: true).
258 * @param prefer_mdy_ Should ambiguous dates be interpreted as
259 * month/day/year rather than day/month/year?
260 * (default: false)
262 * @param epoch_year_ Year to use as the epoch for dates with 2 digit
263 * years (default: 1970, so 1/1/69 is 2069 while
264 * 1/1/70 is 1970).
266 * The string supplied in str_ is used by @a operator() to decide whether
267 * the pair of strings supplied to it constitute a valid range. If
268 * prefix_ is true, the first value in a range must begin with str_ (and
269 * the second value may optionally begin with str_);
270 * if prefix_ is false, the second value in a range must end with str_
271 * (and the first value may optionally end with str_).
273 * If str_ is empty, the setting of prefix_ is irrelevant, and no special
274 * strings are required at the start or end of the strings defining the
275 * range.
277 * The remainder of both strings defining the endpoints must be valid
278 * dates.
280 * For example, if str_ is "created:" and prefix_ is true, and the range
281 * processor has been added to the queryparser, the queryparser will
282 * accept "created:1/1/2000..31/12/2001".
284 DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
285 bool prefix_ = true,
286 bool prefer_mdy_ = false, int epoch_year_ = 1970)
287 : StringValueRangeProcessor(slot_, str_, prefix_),
288 prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
289 #endif
291 /** Check for a valid date range.
293 * @param[in,out] begin The start of the range as specified in the
294 * query string by the user. This parameter is a
295 * non-const reference so the ValueRangeProcessor
296 * can modify it to return the value to start the
297 * range with.
298 * @param[in,out] end The end of the range. This is also a non-const
299 * reference so it can be modified.
301 * @return If BEGIN..END is a sensible date range, this method modifies
302 * them into the format YYYYMMDD and returns the value of slot_
303 * passed at construction time. Otherwise it returns
304 * Xapian::BAD_VALUENO.
306 Xapian::valueno operator()(std::string &begin, std::string &end);
309 /** Handle a number range.
311 * This class must be used on values which have been encoded using
312 * Xapian::sortable_serialise() which turns numbers into strings which
313 * will sort in the same order as the numbers (the same values can be
314 * used to implement a numeric sort).
316 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public StringValueRangeProcessor {
317 public:
318 /** Constructor.
320 * @param slot_ The value number to return from operator().
322 explicit NumberValueRangeProcessor(Xapian::valueno slot_)
323 : StringValueRangeProcessor(slot_) { }
325 /** Constructor.
327 * @param slot_ The value number to return from operator().
329 * @param str_ A string to look for to recognise values as belonging
330 * to this numeric range.
332 * @param prefix_ Whether to look for the string at the start or end of
333 * the values. If true, the string is a prefix; if
334 * false, the string is a suffix (default: true).
336 * The string supplied in str_ is used by @a operator() to decide whether
337 * the pair of strings supplied to it constitute a valid range. If
338 * prefix_ is true, the first value in a range must begin with str_ (and
339 * the second value may optionally begin with str_);
340 * if prefix_ is false, the second value in a range must end with str_
341 * (and the first value may optionally end with str_).
343 * If str_ is empty, the setting of prefix_ is irrelevant, and no special
344 * strings are required at the start or end of the strings defining the
345 * range.
347 * The remainder of both strings defining the endpoints must be valid
348 * floating point numbers. (FIXME: define format recognised).
350 * For example, if str_ is "$" and prefix_ is true, and the range
351 * processor has been added to the queryparser, the queryparser will
352 * accept "$10..50" or "$10..$50", but not "10..50" or "10..$50" as valid
353 * ranges. If str_ is "kg" and prefix_ is false, the queryparser will
354 * accept "10..50kg" or "10kg..50kg", but not "10..50" or "10kg..50" as
355 * valid ranges.
357 NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
358 bool prefix_ = true)
359 : StringValueRangeProcessor(slot_, str_, prefix_) { }
361 /** Check for a valid numeric range.
363 * @param[in,out] begin The start of the range as specified in the
364 * query string by the user. This parameter is a
365 * non-const reference so the ValueRangeProcessor
366 * can modify it to return the value to start the
367 * range with.
368 * @param[in,out] end The end of the range. This is also a non-const
369 * reference so it can be modified.
371 * @return If BEGIN..END is a valid numeric range with the specified
372 * prefix/suffix (if one was specified), this method modifies
373 * them by removing the prefix/suffix, converting to a number,
374 * and encoding with Xapian::sortable_serialise(), and returns the
375 * value of slot_ passed at construction time. Otherwise it
376 * returns Xapian::BAD_VALUENO.
378 Xapian::valueno operator()(std::string &begin, std::string &end);
381 /** Base class for field processors.
383 * Experimental API - may change.
385 struct XAPIAN_VISIBILITY_DEFAULT FieldProcessor
386 : public Xapian::Internal::opt_intrusive_base {
387 /// Destructor.
388 virtual ~FieldProcessor();
390 /** Convert a field-prefixed string to a Query object.
392 * @param str The string to convert.
394 * @return Query object corresponding to @a str.
396 virtual Xapian::Query operator()(const std::string &str) = 0;
398 FieldProcessor * release() {
399 opt_intrusive_base::release();
400 return this;
403 const FieldProcessor * release() const {
404 opt_intrusive_base::release();
405 return this;
409 /// Build a Xapian::Query object from a user query string.
410 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
411 public:
412 /// Class representing the queryparser internals.
413 class Internal;
414 /// @private @internal Reference counted internals.
415 Xapian::Internal::intrusive_ptr<Internal> internal;
417 /// Enum of feature flags.
418 typedef enum {
419 /// Support AND, OR, etc and bracketed subexpressions.
420 FLAG_BOOLEAN = 1,
421 /// Support quoted phrases.
422 FLAG_PHRASE = 2,
423 /// Support + and -.
424 FLAG_LOVEHATE = 4,
425 /// Support AND, OR, etc even if they aren't in ALLCAPS.
426 FLAG_BOOLEAN_ANY_CASE = 8,
427 /** Support wildcards.
429 * At present only right truncation (e.g. Xap*) is supported.
431 * Currently you can't use wildcards with boolean filter prefixes,
432 * or in a phrase (either an explicitly quoted one, or one implicitly
433 * generated by hyphens or other punctuation).
435 * In Xapian 1.2.x, you needed to tell the QueryParser object which
436 * database to expand wildcards from by calling set_database(). In
437 * Xapian 1.3.3, OP_WILDCARD was added and wildcards are now
438 * expanded when Enquire::get_mset() is called, with the expansion
439 * using the database being searched.
441 FLAG_WILDCARD = 16,
442 /** Allow queries such as 'NOT apples'.
444 * These require the use of a list of all documents in the database
445 * which is potentially expensive, so this feature isn't enabled by
446 * default.
448 FLAG_PURE_NOT = 32,
449 /** Enable partial matching.
451 * Partial matching causes the parser to treat the query as a
452 * "partially entered" search. This will automatically treat the
453 * final word as a wildcarded match, unless it is followed by
454 * whitespace, to produce more stable results from interactive
455 * searches.
457 * Currently FLAG_PARTIAL doesn't do anything if the final word
458 * in the query has a boolean filter prefix, or if it is in a phrase
459 * (either an explicitly quoted one, or one implicitly generated by
460 * hyphens or other punctuation). It also doesn't do anything if
461 * if the final word is part of a value range.
463 * In Xapian 1.2.x, you needed to tell the QueryParser object which
464 * database to expand wildcards from by calling set_database(). In
465 * Xapian 1.3.3, OP_WILDCARD was added and wildcards are now
466 * expanded when Enquire::get_mset() is called, with the expansion
467 * using the database being searched.
469 FLAG_PARTIAL = 64,
471 /** Enable spelling correction.
473 * For each word in the query which doesn't exist as a term in the
474 * database, Database::get_spelling_suggestion() will be called and if
475 * a suggestion is returned, a corrected version of the query string
476 * will be built up which can be read using
477 * QueryParser::get_corrected_query_string(). The query returned is
478 * based on the uncorrected query string however - if you want a
479 * parsed query based on the corrected query string, you must call
480 * QueryParser::parse_query() again.
482 * NB: You must also call set_database() for this to work.
484 FLAG_SPELLING_CORRECTION = 128,
486 /** Enable synonym operator '~'.
488 * NB: You must also call set_database() for this to work.
490 FLAG_SYNONYM = 256,
492 /** Enable automatic use of synonyms for single terms.
494 * NB: You must also call set_database() for this to work.
496 FLAG_AUTO_SYNONYMS = 512,
498 /** Enable automatic use of synonyms for single terms and groups of
499 * terms.
501 * NB: You must also call set_database() for this to work.
503 FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
505 /** The default flags.
507 * Used if you don't explicitly pass any to @a parse_query().
508 * The default flags are FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE.
510 * Added in Xapian 1.0.11.
512 FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
513 } feature_flag;
515 /// Stemming strategies, for use with set_stemming_strategy().
516 typedef enum { STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z } stem_strategy;
518 /// Copy constructor.
519 QueryParser(const QueryParser & o);
521 /// Assignment.
522 QueryParser & operator=(const QueryParser & o);
524 /// Default constructor.
525 QueryParser();
527 /// Destructor.
528 ~QueryParser();
530 /** Set the stemmer.
532 * This sets the stemming algorithm which will be used by the query
533 * parser. The stemming algorithm will be used according to the stemming
534 * strategy set by set_stemming_strategy(). As of 1.3.1, this defaults
535 * to STEM_SOME, but in earlier versions the default was STEM_NONE. If
536 * you want to work with older versions, you should explicitly set
537 * a stemming strategy as well as setting a stemmer, otherwise your
538 * stemmer won't actually be used.
540 * @param stemmer The Xapian::Stem object to set.
542 void set_stemmer(const Xapian::Stem & stemmer);
544 /** Set the stemming strategy.
546 * This controls how the query parser will apply the stemming algorithm.
547 * Note that the stemming algorithm is only applied to words in
548 * probabilistic fields - boolean filter terms are never stemmed.
550 * @param strategy The strategy to use - possible values are:
551 * - STEM_NONE: Don't perform any stemming. (default in Xapian <=
552 * 1.3.0)
553 * - STEM_SOME: Stem all terms except for those which start with a
554 * capital letter, or are followed by certain characters
555 * (currently: <code>(/\@<>=*[{"</code> ), or are used
556 * with operators which need positional information.
557 * Stemmed terms are prefixed with 'Z'. (default in
558 * Xapian >= 1.3.1)
559 * - STEM_ALL: Stem all terms (note: no 'Z' prefix is added).
560 * - STEM_ALL_Z: Stem all terms (note: 'Z' prefix is added). (new in
561 * Xapian 1.2.11 and 1.3.1)
563 void set_stemming_strategy(stem_strategy strategy);
565 /** Set the stopper.
567 * @param stop The Stopper object to set (default NULL, which means no
568 * stopwords).
570 void set_stopper(const Stopper *stop = NULL);
572 /** Set the default operator.
574 * @param default_op The operator to use to combine non-filter
575 * query items when no explicit operator is used.
577 * So for example, 'weather forecast' is parsed as
578 * if it were 'weather OR forecast' by default.
580 * The most useful values for this are OP_OR (the
581 * default) and OP_AND. OP_NEAR, OP_PHRASE,
582 * OP_ELITE_SET and OP_SYNONYM are also permitted.
583 * Passing other values will result in
584 * InvalidArgumentError being thrown.
586 void set_default_op(Query::op default_op);
588 /** Get the current default operator. */
589 Query::op get_default_op() const;
591 /** Specify the database being searched.
593 * @param db The database to use for spelling correction
594 * (FLAG_SPELLING_CORRECTION), and synonyms (FLAG_SYNONYM,
595 * FLAG_AUTO_SYNONYMS, and FLAG_AUTO_MULTIWORD_SYNONYMS).
597 void set_database(const Database &db);
599 /** Specify the maximum expansion of a wildcard and/or partial term.
601 * Note: you must also set FLAG_WILDCARD and/or FLAG_PARTIAL in the flags
602 * parameter to @a parse_query() for this setting to have anything to
603 * affect.
605 * If you don't call this method, the default settings are no limit on
606 * wildcard expansion, and partial terms expanding to the most frequent
607 * 100 terms - i.e. as if you'd called:
609 * set_max_expansion(0);
610 * set_max_expansion(100, Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT, Xapian::QueryParser::FLAG_PARTIAL);
612 * @param max_expansion The maximum number of terms each wildcard in the
613 * query can expand to, or 0 for no limit (which is the
614 * default).
615 * @param max_type @a Xapian::Query::WILDCARD_LIMIT_ERROR,
616 * @a Xapian::Query::WILDCARD_LIMIT_FIRST or
617 * @a Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT
618 * (default: Xapian::Query::WILDCARD_LIMIT_ERROR).
619 * @param flags What to set the limit for (default:
620 * FLAG_WILDCARD|FLAG_PARTIAL, setting the limit for both
621 * wildcards and partial terms).
623 void set_max_expansion(Xapian::termcount max_expansion,
624 int max_type = Xapian::Query::WILDCARD_LIMIT_ERROR,
625 unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
627 /** Specify the maximum expansion of a wildcard.
629 * If any wildcard expands to more than @a max_expansion terms, an
630 * exception will be thrown.
632 * This method is provided for API compatibility with Xapian 1.2.x and is
633 * deprecated - replace it with:
635 * set_max_wildcard_expansion(max_expansion,
636 * Xapian::Query::WILDCARD_LIMIT_ERROR,
637 * Xapian::QueryParser::FLAG_WILDCARD);
639 XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
641 /** Parse a query.
643 * @param query_string A free-text query as entered by a user
644 * @param flags Zero or more Query::feature_flag specifying
645 * what features the QueryParser should support. Combine
646 * multiple values with bitwise-or (|) (default FLAG_DEFAULT).
647 * @param default_prefix The default term prefix to use (default none).
648 * For example, you can pass "A" when parsing an "Author" field.
650 * @exception If the query string can't be parsed, then
651 * Xapian::QueryParserError is thrown. You can get an English
652 * error message to report to the user by catching it and
653 * calling get_msg() on the caught exception. The current
654 * possible values (in case you want to translate them) are:
656 * @li Unknown range operation
657 * @li parse error
658 * @li Syntax: &lt;expression&gt; AND &lt;expression&gt;
659 * @li Syntax: &lt;expression&gt; AND NOT &lt;expression&gt;
660 * @li Syntax: &lt;expression&gt; NOT &lt;expression&gt;
661 * @li Syntax: &lt;expression&gt; OR &lt;expression&gt;
662 * @li Syntax: &lt;expression&gt; XOR &lt;expression&gt;
664 Query parse_query(const std::string &query_string,
665 unsigned flags = FLAG_DEFAULT,
666 const std::string &default_prefix = std::string());
668 /** Add a probabilistic term prefix.
670 * For example:
672 * @code
673 * qp.add_prefix("author", "A");
674 * @endcode
676 * This allows the user to search for author:Orwell which will be
677 * converted to a search for the term "Aorwell".
679 * Multiple fields can be mapped to the same prefix. For example, you
680 * can make title: and subject: aliases for each other.
682 * As of 1.0.4, you can call this method multiple times with the same
683 * value of field to allow a single field to be mapped to multiple
684 * prefixes. Multiple terms being generated for such a field, and
685 * combined with @c Xapian::Query::OP_OR.
687 * If any prefixes are specified for the empty field name (i.e. you
688 * call this method with an empty string as the first parameter)
689 * these prefixes will be used for terms without a field specifier.
690 * If you do this and also specify the @c default_prefix parameter to @c
691 * parse_query(), then the @c default_prefix parameter will override.
693 * If the prefix parameter is empty, then "field:word" will produce the
694 * term "word" (and this can be one of several prefixes for a particular
695 * field, or for terms without a field specifier).
697 * If you call @c add_prefix() and @c add_boolean_prefix() for the
698 * same value of @a field, a @c Xapian::InvalidOperationError exception
699 * will be thrown.
701 * In 1.0.3 and earlier, subsequent calls to this method with the same
702 * value of @a field had no effect.
704 * @param field The user visible field name
705 * @param prefix The term prefix to map this to
707 void add_prefix(const std::string &field, const std::string &prefix);
709 /** Register a FieldProcessor.
711 * Experimental API - may change.
713 void add_prefix(const std::string &field, Xapian::FieldProcessor * proc);
715 /** Add a boolean term prefix allowing the user to restrict a
716 * search with a boolean filter specified in the free text query.
718 * For example:
720 * @code
721 * qp.add_boolean_prefix("site", "H");
722 * @endcode
724 * This allows the user to restrict a search with site:xapian.org which
725 * will be converted to Hxapian.org combined with any probabilistic
726 * query with @c Xapian::Query::OP_FILTER.
728 * If multiple boolean filters are specified in a query for the same
729 * prefix, they will be combined with the @c Xapian::Query::OP_OR
730 * operator. Then, if there are boolean filters for different prefixes,
731 * they will be combined with the @c Xapian::Query::OP_AND operator.
733 * Multiple fields can be mapped to the same prefix (so for example
734 * you can make site: and domain: aliases for each other). Instances of
735 * fields with different aliases but the same prefix will still be
736 * combined with the OR operator.
738 * For example, if "site" and "domain" map to "H", but author maps to "A",
739 * a search for "site:foo domain:bar author:Fred" will map to
740 * "(Hfoo OR Hbar) AND Afred".
742 * As of 1.0.4, you can call this method multiple times with the same
743 * value of field to allow a single field to be mapped to multiple
744 * prefixes. Multiple terms being generated for such a field, and
745 * combined with @c Xapian::Query::OP_OR.
747 * Calling this method with an empty string for @a field will cause
748 * a @c Xapian::InvalidArgumentError.
750 * If you call @c add_prefix() and @c add_boolean_prefix() for the
751 * same value of @a field, a @c Xapian::InvalidOperationError exception
752 * will be thrown.
754 * In 1.0.3 and earlier, subsequent calls to this method with the same
755 * value of @a field had no effect.
757 * @param field The user visible field name
758 * @param prefix The term prefix to map this to
759 * @param exclusive If true, each document can have at most one term with
760 * this prefix, so multiple filters with this prefix
761 * should be combined with OP_OR. If false, each
762 * document can have multiple terms with this prefix, so
763 * multiple filters should be combined with OP_AND, like
764 * happens with filters with different prefixes.
765 * [default: true]
767 void add_boolean_prefix(const std::string &field, const std::string &prefix,
768 bool exclusive = true);
770 /** Register a FieldProcessor for a boolean prefix.
772 * Experimental API - may change.
774 void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
775 bool exclusive = true);
777 /// Iterate over terms omitted from the query as stopwords.
778 TermIterator stoplist_begin() const;
779 TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
780 return TermIterator();
783 /// Iterate over unstemmed forms of the given (stemmed) term used in the query.
784 TermIterator unstem_begin(const std::string &term) const;
785 TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
786 return TermIterator();
789 /// Register a ValueRangeProcessor.
790 void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
792 /** Get the spelling-corrected query string.
794 * This will only be set if FLAG_SPELLING_CORRECTION is specified when
795 * QueryParser::parse_query() was last called.
797 * If there were no corrections, an empty string is returned.
799 std::string get_corrected_query_string() const;
801 /// Return a string describing this object.
802 std::string get_description() const;
805 inline void
806 QueryParser::set_max_wildcard_expansion(Xapian::termcount max_expansion)
808 set_max_expansion(max_expansion,
809 Xapian::Query::WILDCARD_LIMIT_ERROR,
810 FLAG_WILDCARD);
813 /// @private @internal Helper for sortable_serialise().
814 XAPIAN_VISIBILITY_DEFAULT
815 size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
817 /** Convert a floating point number to a string, preserving sort order.
819 * This method converts a floating point number to a string, suitable for
820 * using as a value for numeric range restriction, or for use as a sort
821 * key.
823 * The conversion is platform independent.
825 * The conversion attempts to ensure that, for any pair of values supplied
826 * to the conversion algorithm, the result of comparing the original
827 * values (with a numeric comparison operator) will be the same as the
828 * result of comparing the resulting values (with a string comparison
829 * operator). On platforms which represent doubles with the precisions
830 * specified by IEEE_754, this will be the case: if the representation of
831 * doubles is more precise, it is possible that two very close doubles
832 * will be mapped to the same string, so will compare equal.
834 * Note also that both zero and -zero will be converted to the same
835 * representation: since these compare equal, this satisfies the
836 * comparison constraint, but it's worth knowing this if you wish to use
837 * the encoding in some situation where this distinction matters.
839 * Handling of NaN isn't (currently) guaranteed to be sensible.
841 * @param value The number to serialise.
843 inline std::string sortable_serialise(double value) {
844 char buf[9];
845 return std::string(buf, sortable_serialise_(value, buf));
848 /** Convert a string encoded using @a sortable_serialise back to a floating
849 * point number.
851 * This expects the input to be a string produced by @a sortable_serialise().
852 * If the input is not such a string, the value returned is undefined (but
853 * no error will be thrown).
855 * The result of the conversion will be exactly the value which was
856 * supplied to @a sortable_serialise() when making the string on platforms
857 * which represent doubles with the precisions specified by IEEE_754, but
858 * may be a different (nearby) value on other platforms.
860 * @param serialised The serialised string to decode.
862 XAPIAN_VISIBILITY_DEFAULT
863 double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
867 #endif // XAPIAN_INCLUDED_QUERYPARSER_H