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_search_PhraseQuery_ 00008 #define _lucene_search_PhraseQuery_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "SearchHeader.h" 00015 #include "Scorer.h" 00016 #include "BooleanQuery.h" 00017 #include "TermQuery.h" 00018 00019 #include "CLucene/index/Term.h" 00020 #include "CLucene/index/Terms.h" 00021 #include "CLucene/index/IndexReader.h" 00022 00023 #include "CLucene/util/StringBuffer.h" 00024 #include "CLucene/util/VoidList.h" 00025 00026 #include "ExactPhraseScorer.h" 00027 #include "SloppyPhraseScorer.h" 00028 00029 CL_NS_DEF(search) 00030 // A Query that matches documents containing a particular sequence of terms. 00031 // This may be combined with other terms with a {@link BooleanQuery}. 00032 class PhraseQuery: public Query { 00033 private: 00034 CL_NS(util)::CLVector<int32_t,CL_NS(util)::Deletor::DummyInt32> positions; 00035 int32_t slop; 00036 00037 const TCHAR* field; 00038 CL_NS(util)::CLVector<CL_NS(index)::Term*> terms; 00039 00040 00041 class PhraseWeight: public Weight { 00042 private: 00043 Searcher* searcher; 00044 float_t value; 00045 float_t idf; 00046 float_t queryNorm; 00047 float_t queryWeight; 00048 PhraseQuery* _this; 00049 public: 00050 PhraseWeight(Searcher* searcher, PhraseQuery* _this); 00051 ~PhraseWeight(); 00052 TCHAR* toString(); 00053 00054 Query* getQuery(); 00055 float_t getValue(); 00056 00057 float_t sumOfSquaredWeights(); 00058 void normalize(float_t queryNorm); 00059 Scorer* scorer(CL_NS(index)::IndexReader* reader); 00060 void explain(CL_NS(index)::IndexReader* reader, int32_t doc, Explanation* ret); 00061 TCHAR* toString(TCHAR* f); 00062 bool equals(PhraseWeight* o); 00063 }; 00064 friend class PhraseWeight; 00065 protected: 00066 Weight* _createWeight(Searcher* searcher); 00067 PhraseQuery(const PhraseQuery& clone); 00068 public: 00069 //Constructor 00070 PhraseQuery(); 00071 00072 //Destructor 00073 ~PhraseQuery(); 00074 00075 //Returns the string "PhraseQuery" 00076 const TCHAR* getQueryName() const; 00077 static const TCHAR* getClassName(); 00078 00079 //Sets the number of other words permitted between words in query phrase. 00080 //If zero, then this is an exact phrase search. For larger values this works 00081 //like a WITHIN or NEAR operator. 00082 // 00083 //The slop is in fact an edit-distance, where the units correspond to 00084 //moves of terms in the query phrase out of position. For example, to switch 00085 //the order of two words requires two moves (the first move places the words 00086 //atop one another), so to permit re-orderings of phrases, the slop must be 00087 //at least two. 00088 // 00089 //More exact matches are scored higher than sloppier matches, thus search 00090 //results are sorted by exactness. 00091 // 00092 //The slop is zero by default, requiring exact matches. 00093 void setSlop(const int32_t s) { slop = s; } 00094 00095 //Returns the slop. See setSlop(). 00096 int32_t getSlop() const { return slop; } 00097 00098 //Adds a term to the end of the query phrase. 00099 void add(CL_NS(index)::Term* term); 00100 void add(CL_NS(index)::Term* term, int32_t position); 00101 00102 00103 00104 //Returns the sum of squared weights 00105 float_t sumOfSquaredWeights(Searcher* searcher); 00106 00107 //Normalizes the Weight 00108 void normalize(const float_t norm); 00109 00110 Scorer* scorer(CL_NS(index)::IndexReader* reader); 00111 00112 //added by search highlighter 00113 CL_NS(index)::Term** getTerms() const; 00114 _CL_DEPRECATED( deleteDocuments ) int32_t* getPositions() const; 00115 void getPositions(Array<int32_t>& result) const; 00116 const TCHAR* getFieldName() const{ return field; } 00117 00118 //Prints a user-readable version of this query. 00119 TCHAR* toString(const TCHAR* f) const; 00120 00121 Query* clone() const; 00122 bool equals(CL_NS(search)::Query *) const; 00123 00124 size_t hashCode() const; 00125 }; 00126 CL_NS_END 00127 #endif