Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TermQuery.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using Term = Lucene.Net.Index.Term;
22 using TermDocs = Lucene.Net.Index.TermDocs;
23 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
24 using IDFExplanation = Lucene.Net.Search.Explanation.IDFExplanation;
25 
26 namespace Lucene.Net.Search
27 {
28 
29  /// <summary>A Query that matches documents containing a term.
30  /// This may be combined with other terms with a <see cref="BooleanQuery" />.
31  /// </summary>
32  [Serializable]
33  public class TermQuery:Query
34  {
35  private Term term;
36 
37  [Serializable]
38  private class TermWeight:Weight
39  {
40  private void InitBlock(TermQuery enclosingInstance)
41  {
42  this.enclosingInstance = enclosingInstance;
43  }
44  private TermQuery enclosingInstance;
45  public TermQuery Enclosing_Instance
46  {
47  get
48  {
49  return enclosingInstance;
50  }
51 
52  }
53  private Similarity similarity;
54  private float value_Renamed;
55  private float idf;
56  private float queryNorm;
57  private float queryWeight;
58  private IDFExplanation idfExp;
59 
60  public TermWeight(TermQuery enclosingInstance, Searcher searcher)
61  {
62  InitBlock(enclosingInstance);
63  this.similarity = Enclosing_Instance.GetSimilarity(searcher);
64  idfExp = similarity.IdfExplain(Enclosing_Instance.term, searcher);
65  idf = idfExp.Idf;
66  }
67 
68  public override System.String ToString()
69  {
70  return "weight(" + Enclosing_Instance + ")";
71  }
72 
73  public override Query Query
74  {
75  get { return Enclosing_Instance; }
76  }
77 
78  public override float Value
79  {
80  get { return value_Renamed; }
81  }
82 
83  public override float GetSumOfSquaredWeights()
84  {
85  queryWeight = idf*Enclosing_Instance.Boost; // compute query weight
86  return queryWeight*queryWeight; // square it
87  }
88 
89  public override void Normalize(float queryNorm)
90  {
91  this.queryNorm = queryNorm;
92  queryWeight *= queryNorm; // normalize query weight
93  value_Renamed = queryWeight * idf; // idf for document
94  }
95 
96  public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
97  {
98  TermDocs termDocs = reader.TermDocs(Enclosing_Instance.term);
99 
100  if (termDocs == null)
101  return null;
102 
103  return new TermScorer(this, termDocs, similarity, reader.Norms(Enclosing_Instance.term.Field));
104  }
105 
106  public override Explanation Explain(IndexReader reader, int doc)
107  {
108 
109  ComplexExplanation result = new ComplexExplanation();
110  result.Description = "weight(" + Query + " in " + doc + "), product of:";
111 
112  Explanation expl = new Explanation(idf, idfExp.Explain());
113 
114  // explain query weight
115  Explanation queryExpl = new Explanation();
116  queryExpl.Description = "queryWeight(" + Query + "), product of:";
117 
118  Explanation boostExpl = new Explanation(Enclosing_Instance.Boost, "boost");
119  if (Enclosing_Instance.Boost != 1.0f)
120  queryExpl.AddDetail(boostExpl);
121  queryExpl.AddDetail(expl);
122 
123  Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
124  queryExpl.AddDetail(queryNormExpl);
125 
126  queryExpl.Value = boostExpl.Value * expl.Value * queryNormExpl.Value;
127 
128  result.AddDetail(queryExpl);
129 
130  // explain field weight
131  System.String field = Enclosing_Instance.term.Field;
132  ComplexExplanation fieldExpl = new ComplexExplanation();
133  fieldExpl.Description = "fieldWeight(" + Enclosing_Instance.term + " in " + doc + "), product of:";
134 
135  Explanation tfExplanation = new Explanation();
136  int tf = 0;
137  TermDocs termDocs = reader.TermDocs(enclosingInstance.term);
138  if (termDocs != null)
139  {
140  try
141  {
142  if (termDocs.SkipTo(doc) && termDocs.Doc == doc)
143  {
144  tf = termDocs.Freq;
145  }
146  }
147  finally
148  {
149  termDocs.Close();
150  }
151  tfExplanation.Value = similarity.Tf(tf);
152  tfExplanation.Description = "tf(termFreq(" + enclosingInstance.term + ")=" + tf + ")";
153  }
154  else
155  {
156  tfExplanation.Value = 0.0f;
157  tfExplanation.Description = "no matching term";
158  }
159  fieldExpl.AddDetail(tfExplanation);
160  fieldExpl.AddDetail(expl);
161 
162  Explanation fieldNormExpl = new Explanation();
163  byte[] fieldNorms = reader.Norms(field);
164  float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):1.0f;
165  fieldNormExpl.Value = fieldNorm;
166  fieldNormExpl.Description = "fieldNorm(field=" + field + ", doc=" + doc + ")";
167  fieldExpl.AddDetail(fieldNormExpl);
168 
169  fieldExpl.Match = tfExplanation.IsMatch;
170  fieldExpl.Value = tfExplanation.Value * expl.Value * fieldNormExpl.Value;
171 
172  result.AddDetail(fieldExpl);
173  System.Boolean? tempAux = fieldExpl.Match;
174  result.Match = tempAux;
175 
176  // combine them
177  result.Value = queryExpl.Value * fieldExpl.Value;
178 
179  if (queryExpl.Value == 1.0f)
180  return fieldExpl;
181 
182  return result;
183  }
184  }
185 
186  /// <summary>Constructs a query for the term <c>t</c>. </summary>
187  public TermQuery(Term t)
188  {
189  term = t;
190  }
191 
192  /// <summary>Returns the term of this query. </summary>
193  public virtual Term Term
194  {
195  get { return term; }
196  }
197 
198  public override Weight CreateWeight(Searcher searcher)
199  {
200  return new TermWeight(this, searcher);
201  }
202 
203  public override void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
204  {
205  terms.Add(Term);
206  }
207 
208  /// <summary>Prints a user-readable version of this query. </summary>
209  public override System.String ToString(System.String field)
210  {
211  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
212  if (!term.Field.Equals(field))
213  {
214  buffer.Append(term.Field);
215  buffer.Append(":");
216  }
217  buffer.Append(term.Text);
218  buffer.Append(ToStringUtils.Boost(Boost));
219  return buffer.ToString();
220  }
221 
222  /// <summary>Returns true iff <c>o</c> is equal to this. </summary>
223  public override bool Equals(System.Object o)
224  {
225  if (!(o is TermQuery))
226  return false;
227  TermQuery other = (TermQuery) o;
228  return (this.Boost == other.Boost) && this.term.Equals(other.term);
229  }
230 
231  /// <summary>Returns a hash code value for this object.</summary>
232  public override int GetHashCode()
233  {
234  return BitConverter.ToInt32(BitConverter.GetBytes(Boost), 0) ^ term.GetHashCode();
235  }
236  }
237 }