1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 /** A clause in a BooleanQuery. */
21 public class BooleanClause implements java.io.Serializable {
23 /** Specifies how clauses are to occur in matching documents. */
24 public static enum Occur {
26 /** Use this operator for clauses that <i>must</i> appear in the matching documents. */
27 MUST { @Override public String toString() { return "+"; } },
29 /** Use this operator for clauses that <i>should</i> appear in the
30 * matching documents. For a BooleanQuery with no <code>MUST</code>
31 * clauses one or more <code>SHOULD</code> clauses must match a document
32 * for the BooleanQuery to match.
33 * @see BooleanQuery#setMinimumNumberShouldMatch
35 SHOULD { @Override public String toString() { return ""; } },
37 /** Use this operator for clauses that <i>must not</i> appear in the matching documents.
38 * Note that it is not possible to search for queries that only consist
39 * of a <code>MUST_NOT</code> clause. */
40 MUST_NOT { @Override public String toString() { return "-"; } };
44 /** The query whose matching documents are combined by the boolean query.
51 /** Constructs a BooleanClause.
53 public BooleanClause(Query query, Occur occur) {
59 public Occur getOccur() {
63 public void setOccur(Occur occur) {
68 public Query getQuery() {
72 public void setQuery(Query query) {
76 public boolean isProhibited() {
77 return Occur.MUST_NOT == occur;
80 public boolean isRequired() {
81 return Occur.MUST == occur;
86 /** Returns true if <code>o</code> is equal to this. */
88 public boolean equals(Object o) {
89 if (o == null || !(o instanceof BooleanClause))
91 BooleanClause other = (BooleanClause)o;
92 return this.query.equals(other.query)
93 && this.occur == other.occur;
96 /** Returns a hash code value for this object.*/
98 public int hashCode() {
99 return query.hashCode() ^ (Occur.MUST == occur?1:0) ^ (Occur.MUST_NOT == occur?2:0);
104 public String toString() {
105 return occur.toString() + query.toString();