1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #ifndef _lucene_search_BooleanQuery_
8 #define _lucene_search_BooleanQuery_
9
10 #if defined(_LUCENE_PRAGMA_ONCE)
11 # pragma once
12 #endif
13
14 #include "ConjunctionScorer.h"
15 #include "CLucene/index/IndexReader.h"
16 #include "CLucene/util/StringBuffer.h"
17 #include "SearchHeader.h"
18 #include "BooleanClause.h"
19 #include "BooleanScorer.h"
20 #include "Scorer.h"
21
CL_NS_DEF(search)22 CL_NS_DEF(search)
23
24
25 // A Query that matches documents matching boolean combinations of other
26 // queries, typically {@link TermQuery}s or {@link PhraseQuery}s.
27 class BooleanQuery:public Query {
28 public:
29 typedef CL_NS(util)::CLVector<BooleanClause*,CL_NS(util)::Deletor::Object<BooleanClause> > ClausesType;
30 private:
31 BooleanQuery::ClausesType clauses;
32 static size_t maxClauseCount;
33
34 class BooleanWeight: public Weight {
35 private:
36 Searcher* searcher;
37 CL_NS(util)::CLVector<Weight*,CL_NS(util)::Deletor::Object<Weight> > weights;
38 ClausesType* clauses;
39 BooleanQuery* parentQuery;
40 public:
41 BooleanWeight(Searcher* searcher,
42 CL_NS(util)::CLVector<BooleanClause*,CL_NS(util)::Deletor::Object<BooleanClause> >* clauses,
43 BooleanQuery* parentQuery);
44 ~BooleanWeight();
45 Query* getQuery();
46 qreal getValue();
47 qreal sumOfSquaredWeights();
48 void normalize(qreal norm);
49 Scorer* scorer(CL_NS(index)::IndexReader* reader);
50 void explain(CL_NS(index)::IndexReader* reader, int32_t doc, Explanation* ret);
51 };//booleanweight
52
53 protected:
54 Weight* _createWeight(Searcher* searcher) {
55 return _CLNEW BooleanWeight(searcher,&clauses,this);
56 }
57 BooleanQuery(const BooleanQuery& clone);
58 public:
59 /** Constructs an empty boolean query. */
60 BooleanQuery();
61
62 ~BooleanQuery();
63
64 const TCHAR* getQueryName() const;
65 static const TCHAR* getClassName();
66
67 /** Return the maximum number of clauses permitted, 1024 by default.
68 * Attempts to add more than the permitted number of clauses cause {@link
69 * TooManyClauses} to be thrown.*/
70 static size_t getMaxClauseCount();
71
72 /** Set the maximum number of clauses permitted. */
73 static void setMaxClauseCount(size_t maxClauseCount);
74
75 /** Adds a clause to a boolean query. Clauses may be:
76 * <ul>
77 * <li><code>required</code> which means that documents which <i>do not</i>
78 * match this sub-query will <i>not</i> match the boolean query;
79 * <li><code>prohibited</code> which means that documents which <i>do</i>
80 * match this sub-query will <i>not</i> match the boolean query; or
81 * <li>neither, in which case matched documents are neither prohibited from
82 * nor required to match the sub-query. However, a document must match at
83 * least 1 sub-query to match the boolean query.
84 * </ul>
85 * It is an error to specify a clause as both <code>required</code> and
86 * <code>prohibited</code>.
87 *
88 * @see #getMaxClauseCount()
89 */
90 void add(Query* query, const bool required, const bool prohibited){
91 add(query,false,required,prohibited);
92 }
93 void add(Query* query, const bool deleteQuery, const bool required, const bool prohibited);
94
95 /** Copies the clauses of this query into the array.
96 * The array must be at least as long as getClauseCount()
97 * If you want to use the clauses, make sure you null terminate it.
98 */
99 void getClauses(BooleanClause** clauses) const;
100
101 ///@deprecated
102 _CL_DEPRECATED( getClauses(clauses) ) BooleanClause** getClauses() const;
103
104 /**
105 * Give client code access to clauses.size() so we know how
106 * large the array returned by getClauses is.
107 */
108 size_t getClauseCount() const;
109
110 /** Adds a clause to a boolean query.
111 * @see #getMaxClauseCount()
112 */
113 void add(BooleanClause* clause);
114
115 Query* rewrite(CL_NS(index)::IndexReader* reader);
116 Query* clone() const;
117 bool equals(Query* o) const;
118
119 /** Prints a user-readable version of this query. */
120 TCHAR* toString(const TCHAR* field) const;
121 /** Returns a hash code value for this object.*/
122 size_t hashCode() const;
123 };
124
125 CL_NS_END
126 #endif
127