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.
17 using Parameter
= Lucene
.Net
.Util
.Parameter
;
18 namespace Lucene
.Net
.Search
20 /// <summary>A clause in a BooleanQuery. </summary>
22 public class BooleanClause
26 public sealed class Occur
:Parameter
29 internal Occur(System
.String name
):base(name
)
33 public override System
.String
ToString()
42 /// <summary>Use this operator for terms that <i>must</i> appear in the matching documents. </summary>
43 public static readonly Occur MUST
= new Occur("MUST");
44 /// <summary>Use this operator for terms of which <i>should</i> appear in the
45 /// matching documents. For a BooleanQuery with two <code>SHOULD</code>
46 /// subqueries, at least one of the queries must appear in the matching documents.
48 public static readonly Occur SHOULD
= new Occur("SHOULD");
49 /// <summary>Use this operator for terms that <i>must not</i> appear in the matching documents.
50 /// Note that it is not possible to search for queries that only consist
51 /// of a <code>MUST_NOT</code> query.
53 public static readonly Occur MUST_NOT
= new Occur("MUST_NOT");
56 /// <summary>The query whose matching documents are combined by the boolean query.</summary>
57 /// <deprecated> use {@link #SetQuery(Query)} instead
59 public Query query
; // TODO: decrease visibility for Lucene 2.0
61 /// <summary>If true, documents documents which <i>do not</i>
62 /// match this sub-query will <i>not</i> match the boolean query.
64 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
66 public bool required
= false; // TODO: decrease visibility for Lucene 2.0
68 /// <summary>If true, documents documents which <i>do</i>
69 /// match this sub-query will <i>not</i> match the boolean query.
71 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
73 public bool prohibited
= false; // TODO: decrease visibility for Lucene 2.0
75 private Occur occur
= Occur
.SHOULD
;
77 /// <summary>Constructs a BooleanClause with query <code>q</code>, required
78 /// <code>r</code> and prohibited <code>p</code>.
80 /// <deprecated> use BooleanClause(Query, Occur) instead
82 /// <li>For BooleanClause(query, true, false) use BooleanClause(query, BooleanClause.Occur.MUST)
83 /// <li>For BooleanClause(query, false, false) use BooleanClause(query, BooleanClause.Occur.SHOULD)
84 /// <li>For BooleanClause(query, false, true) use BooleanClause(query, BooleanClause.Occur.MUST_NOT)
87 public BooleanClause(Query q
, bool r
, bool p
)
89 // TODO: remove for Lucene 2.0
97 // prohibited && required doesn't make sense, but we want the old behaviour:
98 occur
= Occur
.MUST_NOT
;
109 occur
= Occur
.MUST_NOT
;
113 occur
= Occur
.SHOULD
;
118 /// <summary>Constructs a BooleanClause.</summary>
119 public BooleanClause(Query query
, Occur occur
)
126 public virtual Occur
GetOccur()
131 public virtual void SetOccur(Occur occur
)
137 public virtual Query
GetQuery()
142 public virtual void SetQuery(Query query
)
147 public virtual bool IsProhibited()
152 public virtual bool IsRequired()
157 private void SetFields(Occur occur
)
159 if (occur
== Occur
.MUST
)
164 else if (occur
== Occur
.SHOULD
)
169 else if (occur
== Occur
.MUST_NOT
)
176 throw new System
.ArgumentException("Unknown operator " + occur
);
180 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
181 public override bool Equals(System
.Object o
)
183 if (!(o
is BooleanClause
))
185 BooleanClause other
= (BooleanClause
) o
;
186 return this.query
.Equals(other
.query
) && (this.required
== other
.required
) && (this.prohibited
== other
.prohibited
);
189 /// <summary>Returns a hash code value for this object.</summary>
190 public override int GetHashCode()
192 return query
.GetHashCode() ^
(this.required
?1:0) ^
(this.prohibited
?2:0);
196 public override System
.String
ToString()
198 return occur
.ToString() + query
.ToString();