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 import java.io.IOException;
21 import java.util.HashSet;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.index.TermDocs;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.search.Explanation.IDFExplanation;
28 import org.apache.lucene.util.ReaderUtil;
29 import org.apache.lucene.util.ToStringUtils;
31 /** A Query that matches documents containing a term.
32 This may be combined with other terms with a {@link BooleanQuery}.
34 public class TermQuery extends Query {
37 private class TermWeight extends Weight {
38 private final Similarity similarity;
41 private float queryNorm;
42 private float queryWeight;
43 private IDFExplanation idfExp;
44 private final Set<Integer> hash;
46 public TermWeight(Searcher searcher)
48 this.similarity = getSimilarity(searcher);
49 if (searcher instanceof IndexSearcher) {
50 hash = new HashSet<Integer>();
51 IndexReader ir = ((IndexSearcher)searcher).getIndexReader();
52 final int dfSum[] = new int[1];
53 new ReaderUtil.Gather(ir) {
55 protected void add(int base, IndexReader r) throws IOException {
56 int df = r.docFreq(term);
59 hash.add(r.hashCode());
64 idfExp = similarity.idfExplain(term, searcher, dfSum[0]);
66 idfExp = similarity.idfExplain(term, searcher);
70 idf = idfExp.getIdf();
74 public String toString() { return "weight(" + TermQuery.this + ")"; }
77 public Query getQuery() { return TermQuery.this; }
80 public float getValue() { return value; }
83 public float sumOfSquaredWeights() {
84 queryWeight = idf * getBoost(); // compute query weight
85 return queryWeight * queryWeight; // square it
89 public void normalize(float queryNorm) {
90 this.queryNorm = queryNorm;
91 queryWeight *= queryNorm; // normalize query weight
92 value = queryWeight * idf; // idf for document
96 public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
97 if (hash != null && !hash.contains(reader.hashCode())) {
101 TermDocs termDocs = reader.termDocs(term);
103 if (termDocs == null)
106 return new TermScorer(this, termDocs, similarity, reader.norms(term.field()));
110 public Explanation explain(IndexReader reader, int doc)
113 ComplexExplanation result = new ComplexExplanation();
114 result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
116 Explanation expl = new Explanation(idf, idfExp.explain());
118 // explain query weight
119 Explanation queryExpl = new Explanation();
120 queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
122 Explanation boostExpl = new Explanation(getBoost(), "boost");
123 if (getBoost() != 1.0f)
124 queryExpl.addDetail(boostExpl);
125 queryExpl.addDetail(expl);
127 Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
128 queryExpl.addDetail(queryNormExpl);
130 queryExpl.setValue(boostExpl.getValue() *
132 queryNormExpl.getValue());
134 result.addDetail(queryExpl);
136 // explain field weight
137 String field = term.field();
138 ComplexExplanation fieldExpl = new ComplexExplanation();
139 fieldExpl.setDescription("fieldWeight("+term+" in "+doc+
142 Explanation tfExplanation = new Explanation();
144 TermDocs termDocs = reader.termDocs(term);
145 if (termDocs != null) {
147 if (termDocs.skipTo(doc) && termDocs.doc() == doc) {
148 tf = termDocs.freq();
153 tfExplanation.setValue(similarity.tf(tf));
154 tfExplanation.setDescription("tf(termFreq("+term+")="+tf+")");
156 tfExplanation.setValue(0.0f);
157 tfExplanation.setDescription("no matching term");
159 fieldExpl.addDetail(tfExplanation);
160 fieldExpl.addDetail(expl);
162 Explanation fieldNormExpl = new Explanation();
163 byte[] fieldNorms = reader.norms(field);
165 fieldNorms!=null ? similarity.decodeNormValue(fieldNorms[doc]) : 1.0f;
166 fieldNormExpl.setValue(fieldNorm);
167 fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
168 fieldExpl.addDetail(fieldNormExpl);
170 fieldExpl.setMatch(Boolean.valueOf(tfExplanation.isMatch()));
171 fieldExpl.setValue(tfExplanation.getValue() *
173 fieldNormExpl.getValue());
175 result.addDetail(fieldExpl);
176 result.setMatch(fieldExpl.getMatch());
179 result.setValue(queryExpl.getValue() * fieldExpl.getValue());
181 if (queryExpl.getValue() == 1.0f)
188 /** Constructs a query for the term <code>t</code>. */
189 public TermQuery(Term t) {
193 /** Returns the term of this query. */
194 public Term getTerm() { return term; }
197 public Weight createWeight(Searcher searcher) throws IOException {
198 return new TermWeight(searcher);
202 public void extractTerms(Set<Term> terms) {
203 terms.add(getTerm());
206 /** Prints a user-readable version of this query. */
208 public String toString(String field) {
209 StringBuilder buffer = new StringBuilder();
210 if (!term.field().equals(field)) {
211 buffer.append(term.field());
214 buffer.append(term.text());
215 buffer.append(ToStringUtils.boost(getBoost()));
216 return buffer.toString();
219 /** Returns true iff <code>o</code> is equal to this. */
221 public boolean equals(Object o) {
222 if (!(o instanceof TermQuery))
224 TermQuery other = (TermQuery)o;
225 return (this.getBoost() == other.getBoost())
226 && this.term.equals(other.term);
229 /** Returns a hash code value for this object.*/
231 public int hashCode() {
232 return Float.floatToIntBits(getBoost()) ^ term.hashCode();