2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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}.
27 public class TermQuery
: Query
32 private class TermWeight
: Weight
34 private void InitBlock(TermQuery enclosingInstance
)
36 this.enclosingInstance
= enclosingInstance
;
38 private TermQuery enclosingInstance
;
39 virtual public Query
GetQuery()
41 return Enclosing_Instance
;
43 public TermQuery Enclosing_Instance
47 return enclosingInstance
;
51 private Similarity similarity
;
52 private Searcher searcher
;
53 private float value_Renamed
;
55 private float queryNorm
;
56 private float queryWeight
;
58 public TermWeight(TermQuery enclosingInstance
, Searcher searcher
)
60 InitBlock(enclosingInstance
);
61 this.similarity
= Enclosing_Instance
.GetSimilarity(searcher
);
62 idf
= similarity
.Idf(Enclosing_Instance
.term
, searcher
); // compute idf
65 public override System
.String
ToString()
67 return "weight(" + Enclosing_Instance
+ ")";
69 public virtual float GetValue()
74 public virtual float SumOfSquaredWeights()
76 queryWeight
= idf
* Enclosing_Instance
.GetBoost(); // compute query weight
77 return queryWeight
* queryWeight
; // square it
80 public virtual void Normalize(float queryNorm
)
82 this.queryNorm
= queryNorm
;
83 queryWeight
*= queryNorm
; // normalize query weight
84 value_Renamed
= queryWeight
* idf
; // idf for document
87 public virtual Scorer
Scorer(IndexReader reader
)
89 TermDocs termDocs
= reader
.TermDocs(Enclosing_Instance
.term
);
94 return new TermScorer(this, termDocs
, similarity
, reader
.Norms(Enclosing_Instance
.term
.Field()));
97 public virtual Explanation
Explain(IndexReader reader
, int doc
)
100 Explanation result
= new Explanation();
101 result
.SetDescription("weight(" + GetQuery() + " in " + doc
+ "), product of:");
103 Explanation idfExpl
= new Explanation(idf
, "idf(docFreq=" + reader
.DocFreq(Enclosing_Instance
.term
) + ")");
105 // explain query weight
106 Explanation queryExpl
= new Explanation();
107 queryExpl
.SetDescription("queryWeight(" + GetQuery() + "), product of:");
109 Explanation boostExpl
= new Explanation(Enclosing_Instance
.GetBoost(), "boost");
110 if (Enclosing_Instance
.GetBoost() != 1.0f
)
111 queryExpl
.AddDetail(boostExpl
);
112 queryExpl
.AddDetail(idfExpl
);
114 Explanation queryNormExpl
= new Explanation(queryNorm
, "queryNorm");
115 queryExpl
.AddDetail(queryNormExpl
);
117 queryExpl
.SetValue(boostExpl
.GetValue() * idfExpl
.GetValue() * queryNormExpl
.GetValue());
119 result
.AddDetail(queryExpl
);
121 // explain Field weight
122 System
.String field
= Enclosing_Instance
.term
.Field();
123 Explanation fieldExpl
= new Explanation();
124 fieldExpl
.SetDescription("fieldWeight(" + Enclosing_Instance
.term
+ " in " + doc
+ "), product of:");
126 Explanation tfExpl
= Scorer(reader
).Explain(doc
);
127 fieldExpl
.AddDetail(tfExpl
);
128 fieldExpl
.AddDetail(idfExpl
);
130 Explanation fieldNormExpl
= new Explanation();
131 byte[] fieldNorms
= reader
.Norms(field
);
132 float fieldNorm
= fieldNorms
!= null?Similarity
.DecodeNorm(fieldNorms
[doc
]):0.0f
;
133 fieldNormExpl
.SetValue(fieldNorm
);
134 fieldNormExpl
.SetDescription("fieldNorm(Field=" + field
+ ", doc=" + doc
+ ")");
135 fieldExpl
.AddDetail(fieldNormExpl
);
137 fieldExpl
.SetValue(tfExpl
.GetValue() * idfExpl
.GetValue() * fieldNormExpl
.GetValue());
139 result
.AddDetail(fieldExpl
);
142 result
.SetValue(queryExpl
.GetValue() * fieldExpl
.GetValue());
144 if (queryExpl
.GetValue() == 1.0f
)
151 /// <summary>Constructs a query for the term <code>t</code>. </summary>
152 public TermQuery(Term t
)
157 /// <summary>Returns the term of this query. </summary>
158 public virtual Term
GetTerm()
163 protected internal override Weight
CreateWeight(Searcher searcher
)
165 return new TermWeight(this, searcher
);
168 public override void ExtractTerms(System
.Collections
.Hashtable terms
)
170 Term term
= GetTerm();
171 terms
.Add(term
, term
);
174 /// <summary>Prints a user-readable version of this query. </summary>
175 public override System
.String
ToString(System
.String field
)
177 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
178 if (!term
.Field().Equals(field
))
180 buffer
.Append(term
.Field());
183 buffer
.Append(term
.Text());
184 if (GetBoost() != 1.0f
)
186 System
.Globalization
.NumberFormatInfo nfi
= new System
.Globalization
.CultureInfo("en-US", false).NumberFormat
;
187 nfi
.NumberDecimalDigits
= 1;
190 buffer
.Append(GetBoost().ToString("N", nfi
));
192 //buffer.Append("^");
193 //buffer.Append(GetBoost().ToString());
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();