2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using Parameter
= Lucene
.Net
.Util
.Parameter
;
20 namespace Lucene
.Net
.Search
23 /// <summary>A clause in a BooleanQuery. </summary>
25 public class BooleanClause
29 public sealed class Occur
: Parameter
32 internal Occur(System
.String name
):base(name
)
36 public override System
.String
ToString()
45 /// <summary>Use this operator for terms that <i>must</i> appear in the matching documents. </summary>
46 public static readonly Occur MUST
= new Occur("MUST");
47 /// <summary>Use this operator for terms that <i>should</i> appear in the
48 /// matching documents. For a BooleanQuery with two <code>SHOULD</code>
49 /// subqueries, at least one of the queries must appear in the matching documents.
51 public static readonly Occur SHOULD
= new Occur("SHOULD");
52 /// <summary>Use this operator for terms that <i>must not</i> appear in the matching documents.
53 /// Note that it is not possible to search for queries that only consist
54 /// of a <code>MUST_NOT</code> query.
56 public static readonly Occur MUST_NOT
= new Occur("MUST_NOT");
59 /// <summary>The query whose matching documents are combined by the boolean query.</summary>
60 /// <deprecated> use {@link #SetQuery(Query)} instead
62 public Query query
; // TODO: decrease visibility for Lucene 2.0
64 /// <summary>If true, documents documents which <i>do not</i>
65 /// match this sub-query will <i>not</i> match the boolean query.
67 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
69 public bool required
= false; // TODO: decrease visibility for Lucene 2.0
71 /// <summary>If true, documents documents which <i>do</i>
72 /// match this sub-query will <i>not</i> match the boolean query.
74 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
76 public bool prohibited
= false; // TODO: decrease visibility for Lucene 2.0
78 private Occur occur
= Occur
.SHOULD
;
80 /// <summary>Constructs a BooleanClause with query <code>q</code>, required
81 /// <code>r</code> and prohibited <code>p</code>.
83 /// <deprecated> use BooleanClause(Query, Occur) instead
85 /// <li>For BooleanClause(query, true, false) use BooleanClause(query, BooleanClause.Occur.MUST)
86 /// <li>For BooleanClause(query, false, false) use BooleanClause(query, BooleanClause.Occur.SHOULD)
87 /// <li>For BooleanClause(query, false, true) use BooleanClause(query, BooleanClause.Occur.MUST_NOT)
90 public BooleanClause(Query q
, bool r
, bool p
)
92 // TODO: remove for Lucene 2.0
100 // prohibited && required doesn't make sense, but we want the old behaviour:
101 occur
= Occur
.MUST_NOT
;
112 occur
= Occur
.MUST_NOT
;
116 occur
= Occur
.SHOULD
;
121 /// <summary>Constructs a BooleanClause.</summary>
122 public BooleanClause(Query query
, Occur occur
)
129 public virtual Occur
GetOccur()
134 public virtual void SetOccur(Occur occur
)
140 public virtual Query
GetQuery()
145 public virtual void SetQuery(Query query
)
150 public virtual bool IsProhibited()
155 public virtual bool IsRequired()
160 private void SetFields(Occur occur
)
162 if (occur
== Occur
.MUST
)
167 else if (occur
== Occur
.SHOULD
)
172 else if (occur
== Occur
.MUST_NOT
)
179 throw new System
.ArgumentException("Unknown operator " + occur
);
183 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
184 public override bool Equals(System
.Object o
)
186 if (!(o
is BooleanClause
))
188 BooleanClause other
= (BooleanClause
) o
;
189 return this.query
.Equals(other
.query
) && (this.required
== other
.required
) && (this.prohibited
== other
.prohibited
);
192 /// <summary>Returns a hash code value for this object.</summary>
193 public override int GetHashCode()
195 return query
.GetHashCode() ^
(this.required
?1:0) ^
(this.prohibited
?2:0);
199 public override System
.String
ToString()
201 return occur
.ToString() + query
.ToString();