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.
18 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
19 using Term
= Lucene
.Net
.Index
.Term
;
20 using TermDocs
= Lucene
.Net
.Index
.TermDocs
;
21 using ToStringUtils
= Lucene
.Net
.Util
.ToStringUtils
;
23 namespace Lucene
.Net
.Search
26 /// <summary>A Query that matches documents containing a term.
27 /// This may be combined with other terms with a {@link BooleanQuery}.
30 public class TermQuery
: Query
35 private class TermWeight
: Weight
37 private void InitBlock(TermQuery enclosingInstance
)
39 this.enclosingInstance
= enclosingInstance
;
41 private TermQuery enclosingInstance
;
42 public TermQuery Enclosing_Instance
46 return enclosingInstance
;
50 private Similarity similarity
;
51 private float value_Renamed
;
53 private float queryNorm
;
54 private float queryWeight
;
56 public TermWeight(TermQuery enclosingInstance
, Searcher searcher
)
58 InitBlock(enclosingInstance
);
59 this.similarity
= Enclosing_Instance
.GetSimilarity(searcher
);
60 idf
= similarity
.Idf(Enclosing_Instance
.term
, searcher
); // compute idf
63 public override System
.String
ToString()
65 return "weight(" + Enclosing_Instance
+ ")";
68 public virtual Query
GetQuery()
70 return Enclosing_Instance
;
72 public virtual float GetValue()
77 public virtual float SumOfSquaredWeights()
79 queryWeight
= idf
* Enclosing_Instance
.GetBoost(); // compute query weight
80 return queryWeight
* queryWeight
; // square it
83 public virtual void Normalize(float queryNorm
)
85 this.queryNorm
= queryNorm
;
86 queryWeight
*= queryNorm
; // normalize query weight
87 value_Renamed
= queryWeight
* idf
; // idf for document
90 public virtual Scorer
Scorer(IndexReader reader
)
92 TermDocs termDocs
= reader
.TermDocs(Enclosing_Instance
.term
);
97 return new TermScorer(this, termDocs
, similarity
, reader
.Norms(Enclosing_Instance
.term
.Field()));
100 public virtual Explanation
Explain(IndexReader reader
, int doc
)
103 Explanation result
= new Explanation();
104 result
.SetDescription("weight(" + GetQuery() + " in " + doc
+ "), product of:");
106 Explanation idfExpl
= new Explanation(idf
, "idf(docFreq=" + reader
.DocFreq(Enclosing_Instance
.term
) + ")");
108 // explain query weight
109 Explanation queryExpl
= new Explanation();
110 queryExpl
.SetDescription("queryWeight(" + GetQuery() + "), product of:");
112 Explanation boostExpl
= new Explanation(Enclosing_Instance
.GetBoost(), "boost");
113 if (Enclosing_Instance
.GetBoost() != 1.0f
)
114 queryExpl
.AddDetail(boostExpl
);
115 queryExpl
.AddDetail(idfExpl
);
117 Explanation queryNormExpl
= new Explanation(queryNorm
, "queryNorm");
118 queryExpl
.AddDetail(queryNormExpl
);
120 queryExpl
.SetValue(boostExpl
.GetValue() * idfExpl
.GetValue() * queryNormExpl
.GetValue());
122 result
.AddDetail(queryExpl
);
124 // explain field weight
125 System
.String field
= Enclosing_Instance
.term
.Field();
126 Explanation fieldExpl
= new Explanation();
127 fieldExpl
.SetDescription("fieldWeight(" + Enclosing_Instance
.term
+ " in " + doc
+ "), product of:");
129 Explanation tfExpl
= Scorer(reader
).Explain(doc
);
130 fieldExpl
.AddDetail(tfExpl
);
131 fieldExpl
.AddDetail(idfExpl
);
133 Explanation fieldNormExpl
= new Explanation();
134 byte[] fieldNorms
= reader
.Norms(field
);
135 float fieldNorm
= fieldNorms
!= null ? Similarity
.DecodeNorm(fieldNorms
[doc
]) : 0.0f
;
136 fieldNormExpl
.SetValue(fieldNorm
);
137 fieldNormExpl
.SetDescription("fieldNorm(field=" + field
+ ", doc=" + doc
+ ")");
138 fieldExpl
.AddDetail(fieldNormExpl
);
140 fieldExpl
.SetValue(tfExpl
.GetValue() * idfExpl
.GetValue() * fieldNormExpl
.GetValue());
142 result
.AddDetail(fieldExpl
);
145 result
.SetValue(queryExpl
.GetValue() * fieldExpl
.GetValue());
147 if (queryExpl
.GetValue() == 1.0f
)
154 /// <summary>Constructs a query for the term <code>t</code>. </summary>
155 public TermQuery(Term t
)
160 /// <summary>Returns the term of this query. </summary>
161 public virtual Term
GetTerm()
166 protected internal override Weight
CreateWeight(Searcher searcher
)
168 return new TermWeight(this, searcher
);
171 public override void ExtractTerms(System
.Collections
.Hashtable terms
)
173 Term term
= GetTerm();
174 terms
.Add(term
, term
);
177 /// <summary>Prints a user-readable version of this query. </summary>
178 public override System
.String
ToString(System
.String field
)
180 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
181 if (!term
.Field().Equals(field
))
183 buffer
.Append(term
.Field());
186 buffer
.Append(term
.Text());
187 buffer
.Append(ToStringUtils
.Boost(GetBoost()));
188 return buffer
.ToString();
191 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
192 public override bool Equals(System
.Object o
)
194 if (!(o
is TermQuery
))
196 TermQuery other
= (TermQuery
) o
;
197 return (this.GetBoost() == other
.GetBoost()) && this.term
.Equals(other
.term
);
200 /// <summary>Returns a hash code value for this object.</summary>
201 public override int GetHashCode()
203 return BitConverter
.ToInt32(BitConverter
.GetBytes(GetBoost()), 0) ^ term
.GetHashCode();