summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/search/PhraseQuery.h
blob: 6b3255822d5ad9dd240e774168865e8377034fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#ifndef _lucene_search_PhraseQuery_
#define _lucene_search_PhraseQuery_

#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

#include "SearchHeader.h"
#include "Scorer.h"
#include "BooleanQuery.h"
#include "TermQuery.h"

#include "CLucene/index/Term.h"
#include "CLucene/index/Terms.h"
#include "CLucene/index/IndexReader.h"

#include "CLucene/util/StringBuffer.h"
#include "CLucene/util/VoidList.h"

#include "ExactPhraseScorer.h"
#include "SloppyPhraseScorer.h"

CL_NS_DEF(search)
	// A Query that matches documents containing a particular sequence of terms.
	// This may be combined with other terms with a {@link BooleanQuery}.
	class PhraseQuery: public Query {
	private:
		CL_NS(util)::CLVector<int32_t,CL_NS(util)::Deletor::DummyInt32> positions;
		int32_t slop;

		const TCHAR* field;
		CL_NS(util)::CLVector<CL_NS(index)::Term*> terms;

    
    	class PhraseWeight: public Weight {
    	private:
    		Searcher* searcher;
    		qreal value;
    		qreal idf;
    		qreal queryNorm;
    		qreal queryWeight;
    		PhraseQuery* _this;
    	public:
    		PhraseWeight(Searcher* searcher, PhraseQuery* _this);
    		~PhraseWeight();
    		TCHAR* toString();
    
    		Query* getQuery();
    		qreal getValue();
    
    		qreal sumOfSquaredWeights();
    		void normalize(qreal queryNorm);
    		Scorer* scorer(CL_NS(index)::IndexReader* reader);
    		void explain(CL_NS(index)::IndexReader* reader, int32_t doc, Explanation* ret);
    		TCHAR* toString(TCHAR* f);
    		bool equals(PhraseWeight* o);
    	};
    	friend class PhraseWeight;
	protected:
		Weight* _createWeight(Searcher* searcher);
		PhraseQuery(const PhraseQuery& clone);
	public: 
        //Constructor
        PhraseQuery();

		//Destructor
		~PhraseQuery();

        //Returns the string "PhraseQuery"
        const TCHAR* getQueryName() const;
        static const TCHAR* getClassName();
       
        //Sets the number of other words permitted between words in query phrase.
        //If zero, then this is an exact phrase search.  For larger values this works
        //like a WITHIN or NEAR operator.
        //
        //The slop is in fact an edit-distance, where the units correspond to
        //moves of terms in the query phrase out of position.  For example, to switch
        //the order of two words requires two moves (the first move places the words
        //atop one another), so to permit re-orderings of phrases, the slop must be
        //at least two.
        //
        //More exact matches are scored higher than sloppier matches, thus search
        //results are sorted by exactness.
        //
        //The slop is zero by default, requiring exact matches.
        void setSlop(const int32_t s) { slop = s; }
        
        //Returns the slop.  See setSlop(). 
        int32_t getSlop() const { return slop; }
        
        //Adds a term to the end of the query phrase. 
        void add(CL_NS(index)::Term* term);
		void add(CL_NS(index)::Term* term, int32_t position);


        
		//Returns the sum of squared weights 
        qreal sumOfSquaredWeights(Searcher* searcher);
        
		//Normalizes the Weight
        void normalize(const qreal norm);
        
        Scorer* scorer(CL_NS(index)::IndexReader* reader);
        
        //added by search highlighter
        CL_NS(index)::Term** getTerms() const;
		_CL_DEPRECATED( deleteDocuments ) int32_t* getPositions() const; ///@deprecated. use getPositions(Array<int32_t>& result)
		void getPositions(Array<int32_t>& result) const;
        const TCHAR* getFieldName() const{ return field; }
 
        //Prints a user-readable version of this query. 
        TCHAR* toString(const TCHAR* f) const;

		Query* clone() const;
		bool equals(CL_NS(search)::Query *) const;
		
		size_t hashCode() const;
	};
CL_NS_END
#endif