Lucene++ - a full-featured, c++ search engine
API Documentation


QueryParser.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef QUERYPARSER_H
8 #define QUERYPARSER_H
9 
10 #include "QueryParserConstants.h"
11 #include "DateTools.h"
12 #include "BooleanClause.h"
13 
14 namespace Lucene {
15 
17 
70 class LPPAPI QueryParser : public QueryParserConstants, public LuceneObject {
71 public:
76  QueryParser(LuceneVersion::Version matchVersion, const String& field, const AnalyzerPtr& analyzer);
77 
80 
83 
84  virtual ~QueryParser();
85 
87 
89  enum Operator { OR_OPERATOR, AND_OPERATOR };
90 
91 protected:
92  static const int32_t CONJ_NONE;
93  static const int32_t CONJ_AND;
94  static const int32_t CONJ_OR;
95 
96  static const int32_t MOD_NONE;
97  static const int32_t MOD_NOT;
98  static const int32_t MOD_REQ;
99 
102 
104  int32_t _jj_ntk;
107 
108  int32_t jj_la;
109  int32_t jj_gen;
111 
112  static const int32_t jj_la1_0[];
113  static const int32_t jj_la1_1[];
114 
115  struct JJCalls;
116  typedef boost::shared_ptr<JJCalls> JJCallsPtr;
117 
118  struct JJCalls {
120  gen = 0;
121  arg = 0;
122  }
123 
124  int32_t gen;
126  int32_t arg;
128  };
129 
131  bool jj_rescan;
132  int32_t jj_gc;
133 
136  int32_t jj_kind;
138  int32_t jj_endpos;
139 
140 public:
145 
147  String field;
148  int32_t phraseSlop;
149  double fuzzyMinSim;
151  std::locale locale;
152 
153  // the default date resolution
155 
156  // maps field names to date resolutions
158 
159  // The collator to use when determining range inclusion, for use when constructing RangeQuerys
161 
164 
167 
170 
171 public:
174  QueryPtr parse(const String& query);
175 
178 
180  String getField();
181 
183  double getFuzzyMinSim();
184 
186  void setFuzzyMinSim(double fuzzyMinSim);
187 
191 
194  void setFuzzyPrefixLength(int32_t fuzzyPrefixLength);
195 
198  void setPhraseSlop(int32_t phraseSlop);
199 
201  int32_t getPhraseSlop();
202 
207  void setAllowLeadingWildcard(bool allowLeadingWildcard);
208 
211 
217  void setEnablePositionIncrements(bool enable);
218 
221 
228 
231 
234  void setLowercaseExpandedTerms(bool lowercaseExpandedTerms);
235 
238 
246 
249 
251  void setLocale(std::locale locale);
252 
254  std::locale getLocale();
255 
261 
265  void setDateResolution(const String& fieldName, DateTools::Resolution dateResolution);
266 
269  DateTools::Resolution getDateResolution(const String& fieldName);
270 
277  void setRangeCollator(const CollatorPtr& rc);
278 
281 
283  static int main(Collection<String> args);
284 
287  int32_t Conjunction();
288  int32_t Modifiers();
289 
291  virtual QueryPtr TopLevelQuery(const String& field);
292 
293  virtual QueryPtr ParseQuery(const String& field);
294  virtual QueryPtr ParseClause(const String& field);
295  virtual QueryPtr ParseTerm(const String& field);
296 
298  virtual void ReInit(const QueryParserCharStreamPtr& stream);
299 
301  virtual void ReInit(const QueryParserTokenManagerPtr& tokenMgr);
302 
305 
307  virtual QueryParserTokenPtr getToken(int32_t index);
308 
310  virtual void generateParseException();
311 
313  virtual void enable_tracing();
314 
316  virtual void disable_tracing();
317 
318 protected:
321 
322  virtual void addClause(Collection<BooleanClausePtr> clauses, int32_t conj, int32_t mods, const QueryPtr& q);
323 
326  virtual QueryPtr getFieldQuery(const String& field, const String& queryText);
327 
330  virtual QueryPtr getFieldQuery(const String& field, const String& queryText, int32_t slop);
331 
333  virtual QueryPtr getRangeQuery(const String& field, const String& part1, const String& part2, bool inclusive);
334 
337  BooleanQueryPtr newBooleanQuery(bool disableCoord);
338 
344 
349 
353 
357 
362 
368  QueryPtr newFuzzyQuery(const TermPtr& term, double minimumSimilarity, int32_t prefixLength);
369 
376  QueryPtr newRangeQuery(const String& field, const String& part1, const String& part2, bool inclusive);
377 
381 
386 
395 
404  virtual QueryPtr getBooleanQuery(Collection<BooleanClausePtr> clauses, bool disableCoord);
405 
421  virtual QueryPtr getWildcardQuery(const String& field, const String& termStr);
422 
437  virtual QueryPtr getPrefixQuery(const String& field, const String& termStr);
438 
445  virtual QueryPtr getFuzzyQuery(const String& field, const String& termStr, double minSimilarity);
446 
449  String discardEscapeChar(const String& input);
450 
452  static int32_t hexToInt(wchar_t c);
453 
456  static String escape(const String& s);
457 
458  bool jj_2_1(int32_t xla);
459  bool jj_3R_2();
460  bool jj_3_1();
461  bool jj_3R_3();
462 
464  bool jj_scan_token(int32_t kind);
465  int32_t jj_ntk();
466  void jj_add_error_token(int32_t kind, int32_t pos);
468  void jj_save(int32_t index, int32_t xla);
469 };
470 
471 }
472 
473 #endif
#define LUCENE_CLASS(Name)
Definition: LuceneObject.h:24
Occur
Specifies how clauses are to occur in matching documents.
Definition: BooleanClause.h:18
Resolution
Definition: DateTools.h:35
Base class for all Lucene classes.
Definition: LuceneObject.h:31
Version
Definition: Constants.h:40
Token literal values and constants.
Definition: QueryParserConstants.h:15
The most important method is parse(const String&).
Definition: QueryParser.h:70
virtual QueryPtr getFieldQuery(const String &field, const String &queryText, int32_t slop)
Base implementation delegates to getFieldQuery(const String&, const String&). This method may be over...
double fuzzyMinSim
Definition: QueryParser.h:149
bool enablePositionIncrements
Definition: QueryParser.h:144
void setAllowLeadingWildcard(bool allowLeadingWildcard)
Set to true to allow leading wildcard characters.
MapStringResolution fieldToDateResolution
Definition: QueryParser.h:157
int32_t jj_gen
Definition: QueryParser.h:109
virtual QueryPtr ParseQuery(const String &field)
virtual QueryPtr getFieldQuery(const String &field, const String &queryText)
Use the analyzer to get all the tokens, and then build a TermQuery, PhraseQuery, or nothing based on ...
static int main(Collection< String > args)
Command line tool to test QueryParser, using SimpleAnalyzer.
CollatorPtr rangeCollator
Definition: QueryParser.h:160
virtual QueryPtr getWildcardQuery(const String &field, const String &termStr)
Factory method for generating a query. Called when parser parses an input term token that contains on...
void setLowercaseExpandedTerms(bool lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not...
void setEnablePositionIncrements(bool enable)
Set to true to enable position increments in result query.
PhraseQueryPtr newPhraseQuery()
Builds a new PhraseQuery instance.
virtual QueryPtr getFuzzyQuery(const String &field, const String &termStr, double minSimilarity)
Factory method for generating a query (similar to getWildcardQuery). Called when parser parses an inp...
virtual void generateParseException()
Generate QueryParserError exception.
void jj_add_error_token(int32_t kind, int32_t pos)
std::locale getLocale()
Returns current locale, allowing access by subclasses.
void setRangeCollator(const CollatorPtr &rc)
Sets the collator used to determine index term inclusion in ranges for RangeQuerys.
bool jj_2_1(int32_t xla)
QueryParser(const QueryParserCharStreamPtr &stream)
Constructor with user supplied QueryParserCharStream.
static const int32_t MOD_REQ
Definition: QueryParser.h:98
virtual void disable_tracing()
Disable tracing.
String discardEscapeChar(const String &input)
Returns a String where the escape char has been removed, or kept only once if there was a double esca...
static const int32_t MOD_NONE
Definition: QueryParser.h:96
virtual void addClause(Collection< BooleanClausePtr > clauses, int32_t conj, int32_t mods, const QueryPtr &q)
BooleanQueryPtr newBooleanQuery(bool disableCoord)
Builds a new BooleanQuery instance.
QueryParserTokenPtr jj_lastpos
Definition: QueryParser.h:106
DateTools::Resolution getDateResolution(const String &fieldName)
Returns the date resolution that is used by RangeQueries for the given field. Returns null,...
int32_t fuzzyPrefixLength
Definition: QueryParser.h:150
virtual QueryParserTokenPtr getToken(int32_t index)
Get the specific Token.
void setDateResolution(const String &fieldName, DateTools::Resolution dateResolution)
Sets the date resolution used by RangeQueries for a specific field.
int32_t _jj_ntk
Next token.
Definition: QueryParser.h:104
bool jj_scan_token(int32_t kind)
Collection< int32_t > jj_lasttokens
Definition: QueryParser.h:137
virtual QueryParserTokenPtr getNextToken()
Get the next Token.
void setMultiTermRewriteMethod(const RewriteMethodPtr &method)
By default QueryParser uses MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT when creating a Prefix...
QueryParser(LuceneVersion::Version matchVersion, const String &field, const AnalyzerPtr &analyzer)
Constructs a query parser.
virtual ~QueryParser()
bool jj_rescan
Definition: QueryParser.h:131
Collection< JJCallsPtr > jj_2_rtns
Definition: QueryParser.h:130
bool allowLeadingWildcard
Definition: QueryParser.h:143
QueryPtr newFuzzyQuery(const TermPtr &term, double minimumSimilarity, int32_t prefixLength)
Builds a new FuzzyQuery instance.
QueryPtr newTermQuery(const TermPtr &term)
Builds a new TermQuery instance.
virtual QueryPtr getPrefixQuery(const String &field, const String &termStr)
Factory method for generating a query (similar to getWildcardQuery). Called when parser parses an inp...
AnalyzerPtr getAnalyzer()
void jj_save(int32_t index, int32_t xla)
AnalyzerPtr analyzer
Definition: QueryParser.h:146
static const int32_t CONJ_OR
Definition: QueryParser.h:94
Operator
The default operator for parsing queries. Use QueryParser#setDefaultOperator to change it.
Definition: QueryParser.h:89
void setLocale(std::locale locale)
Set locale used by date range parsing.
void setDefaultOperator(Operator op)
Sets the boolean operator of the QueryParser. In default mode (OR_OPERATOR) terms without any modifie...
RewriteMethodPtr multiTermRewriteMethod
Definition: QueryParser.h:142
QueryPtr parse(const String &query)
Parses a query string, returning a Query.
Collection< int32_t > jj_la1
Definition: QueryParser.h:110
static const int32_t CONJ_NONE
Definition: QueryParser.h:92
int32_t getPhraseSlop()
Gets the default slop for phrases.
virtual QueryPtr getBooleanQuery(Collection< BooleanClausePtr > clauses, bool disableCoord)
Factory method for generating query, given a set of clauses. By default creates a boolean query compo...
virtual void ReInit(const QueryParserTokenManagerPtr &tokenMgr)
Reinitialise.
Operator getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
int32_t jj_la
Definition: QueryParser.h:108
static const int32_t MOD_NOT
Definition: QueryParser.h:97
CollatorPtr getRangeCollator()
QueryPtr newWildcardQuery(const TermPtr &term)
Builds a new WildcardQuery instance.
Collection< int32_t > jj_expentry
Definition: QueryParser.h:135
Operator _operator
The actual operator that parser uses to combine query terms.
Definition: QueryParser.h:101
QueryPtr newRangeQuery(const String &field, const String &part1, const String &part2, bool inclusive)
Builds a new TermRangeQuery instance.
virtual void ReInit(const QueryParserCharStreamPtr &stream)
Reinitialise.
QueryPtr newMatchAllDocsQuery()
Builds a new MatchAllDocsQuery instance.
static String escape(const String &s)
Returns a String where those characters that QueryParser expects to be escaped are escaped by a prece...
QueryParserTokenPtr jj_scanpos
Definition: QueryParser.h:105
virtual QueryPtr getRangeQuery(const String &field, const String &part1, const String &part2, bool inclusive)
Builds a new TermRangeQuery instance for given min/max parts.
virtual QueryPtr TopLevelQuery(const String &field)
This makes sure that there is no garbage after the query string.
void setFuzzyMinSim(double fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 0.5.
void setFuzzyPrefixLength(int32_t fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.
void setPhraseSlop(int32_t phraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required....
String field
Definition: QueryParser.h:147
int32_t Conjunction()
Query ::= ( Clause )* Clause ::= ["+", "-"] [ ":"] ( | "(" Query ")" )
QueryParser(const QueryParserTokenManagerPtr &tokenMgr)
Constructor with generated Token Manager.
void ConstructParser(const QueryParserCharStreamPtr &stream, const QueryParserTokenManagerPtr &tokenMgr)
Construct query parser with supplied QueryParserCharStream or TokenManager.
QueryParserTokenManagerPtr token_source
Generated Token Manager.
Definition: QueryParser.h:163
virtual void enable_tracing()
Enable tracing.
QueryPtr newPrefixQuery(const TermPtr &prefix)
Builds a new PrefixQuery instance.
static int32_t hexToInt(wchar_t c)
Returns the numeric value of the hexadecimal character.
double getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
virtual QueryPtr ParseClause(const String &field)
static const int32_t CONJ_AND
Definition: QueryParser.h:93
std::locale locale
Definition: QueryParser.h:151
int32_t jj_gc
Definition: QueryParser.h:132
virtual QueryPtr ParseTerm(const String &field)
int32_t jj_endpos
Definition: QueryParser.h:138
bool getLowercaseExpandedTerms()
bool getEnablePositionIncrements()
virtual QueryPtr getBooleanQuery(Collection< BooleanClausePtr > clauses)
Factory method for generating query, given a set of clauses. By default creates a boolean query compo...
int32_t getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.
DateTools::Resolution dateResolution
Definition: QueryParser.h:154
RewriteMethodPtr getMultiTermRewriteMethod()
QueryParserTokenPtr jj_nt
Next token.
Definition: QueryParser.h:169
boost::shared_ptr< JJCalls > JJCallsPtr
Definition: QueryParser.h:115
int32_t phraseSlop
Definition: QueryParser.h:148
bool getAllowLeadingWildcard()
bool lowercaseExpandedTerms
Definition: QueryParser.h:141
BooleanClausePtr newBooleanClause(const QueryPtr &q, BooleanClause::Occur occur)
Builds a new BooleanClause instance.
MultiPhraseQueryPtr newMultiPhraseQuery()
Builds a new MultiPhraseQuery instance.
Collection< Collection< int32_t > > jj_expentries
Definition: QueryParser.h:134
int32_t jj_kind
Definition: QueryParser.h:136
void setDateResolution(DateTools::Resolution dateResolution)
Sets the default date resolution used by RangeQueries for fields for which no specific date resolutio...
QueryParserTokenPtr jj_consume_token(int32_t kind)
QueryParserTokenPtr token
Current token.
Definition: QueryParser.h:166
Definition: AbstractAllTermDocs.h:12
boost::shared_ptr< RewriteMethod > RewriteMethodPtr
Definition: LuceneTypes.h:425
boost::shared_ptr< QueryParserTokenManager > QueryParserTokenManagerPtr
Definition: LuceneTypes.h:274
HashMap< String, DateTools::Resolution > MapStringResolution
Definition: QueryParser.h:16
boost::shared_ptr< Query > QueryPtr
Definition: LuceneTypes.h:420
boost::shared_ptr< MultiPhraseQuery > MultiPhraseQueryPtr
Definition: LuceneTypes.h:385
boost::shared_ptr< Analyzer > AnalyzerPtr
Definition: LuceneTypes.h:20
boost::shared_ptr< Collator > CollatorPtr
Definition: LuceneTypes.h:525
boost::shared_ptr< Term > TermPtr
Definition: LuceneTypes.h:233
boost::shared_ptr< PhraseQuery > PhraseQueryPtr
Definition: LuceneTypes.h:411
boost::shared_ptr< BooleanClause > BooleanClausePtr
Definition: LuceneTypes.h:278
boost::shared_ptr< QueryParserCharStream > QueryParserCharStreamPtr
Definition: LuceneTypes.h:271
boost::shared_ptr< QueryParserToken > QueryParserTokenPtr
Definition: LuceneTypes.h:273
boost::shared_ptr< BooleanQuery > BooleanQueryPtr
Definition: LuceneTypes.h:279
Definition: QueryParser.h:118
int32_t arg
Definition: QueryParser.h:126
JJCallsPtr next
Definition: QueryParser.h:127
QueryParserTokenPtr first
Definition: QueryParser.h:125
JJCalls()
Definition: QueryParser.h:119
int32_t gen
Definition: QueryParser.h:124

clucene.sourceforge.net