cvsimport
[beagle.git] / beagled / Lucene.Net / Search / BooleanClause.cs
blobbaff514af2d262af159e6d84f4197c77fb92a49b
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.
17 using System;
18 using Parameter = Lucene.Net.Util.Parameter;
20 namespace Lucene.Net.Search
23 /// <summary>A clause in a BooleanQuery. </summary>
24 [Serializable]
25 public class BooleanClause
28 [Serializable]
29 public sealed class Occur : Parameter
32 internal Occur(System.String name):base(name)
36 public override System.String ToString()
38 if (this == MUST)
39 return "+";
40 if (this == MUST_NOT)
41 return "-";
42 return "";
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.
50 /// </summary>
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.
55 /// </summary>
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
61 /// </deprecated>
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.
66 /// </summary>
67 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
68 /// </deprecated>
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.
73 /// </summary>
74 /// <deprecated> use {@link #SetOccur(BooleanClause.Occur)} instead
75 /// </deprecated>
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>.
82 /// </summary>
83 /// <deprecated> use BooleanClause(Query, Occur) instead
84 /// <ul>
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)
88 /// </ul>
89 /// </deprecated>
90 public BooleanClause(Query q, bool r, bool p)
92 // TODO: remove for Lucene 2.0
93 query = q;
94 required = r;
95 prohibited = p;
96 if (required)
98 if (prohibited)
100 // prohibited && required doesn't make sense, but we want the old behaviour:
101 occur = Occur.MUST_NOT;
103 else
105 occur = Occur.MUST;
108 else
110 if (prohibited)
112 occur = Occur.MUST_NOT;
114 else
116 occur = Occur.SHOULD;
121 /// <summary>Constructs a BooleanClause.</summary>
122 public BooleanClause(Query query, Occur occur)
124 this.query = query;
125 this.occur = occur;
126 SetFields(occur);
129 public virtual Occur GetOccur()
131 return occur;
134 public virtual void SetOccur(Occur occur)
136 this.occur = occur;
137 SetFields(occur);
140 public virtual Query GetQuery()
142 return query;
145 public virtual void SetQuery(Query query)
147 this.query = query;
150 public virtual bool IsProhibited()
152 return prohibited;
155 public virtual bool IsRequired()
157 return required;
160 private void SetFields(Occur occur)
162 if (occur == Occur.MUST)
164 required = true;
165 prohibited = false;
167 else if (occur == Occur.SHOULD)
169 required = false;
170 prohibited = false;
172 else if (occur == Occur.MUST_NOT)
174 required = false;
175 prohibited = true;
177 else
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))
187 return false;
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();