2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using Term = Mono.Lucene.Net.Index.Term;
22 using TermDocs = Mono.Lucene.Net.Index.TermDocs;
23 using ToStringUtils = Mono.Lucene.Net.Util.ToStringUtils;
24 using IDFExplanation = Mono.Lucene.Net.Search.Explanation.IDFExplanation;
26 namespace Mono.Lucene.Net.Search
29 /// <summary>A Query that matches documents containing a term.
30 /// This may be combined with other terms with a {@link BooleanQuery}.
33 public class TermQuery:Query
38 private class TermWeight:Weight
40 private void InitBlock(TermQuery enclosingInstance)
42 this.enclosingInstance = enclosingInstance;
44 private TermQuery enclosingInstance;
45 public TermQuery Enclosing_Instance
49 return enclosingInstance;
53 private Similarity similarity;
54 private float value_Renamed;
56 private float queryNorm;
57 private float queryWeight;
58 private IDFExplanation idfExp;
60 public TermWeight(TermQuery enclosingInstance, Searcher searcher)
62 InitBlock(enclosingInstance);
63 this.similarity = Enclosing_Instance.GetSimilarity(searcher);
64 idfExp = similarity.IdfExplain(Enclosing_Instance.term, searcher);
65 idf = idfExp.GetIdf();
68 public override System.String ToString()
70 return "weight(" + Enclosing_Instance + ")";
73 public override Query GetQuery()
75 return Enclosing_Instance;
77 public override float GetValue()
82 public override float SumOfSquaredWeights()
84 queryWeight = idf * Enclosing_Instance.GetBoost(); // compute query weight
85 return queryWeight * queryWeight; // square it
88 public override void Normalize(float queryNorm)
90 this.queryNorm = queryNorm;
91 queryWeight *= queryNorm; // normalize query weight
92 value_Renamed = queryWeight * idf; // idf for document
95 public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
97 TermDocs termDocs = reader.TermDocs(Enclosing_Instance.term);
102 return new TermScorer(this, termDocs, similarity, reader.Norms(Enclosing_Instance.term.Field()));
105 public override Explanation Explain(IndexReader reader, int doc)
108 ComplexExplanation result = new ComplexExplanation();
109 result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
111 Explanation expl = new Explanation(idf, idfExp.Explain());
113 // explain query weight
114 Explanation queryExpl = new Explanation();
115 queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");
117 Explanation boostExpl = new Explanation(Enclosing_Instance.GetBoost(), "boost");
118 if (Enclosing_Instance.GetBoost() != 1.0f)
119 queryExpl.AddDetail(boostExpl);
120 queryExpl.AddDetail(expl);
122 Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
123 queryExpl.AddDetail(queryNormExpl);
125 queryExpl.SetValue(boostExpl.GetValue() * expl.GetValue() * queryNormExpl.GetValue());
127 result.AddDetail(queryExpl);
129 // explain field weight
130 System.String field = Enclosing_Instance.term.Field();
131 ComplexExplanation fieldExpl = new ComplexExplanation();
132 fieldExpl.SetDescription("fieldWeight(" + Enclosing_Instance.term + " in " + doc + "), product of:");
134 Explanation tfExpl = Scorer(reader, true, false).Explain(doc);
135 fieldExpl.AddDetail(tfExpl);
136 fieldExpl.AddDetail(expl);
138 Explanation fieldNormExpl = new Explanation();
139 byte[] fieldNorms = reader.Norms(field);
140 float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):1.0f;
141 fieldNormExpl.SetValue(fieldNorm);
142 fieldNormExpl.SetDescription("fieldNorm(field=" + field + ", doc=" + doc + ")");
143 fieldExpl.AddDetail(fieldNormExpl);
145 fieldExpl.SetMatch(tfExpl.IsMatch());
146 fieldExpl.SetValue(tfExpl.GetValue() * expl.GetValue() * fieldNormExpl.GetValue());
148 result.AddDetail(fieldExpl);
149 System.Boolean? tempAux = fieldExpl.GetMatch();
150 result.SetMatch(tempAux);
153 result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
155 if (queryExpl.GetValue() == 1.0f)
162 /// <summary>Constructs a query for the term <code>t</code>. </summary>
163 public TermQuery(Term t)
168 /// <summary>Returns the term of this query. </summary>
169 public virtual Term GetTerm()
174 public override Weight CreateWeight(Searcher searcher)
176 return new TermWeight(this, searcher);
179 public override void ExtractTerms(System.Collections.Hashtable terms)
181 SupportClass.CollectionsHelper.AddIfNotContains(terms, GetTerm());
184 /// <summary>Prints a user-readable version of this query. </summary>
185 public override System.String ToString(System.String field)
187 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
188 if (!term.Field().Equals(field))
190 buffer.Append(term.Field());
193 buffer.Append(term.Text());
194 buffer.Append(ToStringUtils.Boost(GetBoost()));
195 return buffer.ToString();
198 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
199 public override bool Equals(System.Object o)
201 if (!(o is TermQuery))
203 TermQuery other = (TermQuery) o;
204 return (this.GetBoost() == other.GetBoost()) && this.term.Equals(other.term);
207 /// <summary>Returns a hash code value for this object.</summary>
208 public override int GetHashCode()
210 return BitConverter.ToInt32(BitConverter.GetBytes(GetBoost()), 0) ^ term.GetHashCode();