Xapian::QueryParser Class Reference

Build a Xapian::Query object from a user query string. More...

#include <queryparser.h>

Collaboration diagram for Xapian::QueryParser:

Collaboration graph
[legend]
List of all members.

Public Types

enum  feature_flag {
  FLAG_BOOLEAN = 1, FLAG_PHRASE = 2, FLAG_LOVEHATE = 4, FLAG_BOOLEAN_ANY_CASE = 8,
  FLAG_WILDCARD = 16, FLAG_PURE_NOT = 32, FLAG_PARTIAL = 64, FLAG_SPELLING_CORRECTION = 128,
  FLAG_SYNONYM = 256, FLAG_AUTO_SYNONYMS = 512, FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS
}
 Enum of feature flags. More...
enum  stem_strategy { STEM_NONE, STEM_SOME, STEM_ALL }

Public Member Functions

 QueryParser (const QueryParser &o)
 Copy constructor.
QueryParseroperator= (const QueryParser &o)
 Assignment.
 QueryParser ()
 Default constructor.
 ~QueryParser ()
 Destructor.
void set_stemmer (const Xapian::Stem &stemmer)
 Set the stemmer.
void set_stemming_strategy (stem_strategy strategy)
 Set the stemming strategy.
void set_stopper (const Stopper *stop=NULL)
 Set the stopper.
void set_default_op (Query::op default_op)
 Set the default boolean operator.
Query::op get_default_op () const
 Get the default boolean operator.
void set_database (const Database &db)
 Specify the database being searched.
Query parse_query (const std::string &query_string, unsigned flags=FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE, const std::string &default_prefix="")
 Parse a query.
void add_prefix (const std::string &field, const std::string &prefix)
 Add a probabilistic term prefix.
void add_boolean_prefix (const std::string &field, const std::string &prefix)
 Add a boolean term prefix allowing the user to restrict a search with a boolean filter specified in the free text query.
TermIterator stoplist_begin () const
 Iterate over terms omitted from the query as stopwords.
TermIterator stoplist_end () const
TermIterator unstem_begin (const std::string &term) const
 Iterate over unstemmed forms of the given (stemmed) term used in the query.
TermIterator unstem_end (const std::string &) const
void add_valuerangeprocessor (Xapian::ValueRangeProcessor *vrproc)
 Register a ValueRangeProcessor.
std::string get_corrected_query_string () const
 Get the spelling-corrected query string.
std::string get_description () const
 Return a string describing this object.

Private Attributes

Xapian::Internal::RefCntPtr<
Internal
internal
 

For internal use only.

Reference counted internals.



Classes

class  Internal

Detailed Description

Build a Xapian::Query object from a user query string.

Definition at line 246 of file queryparser.h.


Member Enumeration Documentation

enum Xapian::QueryParser::feature_flag

Enum of feature flags.

Enumerator:
FLAG_BOOLEAN  Support AND, OR, etc and bracketed subexpressions.
FLAG_PHRASE  Support quoted phrases.
FLAG_LOVEHATE  Support + and -.
FLAG_BOOLEAN_ANY_CASE  Support AND, OR, etc even if they aren't in ALLCAPS.
FLAG_WILDCARD  Support right truncation (e.g.

Xap*).

NB: You need to tell the QueryParser object which database to expand wildcards from by calling set_database.

FLAG_PURE_NOT  Allow queries such as 'NOT apples'.

These require the use of a list of all documents in the database which is potentially expensive, so this feature isn't enabled by default.

FLAG_PARTIAL  Enable partial matching.

Partial matching causes the parser to treat the query as a "partially entered" search. This will automatically treat the final word as a wildcarded match, unless it is followed by whitespace, to produce more stable results from interactive searches.

NB: You need to tell the QueryParser object which database to expand wildcards from by calling set_database.

FLAG_SPELLING_CORRECTION  Enable spelling correction.

For each word in the query which doesn't exist as a term in the database, Database::get_spelling_suggestion() will be called and if a suggestion is returned, a corrected version of the query string will be built up which can be read using QueryParser::get_corrected_query_string(). The query returned is based on the uncorrected query string however - if you want a parsed query based on the corrected query string, you must call QueryParser::parse_query() again.

NB: You must also call set_database() for this to work.

FLAG_SYNONYM  Enable synonym operator '~'.

NB: You must also call set_database() for this to work.

FLAG_AUTO_SYNONYMS  Enable automatic use of synonyms for single terms.

NB: You must also call set_database() for this to work.

FLAG_AUTO_MULTIWORD_SYNONYMS  Enable automatic use of synonyms for single terms and groups of terms.

NB: You must also call set_database() for this to work.

Definition at line 254 of file queryparser.h.

enum Xapian::QueryParser::stem_strategy

Enumerator:
STEM_NONE 
STEM_SOME 
STEM_ALL 

Definition at line 324 of file queryparser.h.


Constructor & Destructor Documentation

QueryParser::QueryParser ( const QueryParser o  ) 

Copy constructor.

Definition at line 59 of file queryparser.cc.

QueryParser::QueryParser (  ) 

Default constructor.

Definition at line 68 of file queryparser.cc.

QueryParser::~QueryParser (  ) 

Destructor.

Definition at line 70 of file queryparser.cc.


Member Function Documentation

QueryParser & QueryParser::operator= ( const QueryParser o  ) 

Assignment.

Definition at line 62 of file queryparser.cc.

References internal.

void QueryParser::set_stemmer ( const Xapian::Stem stemmer  ) 

Set the stemmer.

This sets the stemming algorithm which will be used by the query parser. Note that the stemming algorithm will only be used according to the stemming strategy set by set_stemming_strategy(), which defaults to STEM_NONE. Therefore, to use a stemming algorithm, you will also need to call set_stemming_strategy() with a value other than STEM_NONE.

Definition at line 73 of file queryparser.cc.

References stemmer.

Referenced by DEFINE_TESTCASE(), main(), test_qp_default_op1(), test_qp_default_prefix1(), test_qp_default_prefix2(), test_qp_flag_partial1(), test_qp_flag_pure_not1(), test_qp_stem_all1(), test_qp_synonym1(), test_qp_synonym2(), test_qp_synonym3(), and test_queryparser1().

void QueryParser::set_stemming_strategy ( stem_strategy  strategy  ) 

Set the stemming strategy.

This controls how the query parser will apply the stemming algorithm. The default value is STEM_NONE. The possible values are:

Note that the stemming algorithm is only applied to words in probabilistic fields - boolean filter terms are never stemmed.

Definition at line 79 of file queryparser.cc.

Referenced by DEFINE_TESTCASE(), main(), test_qp_default_op1(), test_qp_default_prefix1(), test_qp_default_prefix2(), test_qp_flag_partial1(), test_qp_flag_pure_not1(), test_qp_spell1(), test_qp_spell2(), test_qp_stem_all1(), test_qp_synonym1(), test_qp_synonym2(), test_qp_synonym3(), and test_queryparser1().

void QueryParser::set_stopper ( const Stopper stop = NULL  ) 

Set the stopper.

Definition at line 85 of file queryparser.cc.

Referenced by main(), test_qp_stoplist1(), and test_qp_stopper1().

void QueryParser::set_default_op ( Query::op  default_op  ) 

Set the default boolean operator.

Definition at line 91 of file queryparser.cc.

Referenced by main(), test_qp_default_op1(), test_qp_flag_wildcard1(), and test_qp_stopper1().

Query::op QueryParser::get_default_op (  )  const

Get the default boolean operator.

Definition at line 97 of file queryparser.cc.

void QueryParser::set_database ( const Database db  ) 

Specify the database being searched.

Definition at line 103 of file queryparser.cc.

Referenced by DEFINE_TESTCASE(), main(), test_qp_flag_partial1(), test_qp_flag_wildcard1(), test_qp_flag_wildcard2(), test_qp_spell1(), test_qp_spell2(), test_qp_synonym1(), test_qp_synonym2(), test_qp_synonym3(), and time_query_parse().

Query Xapian::QueryParser::parse_query ( const std::string &  query_string,
unsigned  flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
const std::string &  default_prefix = "" 
)

Parse a query.

Parameters:
query_string A free-text query as entered by a user
flags Zero or more Query::feature_flag specifying what features the QueryParser should support. Combine multiple values with bitwise-or (|).
default_prefix The default term prefix to use (default none). For example, you can pass "A" when parsing an "Author" field.

Referenced by DEFINE_TESTCASE(), main(), test_qp_default_op1(), test_qp_default_prefix1(), test_qp_default_prefix2(), test_qp_flag_bool_any_case1(), test_qp_flag_partial1(), test_qp_flag_pure_not1(), test_qp_flag_wildcard1(), test_qp_flag_wildcard2(), test_qp_odd_chars1(), test_qp_spell1(), test_qp_spell2(), test_qp_stem_all1(), test_qp_stoplist1(), test_qp_stopper1(), test_qp_synonym1(), test_qp_synonym2(), test_qp_synonym3(), test_qp_unstem_boolean_prefix(), test_qp_value_customrange1(), test_qp_value_daterange1(), test_qp_value_range1(), test_qp_value_range2(), test_qp_value_range3(), test_queryparser1(), and time_query_parse().

void Xapian::QueryParser::add_prefix ( const std::string &  field,
const std::string &  prefix 
)

Add a probabilistic term prefix.

For example:

  qp.add_prefix("author", "A");

This allows the user to search for author:Orwell which will be converted to a search for the term "Aorwell".

Multiple fields can be mapped to the same prefix. For example, you can make title: and subject: aliases for each other.

As of 1.0.4, you can call this method multiple times with the same value of field to allow a single field to be mapped to multiple prefixes. Multiple terms being generated for such a field, and combined with Xapian::Query::OP_OR.

If any prefixes are specified for the empty field name (i.e. you call this method with an empty string as the first parameter) these prefixes will be used as the default prefix. If you do this and also specify the default_prefix parameter to parse_query(), then the default_prefix parameter will override.

If you call add_prefix() and add_boolean_prefix() for the same value of field, a Xapian::InvalidOperationError exception will be thrown.

In 1.0.3 and earlier, subsequent calls to this method with the same value of field had no effect.

Parameters:
field The user visible field name
prefix The term prefix to map this to

Referenced by test_qp_default_op1(), test_qp_default_prefix1(), test_qp_default_prefix2(), test_qp_flag_partial1(), test_qp_flag_wildcard2(), and test_queryparser1().

void Xapian::QueryParser::add_boolean_prefix ( const std::string &  field,
const std::string &  prefix 
)

Add a boolean term prefix allowing the user to restrict a search with a boolean filter specified in the free text query.

For example:

  qp.add_boolean_prefix("site", "H");

This allows the user to restrict a search with site:xapian.org which will be converted to Hxapian.org combined with any probabilistic query with Xapian::Query::OP_FILTER.

If multiple boolean filters are specified in a query for the same prefix, they will be combined with the Xapian::Query::OP_OR operator. Then, if there are boolean filters for different prefixes, they will be combined with the Xapian::Query::OP_AND operator.

Multiple fields can be mapped to the same prefix (so for example you can make site: and domain: aliases for each other). Instances of fields with different aliases but the same prefix will still be combined with the OR operator.

For example, if "site" and "domain" map to "H", but author maps to "A", a search for "site:foo domain:bar author:Fred" will map to "(Hfoo OR Hbar) AND Afred".

As of 1.0.4, you can call this method multiple times with the same value of field to allow a single field to be mapped to multiple prefixes. Multiple terms being generated for such a field, and combined with Xapian::Query::OP_OR.

Calling this method with an empty string for field will cause a Xapian::InvalidArgumentError.

If you call add_prefix() and add_boolean_prefix() for the same value of field, a Xapian::InvalidOperationError exception will be thrown.

In 1.0.3 and earlier, subsequent calls to this method with the same value of field had no effect.

Parameters:
field The user visible field name
prefix The term prefix to map this to

Referenced by test_qp_default_op1(), test_qp_default_prefix2(), test_qp_unstem_boolean_prefix(), test_qp_value_range1(), test_qp_value_range2(), and test_queryparser1().

TermIterator QueryParser::stoplist_begin (  )  const

Iterate over terms omitted from the query as stopwords.

Definition at line 145 of file queryparser.cc.

Referenced by test_qp_stoplist1().

TermIterator Xapian::QueryParser::stoplist_end (  )  const [inline]

Definition at line 478 of file queryparser.h.

Referenced by test_qp_stoplist1().

TermIterator Xapian::QueryParser::unstem_begin ( const std::string &  term  )  const

Iterate over unstemmed forms of the given (stemmed) term used in the query.

Referenced by test_qp_unstem_boolean_prefix().

TermIterator Xapian::QueryParser::unstem_end ( const std::string &   )  const [inline]

Definition at line 484 of file queryparser.h.

Referenced by test_qp_unstem_boolean_prefix().

void QueryParser::add_valuerangeprocessor ( Xapian::ValueRangeProcessor vrproc  ) 

Register a ValueRangeProcessor.

Definition at line 167 of file queryparser.cc.

References Assert.

Referenced by test_qp_value_customrange1(), test_qp_value_daterange1(), test_qp_value_range1(), test_qp_value_range2(), and test_qp_value_range3().

string QueryParser::get_corrected_query_string (  )  const

Get the spelling-corrected query string.

This will only be set if FLAG_SPELLING_CORRECTION is specified when QueryParser::parse_query() was last called.

If there were no corrections, an empty string is returned.

Definition at line 174 of file queryparser.cc.

Referenced by test_qp_spell1(), and test_qp_spell2().

string QueryParser::get_description (  )  const

Return a string describing this object.

Definition at line 180 of file queryparser.cc.


Member Data Documentation

Xapian::Internal::RefCntPtr<Internal> Xapian::QueryParser::internal [private]

For internal use only.

Reference counted internals.

Definition at line 249 of file queryparser.h.

Referenced by operator=().


The documentation for this class was generated from the following files:
Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.