Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / TermQuery.cs
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 = 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;
25
26 namespace Mono.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 {@link 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.GetIdf();
66                         }
67                         
68                         public override System.String ToString()
69                         {
70                                 return "weight(" + Enclosing_Instance + ")";
71                         }
72                         
73                         public override Query GetQuery()
74                         {
75                                 return Enclosing_Instance;
76                         }
77                         public override float GetValue()
78                         {
79                                 return value_Renamed;
80                         }
81                         
82                         public override float SumOfSquaredWeights()
83                         {
84                                 queryWeight = idf * Enclosing_Instance.GetBoost(); // compute query weight
85                                 return queryWeight * queryWeight; // square it
86                         }
87                         
88                         public override void  Normalize(float queryNorm)
89                         {
90                                 this.queryNorm = queryNorm;
91                                 queryWeight *= queryNorm; // normalize query weight
92                                 value_Renamed = queryWeight * idf; // idf for document
93                         }
94                         
95                         public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
96                         {
97                                 TermDocs termDocs = reader.TermDocs(Enclosing_Instance.term);
98                                 
99                                 if (termDocs == null)
100                                         return null;
101                                 
102                                 return new TermScorer(this, termDocs, similarity, reader.Norms(Enclosing_Instance.term.Field()));
103                         }
104                         
105                         public override Explanation Explain(IndexReader reader, int doc)
106                         {
107                                 
108                                 ComplexExplanation result = new ComplexExplanation();
109                                 result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
110                                 
111                                 Explanation expl = new Explanation(idf, idfExp.Explain());
112                                 
113                                 // explain query weight
114                                 Explanation queryExpl = new Explanation();
115                                 queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");
116                                 
117                                 Explanation boostExpl = new Explanation(Enclosing_Instance.GetBoost(), "boost");
118                                 if (Enclosing_Instance.GetBoost() != 1.0f)
119                                         queryExpl.AddDetail(boostExpl);
120                                 queryExpl.AddDetail(expl);
121                                 
122                                 Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
123                                 queryExpl.AddDetail(queryNormExpl);
124                                 
125                                 queryExpl.SetValue(boostExpl.GetValue() * expl.GetValue() * queryNormExpl.GetValue());
126                                 
127                                 result.AddDetail(queryExpl);
128                                 
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:");
133                                 
134                                 Explanation tfExpl = Scorer(reader, true, false).Explain(doc);
135                                 fieldExpl.AddDetail(tfExpl);
136                                 fieldExpl.AddDetail(expl);
137                                 
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);
144                                 
145                                 fieldExpl.SetMatch(tfExpl.IsMatch());
146                                 fieldExpl.SetValue(tfExpl.GetValue() * expl.GetValue() * fieldNormExpl.GetValue());
147                                 
148                                 result.AddDetail(fieldExpl);
149                                 System.Boolean? tempAux = fieldExpl.GetMatch();
150                                 result.SetMatch(tempAux);
151                                 
152                                 // combine them
153                                 result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
154                                 
155                                 if (queryExpl.GetValue() == 1.0f)
156                                         return fieldExpl;
157                                 
158                                 return result;
159                         }
160                 }
161                 
162                 /// <summary>Constructs a query for the term <code>t</code>. </summary>
163                 public TermQuery(Term t)
164                 {
165                         term = t;
166                 }
167                 
168                 /// <summary>Returns the term of this query. </summary>
169                 public virtual Term GetTerm()
170                 {
171                         return term;
172                 }
173                 
174                 public override Weight CreateWeight(Searcher searcher)
175                 {
176                         return new TermWeight(this, searcher);
177                 }
178                 
179                 public override void  ExtractTerms(System.Collections.Hashtable terms)
180                 {
181                         SupportClass.CollectionsHelper.AddIfNotContains(terms, GetTerm());
182                 }
183                 
184                 /// <summary>Prints a user-readable version of this query. </summary>
185                 public override System.String ToString(System.String field)
186                 {
187                         System.Text.StringBuilder buffer = new System.Text.StringBuilder();
188                         if (!term.Field().Equals(field))
189                         {
190                                 buffer.Append(term.Field());
191                                 buffer.Append(":");
192                         }
193                         buffer.Append(term.Text());
194                         buffer.Append(ToStringUtils.Boost(GetBoost()));
195                         return buffer.ToString();
196                 }
197                 
198                 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
199                 public  override bool Equals(System.Object o)
200                 {
201                         if (!(o is TermQuery))
202                                 return false;
203                         TermQuery other = (TermQuery) o;
204                         return (this.GetBoost() == other.GetBoost()) && this.term.Equals(other.term);
205                 }
206                 
207                 /// <summary>Returns a hash code value for this object.</summary>
208                 public override int GetHashCode()
209                 {
210                         return BitConverter.ToInt32(BitConverter.GetBytes(GetBoost()), 0) ^ term.GetHashCode();
211         }
212         }
213 }