KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > MultiTermQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.util.ToStringUtils;
24
25 /**
26  * A {@link Query} that matches documents containing a subset of terms provided
27  * by a {@link FilteredTermEnum} enumeration.
28  * <P>
29  * <code>MultiTermQuery</code> is not designed to be used by itself.
30  * <BR>
31  * The reason being that it is not intialized with a {@link FilteredTermEnum}
32  * enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
33  * <P>
34  * For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
35  * <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
36  * {@link FuzzyTermEnum}, respectively.
37  */

38 public abstract class MultiTermQuery extends Query {
39     private Term term;
40
41     /** Constructs a query for terms matching <code>term</code>. */
42     public MultiTermQuery(Term term) {
43         this.term = term;
44     }
45
46     /** Returns the pattern term. */
47     public Term getTerm() { return term; }
48
49     /** Construct the enumeration to be used, expanding the pattern term. */
50     protected abstract FilteredTermEnum getEnum(IndexReader reader)
51       throws IOException JavaDoc;
52
53     public Query rewrite(IndexReader reader) throws IOException JavaDoc {
54       FilteredTermEnum enumerator = getEnum(reader);
55       BooleanQuery query = new BooleanQuery(true);
56       try {
57         do {
58           Term t = enumerator.term();
59           if (t != null) {
60             TermQuery tq = new TermQuery(t); // found a match
61
tq.setBoost(getBoost() * enumerator.difference()); // set the boost
62
query.add(tq, BooleanClause.Occur.SHOULD); // add to query
63
}
64         } while (enumerator.next());
65       } finally {
66         enumerator.close();
67       }
68       return query;
69     }
70
71     /** Prints a user-readable version of this query. */
72     public String JavaDoc toString(String JavaDoc field) {
73         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
74         if (!term.field().equals(field)) {
75             buffer.append(term.field());
76             buffer.append(":");
77         }
78         buffer.append(term.text());
79         buffer.append(ToStringUtils.boost(getBoost()));
80         return buffer.toString();
81     }
82
83     public boolean equals(Object JavaDoc o) {
84       if (this == o) return true;
85       if (!(o instanceof MultiTermQuery)) return false;
86
87       final MultiTermQuery multiTermQuery = (MultiTermQuery) o;
88
89       if (!term.equals(multiTermQuery.term)) return false;
90
91       return getBoost() == multiTermQuery.getBoost();
92     }
93
94     public int hashCode() {
95       return term.hashCode() + Float.floatToRawIntBits(getBoost());
96     }
97 }
98
Popular Tags