Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Search / BooleanClause.cs
blob7b447ab0bbf85b279efbb439c667bcb5b1fa83e4
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
16 using System;
17 using Parameter = Lucene.Net.Util.Parameter;
18 namespace Lucene.Net.Search
20 /// <summary>A clause in a BooleanQuery. </summary>
21 [Serializable]
22 public class BooleanClause
25 [Serializable]
26 public sealed class Occur:Parameter
29 internal Occur(System.String name):base(name)
33 public override System.String ToString()
35 if (this == MUST)
36 return "+";
37 if (this == MUST_NOT)
38 return "-";
39 return "";
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.
47 /// </summary>
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.
52 /// </summary>
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
58 /// </deprecated>
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.
63 /// </summary>
64 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
65 /// </deprecated>
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.
70 /// </summary>
71 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
72 /// </deprecated>
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>.
79 /// </summary>
80 /// <deprecated> use BooleanClause(Query, Occur) instead
81 /// <ul>
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)
85 /// </ul>
86 /// </deprecated>
87 public BooleanClause(Query q, bool r, bool p)
89 // TODO: remove for Lucene 2.0
90 query = q;
91 required = r;
92 prohibited = p;
93 if (required)
95 if (prohibited)
97 // prohibited && required doesn't make sense, but we want the old behaviour:
98 occur = Occur.MUST_NOT;
100 else
102 occur = Occur.MUST;
105 else
107 if (prohibited)
109 occur = Occur.MUST_NOT;
111 else
113 occur = Occur.SHOULD;
118 /// <summary>Constructs a BooleanClause.</summary>
119 public BooleanClause(Query query, Occur occur)
121 this.query = query;
122 this.occur = occur;
123 SetFields(occur);
126 public virtual Occur GetOccur()
128 return occur;
131 public virtual void SetOccur(Occur occur)
133 this.occur = occur;
134 SetFields(occur);
137 public virtual Query GetQuery()
139 return query;
142 public virtual void SetQuery(Query query)
144 this.query = query;
147 public virtual bool IsProhibited()
149 return prohibited;
152 public virtual bool IsRequired()
154 return required;
157 private void SetFields(Occur occur)
159 if (occur == Occur.MUST)
161 required = true;
162 prohibited = false;
164 else if (occur == Occur.SHOULD)
166 required = false;
167 prohibited = false;
169 else if (occur == Occur.MUST_NOT)
171 required = false;
172 prohibited = true;
174 else
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))
184 return false;
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();