Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / TermQuery.cs
blob00bd06f6384cabe92b5c3804206cdc9e68aea1a9
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 using TermDocs = Lucene.Net.Index.TermDocs;
20 namespace Lucene.Net.Search
23 /// <summary>A Query that matches documents containing a term.
24 /// This may be combined with other terms with a {@link BooleanQuery}.
25 /// </summary>
26 [Serializable]
27 public class TermQuery:Query
29 private Term term;
31 [Serializable]
32 private class TermWeight : Weight
34 private void InitBlock(TermQuery enclosingInstance)
36 this.enclosingInstance = enclosingInstance;
38 private TermQuery enclosingInstance;
39 virtual public Query Query
41 get
43 return Enclosing_Instance;
47 virtual public float Value
49 get
51 return value_Renamed;
55 public TermQuery Enclosing_Instance
57 get
59 return enclosingInstance;
63 private Searcher searcher;
64 private float value_Renamed;
65 private float idf;
66 private float queryNorm;
67 private float queryWeight;
69 public TermWeight(TermQuery enclosingInstance, Searcher searcher)
71 InitBlock(enclosingInstance);
72 this.searcher = searcher;
75 public override System.String ToString()
77 return "weight(" + Enclosing_Instance + ")";
80 public virtual float SumOfSquaredWeights()
82 idf = Enclosing_Instance.GetSimilarity(searcher).Idf(Enclosing_Instance.term, searcher); // compute idf
83 queryWeight = idf * Enclosing_Instance.GetBoost(); // compute query weight
84 return queryWeight * queryWeight; // square it
87 public virtual void Normalize(float queryNorm)
89 this.queryNorm = queryNorm;
90 queryWeight *= queryNorm; // normalize query weight
91 value_Renamed = queryWeight * idf; // idf for document
94 public virtual Scorer Scorer(IndexReader reader)
96 TermDocs termDocs = reader.TermDocs(Enclosing_Instance.term);
98 if (termDocs == null)
99 return null;
101 return new TermScorer(this, termDocs, Enclosing_Instance.GetSimilarity(searcher), reader.Norms(Enclosing_Instance.term.Field()));
104 public virtual Explanation Explain(IndexReader reader, int doc)
107 Explanation result = new Explanation();
108 result.SetDescription("weight(" + Query + " in " + doc + "), product of:");
110 Explanation idfExpl = new Explanation(idf, "idf(docFreq=" + searcher.DocFreq(Enclosing_Instance.term) + ")");
112 // explain query weight
113 Explanation queryExpl = new Explanation();
114 queryExpl.SetDescription("queryWeight(" + Query + "), product of:");
116 Explanation boostExpl = new Explanation(Enclosing_Instance.GetBoost(), "boost");
117 if (Enclosing_Instance.GetBoost() != 1.0f)
118 queryExpl.AddDetail(boostExpl);
119 queryExpl.AddDetail(idfExpl);
121 Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
122 queryExpl.AddDetail(queryNormExpl);
124 queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());
126 result.AddDetail(queryExpl);
128 // explain Field weight
129 System.String field = Enclosing_Instance.term.Field();
130 Explanation fieldExpl = new Explanation();
131 fieldExpl.SetDescription("fieldWeight(" + Enclosing_Instance.term + " in " + doc + "), product of:");
133 Explanation tfExpl = Scorer(reader).Explain(doc);
134 fieldExpl.AddDetail(tfExpl);
135 fieldExpl.AddDetail(idfExpl);
137 Explanation fieldNormExpl = new Explanation();
138 byte[] fieldNorms = reader.Norms(field);
139 float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):0.0f;
140 fieldNormExpl.SetValue(fieldNorm);
141 fieldNormExpl.SetDescription("fieldNorm(Field=" + field + ", doc=" + doc + ")");
142 fieldExpl.AddDetail(fieldNormExpl);
144 fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());
146 result.AddDetail(fieldExpl);
148 // combine them
149 result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
151 if (queryExpl.GetValue() == 1.0f)
152 return fieldExpl;
154 return result;
158 /// <summary>Constructs a query for the term <code>t</code>. </summary>
159 public TermQuery(Term t)
161 term = t;
164 /// <summary>Returns the term of this query. </summary>
165 public virtual Term GetTerm()
167 return term;
170 protected internal override Weight CreateWeight(Searcher searcher)
172 return new TermWeight(this, searcher);
175 /// <summary>Prints a user-readable version of this query. </summary>
176 public override System.String ToString(System.String field)
178 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
179 if (!term.Field().Equals(field))
181 buffer.Append(term.Field());
182 buffer.Append(":");
184 buffer.Append(term.Text());
185 if (GetBoost() != 1.0f)
187 System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
188 nfi.NumberDecimalDigits = 1;
190 buffer.Append("^");
191 buffer.Append(GetBoost().ToString("N", nfi));
193 //buffer.Append("^");
194 //buffer.Append(GetBoost().ToString());
196 return buffer.ToString();
199 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
200 public override bool Equals(System.Object o)
202 if (!(o is TermQuery))
203 return false;
204 TermQuery other = (TermQuery) o;
205 return (this.GetBoost() == other.GetBoost()) && this.term.Equals(other.term);
208 /// <summary>Returns a hash code value for this object.</summary>
209 public override int GetHashCode()
211 return BitConverter.ToInt32(BitConverter.GetBytes(GetBoost()), 0) ^ term.GetHashCode();
213 override public System.Object Clone()
215 return new TermQuery (this.term);