CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_queryParser_QueryParser_ 00008 #define _lucene_queryParser_QueryParser_ 00009 00010 00011 //#include "CLucene/analysis/AnalysisHeader.h" 00012 CL_CLASS_DEF(util,Reader) 00013 //#include "CLucene/search/SearchHeader.h" 00014 CL_CLASS_DEF(index,Term) 00015 CL_CLASS_DEF(analysis,Analyzer) 00016 CL_CLASS_DEF(search,Query) 00017 CL_CLASS_DEF(search,BooleanClause) 00018 CL_CLASS_DEF(queryParser,TokenList) 00019 00020 #include <vector> 00021 00022 //#include "TokenList.h" 00023 #include "QueryToken.h" 00024 //#include "QueryParserBase.h" 00025 //#include "Lexer.h" 00026 00027 CL_NS_DEF(queryParser) 00028 00029 00030 00034 class CLUCENE_EXPORT QueryParserBase:LUCENE_BASE 00035 { 00036 protected: 00037 /* The actual operator the parser uses to combine query terms */ 00038 int defaultOperator; 00039 int32_t phraseSlop; 00040 00041 bool lowercaseExpandedTerms; 00042 00043 LUCENE_STATIC_CONSTANT(int, CONJ_NONE=0); 00044 LUCENE_STATIC_CONSTANT(int, CONJ_AND=1); 00045 LUCENE_STATIC_CONSTANT(int, CONJ_OR=2); 00046 00047 LUCENE_STATIC_CONSTANT(int, MOD_NONE=0); 00048 LUCENE_STATIC_CONSTANT(int, MOD_NOT=10); 00049 LUCENE_STATIC_CONSTANT(int, MOD_REQ=11); 00050 00051 CL_NS(analysis)::Analyzer* analyzer; 00052 00053 public: 00054 QueryParserBase(CL_NS(analysis)::Analyzer* analyzer); 00055 ~QueryParserBase(); 00056 00061 void setLowercaseExpandedTerms(bool lowercaseExpandedTerms); 00062 00066 bool getLowercaseExpandedTerms() const; 00067 00068 //values used for setOperator 00069 LUCENE_STATIC_CONSTANT(int, OR_OPERATOR=0); 00070 LUCENE_STATIC_CONSTANT(int, AND_OPERATOR=1); 00071 00080 void setDefaultOperator(int oper); 00085 int getDefaultOperator() const; 00086 00087 //public so that the lexer can call this 00088 virtual void throwParserException(const TCHAR* message, TCHAR ch, int32_t col, int32_t line ); 00089 00094 void setPhraseSlop(int phraseSlop) { this->phraseSlop = phraseSlop; } 00095 00099 int getPhraseSlop() { return phraseSlop; } 00100 00101 protected: 00102 00106 void discardEscapeChar(TCHAR* token) const; 00107 00108 //Analyzes the expanded term termStr with the StandardFilter and the LowerCaseFilter. 00109 TCHAR* AnalyzeExpandedTerm(const TCHAR* field, TCHAR* termStr); 00110 00111 // Adds the next parsed clause. 00112 virtual void AddClause(std::vector<CL_NS(search)::BooleanClause*>& clauses, int32_t conj, int32_t mods, CL_NS(search)::Query* q); 00113 00119 virtual CL_NS(search)::Query* GetFieldQuery(const TCHAR* field, TCHAR* queryText); 00120 00125 virtual CL_NS(search)::Query* GetFieldQuery(const TCHAR* field, TCHAR* queryText, int32_t slop); 00126 00150 virtual CL_NS(search)::Query* GetPrefixQuery(const TCHAR* field, TCHAR* termStr); 00151 00173 virtual CL_NS(search)::Query* GetWildcardQuery(const TCHAR* field, TCHAR* termStr); 00174 00186 virtual CL_NS(search)::Query* GetFuzzyQuery(const TCHAR* field, TCHAR* termStr); 00187 00203 virtual CL_NS(search)::Query* GetBooleanQuery(std::vector<CL_NS(search)::BooleanClause*>& clauses); 00204 virtual CL_NS(search)::Query* GetBooleanQuery(std::vector<CL_NS(search)::BooleanClause*>& clauses, bool disableCoord ); 00205 00209 virtual CL_NS(search)::Query* GetRangeQuery(const TCHAR* field, TCHAR* part1, TCHAR* part2, bool inclusive); 00210 virtual CL_NS(search)::Query* ParseRangeQuery(const TCHAR* field, TCHAR* str, bool inclusive); 00211 }; 00212 00243 class CLUCENE_EXPORT QueryParser : public QueryParserBase 00244 { 00245 private: 00246 const TCHAR* field; 00247 TokenList* tokens; 00248 public: 00253 QueryParser(const TCHAR* field, CL_NS(analysis)::Analyzer* analyzer); 00254 ~QueryParser(); 00255 00262 virtual CL_NS(search)::Query* parse(const TCHAR* query); 00263 00270 virtual CL_NS(search)::Query* parse(CL_NS(util)::Reader* reader); 00271 00276 static CL_NS(search)::Query* parse(const TCHAR* query, const TCHAR* field, CL_NS(analysis)::Analyzer* analyzer); 00277 00278 CL_NS(analysis)::Analyzer* getAnalyzer() { return analyzer; } 00279 00283 const TCHAR* getField(); 00284 00285 //deprecated functions 00286 _CL_DEPRECATED( setLowercaseExpandedTerms ) void setLowercaseWildcardTerms(bool lowercaseWildcardTerms); 00287 _CL_DEPRECATED( getLowercaseExpandedTerms ) bool getLowercaseWildcardTerms() const; 00288 protected: 00289 //these functions may be defined under certain compilation conditions. 00290 //note that this functionality is deprecated, you should create your own queryparser 00291 //if you want to remove this functionality...it will be removed... be warned! 00292 #ifdef NO_PREFIX_QUERY 00293 virtual CL_NS(search)::Query* GetPrefixQuery(const TCHAR* field,const TCHAR* termStr){ return NULL; } 00294 #endif 00295 #ifdef NO_FUZZY_QUERY 00296 virtual CL_NS(search)::Query* GetFuzzyQuery(const TCHAR* field,const TCHAR* termStr){ return NULL; } 00297 #endif 00298 #ifdef NO_RANGE_QUERY 00299 virtual CL_NS(search)::Query* GetRangeQuery(const TCHAR* field, const TCHAR* part1, const TCHAR* part2, bool inclusive) { return NULL; } 00300 #endif 00301 #ifdef NO_WILDCARD_QUERY 00302 virtual CL_NS(search)::Query* GetWildcardQuery(const TCHAR* field, TCHAR* termStr) { return NULL; } 00303 #endif 00304 private: 00309 int32_t MatchConjunction(); 00310 00315 int32_t MatchModifier(); 00316 00321 CL_NS(search)::Query* MatchQuery(const TCHAR* field); 00322 00327 CL_NS(search)::Query* MatchClause(const TCHAR* field); 00328 00337 CL_NS(search)::Query* MatchTerm(const TCHAR* field); 00338 00343 QueryToken* MatchQueryToken(QueryToken::Types expectedType); 00344 00349 void ExtractAndDeleteToken(void); 00350 }; 00351 CL_NS_END 00352 #endif