KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Set JavaDoc;
21
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.index.TermDocs;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.util.ToStringUtils;
26
27 /** A Query that matches documents containing a term.
28   This may be combined with other terms with a {@link BooleanQuery}.
29   */

30 public class TermQuery extends Query {
31   private Term term;
32
33   private class TermWeight implements Weight {
34     private Similarity similarity;
35     private float value;
36     private float idf;
37     private float queryNorm;
38     private float queryWeight;
39
40     public TermWeight(Searcher searcher)
41       throws IOException JavaDoc {
42       this.similarity = getSimilarity(searcher);
43       idf = similarity.idf(term, searcher); // compute idf
44
}
45
46     public String JavaDoc toString() { return "weight(" + TermQuery.this + ")"; }
47
48     public Query getQuery() { return TermQuery.this; }
49     public float getValue() { return value; }
50
51     public float sumOfSquaredWeights() {
52       queryWeight = idf * getBoost(); // compute query weight
53
return queryWeight * queryWeight; // square it
54
}
55
56     public void normalize(float queryNorm) {
57       this.queryNorm = queryNorm;
58       queryWeight *= queryNorm; // normalize query weight
59
value = queryWeight * idf; // idf for document
60
}
61
62     public Scorer scorer(IndexReader reader) throws IOException JavaDoc {
63       TermDocs termDocs = reader.termDocs(term);
64
65       if (termDocs == null)
66         return null;
67
68       return new TermScorer(this, termDocs, similarity,
69                             reader.norms(term.field()));
70     }
71
72     public Explanation explain(IndexReader reader, int doc)
73       throws IOException JavaDoc {
74
75       Explanation result = new Explanation();
76       result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
77
78       Explanation idfExpl =
79         new Explanation(idf, "idf(docFreq=" + reader.docFreq(term) + ")");
80
81       // explain query weight
82
Explanation queryExpl = new Explanation();
83       queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
84
85       Explanation boostExpl = new Explanation(getBoost(), "boost");
86       if (getBoost() != 1.0f)
87         queryExpl.addDetail(boostExpl);
88       queryExpl.addDetail(idfExpl);
89
90       Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
91       queryExpl.addDetail(queryNormExpl);
92
93       queryExpl.setValue(boostExpl.getValue() *
94                          idfExpl.getValue() *
95                          queryNormExpl.getValue());
96
97       result.addDetail(queryExpl);
98
99       // explain field weight
100
String JavaDoc field = term.field();
101       Explanation fieldExpl = new Explanation();
102       fieldExpl.setDescription("fieldWeight("+term+" in "+doc+
103                                "), product of:");
104
105       Explanation tfExpl = scorer(reader).explain(doc);
106       fieldExpl.addDetail(tfExpl);
107       fieldExpl.addDetail(idfExpl);
108
109       Explanation fieldNormExpl = new Explanation();
110       byte[] fieldNorms = reader.norms(field);
111       float fieldNorm =
112         fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) : 0.0f;
113       fieldNormExpl.setValue(fieldNorm);
114       fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
115       fieldExpl.addDetail(fieldNormExpl);
116
117       fieldExpl.setValue(tfExpl.getValue() *
118                          idfExpl.getValue() *
119                          fieldNormExpl.getValue());
120
121       result.addDetail(fieldExpl);
122
123       // combine them
124
result.setValue(queryExpl.getValue() * fieldExpl.getValue());
125
126       if (queryExpl.getValue() == 1.0f)
127         return fieldExpl;
128
129       return result;
130     }
131   }
132
133   /** Constructs a query for the term <code>t</code>. */
134   public TermQuery(Term t) {
135     term = t;
136   }
137
138   /** Returns the term of this query. */
139   public Term getTerm() { return term; }
140
141   protected Weight createWeight(Searcher searcher) throws IOException JavaDoc {
142     return new TermWeight(searcher);
143   }
144
145   public void extractTerms(Set JavaDoc terms) {
146     terms.add(getTerm());
147   }
148
149   /** Prints a user-readable version of this query. */
150   public String JavaDoc toString(String JavaDoc field) {
151     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
152     if (!term.field().equals(field)) {
153       buffer.append(term.field());
154       buffer.append(":");
155     }
156     buffer.append(term.text());
157     buffer.append(ToStringUtils.boost(getBoost()));
158     return buffer.toString();
159   }
160
161   /** Returns true iff <code>o</code> is equal to this. */
162   public boolean equals(Object JavaDoc o) {
163     if (!(o instanceof TermQuery))
164       return false;
165     TermQuery other = (TermQuery)o;
166     return (this.getBoost() == other.getBoost())
167       && this.term.equals(other.term);
168   }
169
170   /** Returns a hash code value for this object.*/
171   public int hashCode() {
172     return Float.floatToIntBits(getBoost()) ^ term.hashCode();
173   }
174
175 }
176
Popular Tags