TermQuery.java
001 package gate.creole.annic.apache.lucene.search;
002 
003 /**
004  * Copyright 2004 The Apache Software Foundation
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 
019 import java.io.IOException;
020 import gate.creole.annic.apache.lucene.index.Term;
021 import gate.creole.annic.apache.lucene.index.TermDocs;
022 import gate.creole.annic.apache.lucene.index.IndexReader;
023 
024 /** A Query that matches documents containing a term.
025   This may be combined with other terms with a {@link BooleanQuery}.
026   */
027 public class TermQuery extends Query {
028   private Term term;
029 
030   private class TermWeight implements Weight {
031     private Searcher searcher;
032     private float value;
033     private float idf;
034     private float queryNorm;
035     private float queryWeight;
036 
037     public TermWeight(Searcher searcher) {
038       this.searcher = searcher;
039     }
040 
041     public String toString() { return "weight(" + TermQuery.this ")"}
042 
043     public Query getQuery() { return TermQuery.this}
044     public float getValue() { return value; }
045 
046     public float sumOfSquaredWeights() throws IOException {
047       idf = getSimilarity(searcher).idf(term, searcher)// compute idf
048       queryWeight = idf * getBoost();             // compute query weight
049       return queryWeight * queryWeight;           // square it
050     }
051 
052     public void normalize(float queryNorm) {
053       this.queryNorm = queryNorm;
054       queryWeight *= queryNorm;                   // normalize query weight
055       value = queryWeight * idf;                  // idf for document
056     }
057 
058     public Scorer scorer(IndexReader reader, Searcher searcherthrows IOException {
059       this.searcher = searcher;
060       TermDocs termDocs = reader.termDocs(term);
061 
062       if (termDocs == null)
063         return null;
064 
065       return new TermScorer(this, termDocs, getSimilarity(searcher),
066                             reader.norms(term.field()), term);
067     }
068 
069     public Explanation explain(IndexReader reader, int doc)
070       throws IOException {
071 
072       Explanation result = new Explanation();
073       result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
074 
075       Explanation idfExpl =
076         new Explanation(idf, "idf(docFreq=" + searcher.docFreq(term")");
077 
078       // explain query weight
079       Explanation queryExpl = new Explanation();
080       queryExpl.setDescription("queryWeight(" + getQuery() "), product of:");
081 
082       Explanation boostExpl = new Explanation(getBoost()"boost");
083       if (getBoost() != 1.0f)
084         queryExpl.addDetail(boostExpl);
085       queryExpl.addDetail(idfExpl);
086 
087       Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
088       queryExpl.addDetail(queryNormExpl);
089 
090       queryExpl.setValue(boostExpl.getValue() *
091                          idfExpl.getValue() *
092                          queryNormExpl.getValue());
093 
094       result.addDetail(queryExpl);
095 
096       // explain field weight
097       String field = term.field();
098       Explanation fieldExpl = new Explanation();
099       fieldExpl.setDescription("fieldWeight("+term+" in "+doc+
100                                "), product of:");
101 
102       Explanation tfExpl = scorer(reader, this.searcher).explain(doc);
103       fieldExpl.addDetail(tfExpl);
104       fieldExpl.addDetail(idfExpl);
105 
106       Explanation fieldNormExpl = new Explanation();
107       byte[] fieldNorms = reader.norms(field);
108       float fieldNorm =
109         fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) 0.0f;
110       fieldNormExpl.setValue(fieldNorm);
111       fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
112       fieldExpl.addDetail(fieldNormExpl);
113 
114       fieldExpl.setValue(tfExpl.getValue() *
115                          idfExpl.getValue() *
116                          fieldNormExpl.getValue());
117 
118       result.addDetail(fieldExpl);
119 
120       // combine them
121       result.setValue(queryExpl.getValue() * fieldExpl.getValue());
122 
123       if (queryExpl.getValue() == 1.0f)
124         return fieldExpl;
125 
126       return result;
127     }
128   }
129 
130   /** Constructs a query for the term <code>t</code>. */
131   public TermQuery(Term t) {
132     term = t;
133   }
134 
135   /** Returns the term of this query. */
136   public Term getTerm() { return term; }
137 
138   protected Weight createWeight(Searcher searcher) {
139     return new TermWeight(searcher);
140   }
141 
142   /** Prints a user-readable version of this query. */
143   public String toString(String field) {
144     StringBuffer buffer = new StringBuffer();
145     if (!term.field().equals(field)) {
146       buffer.append(term.field());
147       buffer.append(":");
148     }
149     buffer.append(term.text());
150     if (getBoost() != 1.0f) {
151       buffer.append("^");
152       buffer.append(Float.toString(getBoost()));
153     }
154     return buffer.toString();
155   }
156 
157   /** Returns true iff <code>o</code> is equal to this. */
158   public boolean equals(Object o) {
159     if (!(instanceof TermQuery))
160       return false;
161     TermQuery other = (TermQuery)o;
162     try {
163       throw new Exception("My Error");
164     catch(Exception e) {
165       e.printStackTrace();
166     }
167 
168     return (this.getBoost() == other.getBoost())
169       && this.term.equals(other.term);
170   }
171 
172   /** Returns a hash code value for this object.*/
173   public int hashCode() {
174     return Float.floatToIntBits(getBoost()) ^ term.hashCode();
175   }
176 
177 }