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
ConstantScoreQuery.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 using Lucene.Net.Index;
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 
22 namespace Lucene.Net.Search
23 {
24 
25  /// <summary> A query that wraps a filter and simply returns a constant score equal to the
26  /// query boost for every document in the filter.
27  /// </summary>
28  [Serializable]
30  {
31  protected internal Filter internalFilter;
32 
33  public ConstantScoreQuery(Filter filter)
34  {
35  this.internalFilter = filter;
36  }
37 
38  /// <summary>Returns the encapsulated filter </summary>
39  public virtual Filter Filter
40  {
41  get { return internalFilter; }
42  }
43 
44  public override Query Rewrite(IndexReader reader)
45  {
46  return this;
47  }
48 
49  public override void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
50  {
51  // OK to not add any terms when used for MultiSearcher,
52  // but may not be OK for highlighting
53  }
54 
55  [Serializable]
56  protected internal class ConstantWeight:Weight
57  {
58  private void InitBlock(ConstantScoreQuery enclosingInstance)
59  {
60  this.enclosingInstance = enclosingInstance;
61  }
62  private ConstantScoreQuery enclosingInstance;
63  public ConstantScoreQuery Enclosing_Instance
64  {
65  get
66  {
67  return enclosingInstance;
68  }
69 
70  }
71  private readonly Similarity similarity;
72  private float queryNorm;
73  private float queryWeight;
74 
75  public ConstantWeight(ConstantScoreQuery enclosingInstance, Searcher searcher)
76  {
77  InitBlock(enclosingInstance);
78  this.similarity = Enclosing_Instance.GetSimilarity(searcher);
79  }
80 
81  public override Query Query
82  {
83  get { return Enclosing_Instance; }
84  }
85 
86  public override float Value
87  {
88  get { return queryWeight; }
89  }
90 
91  public override float GetSumOfSquaredWeights()
92  {
93  queryWeight = Enclosing_Instance.Boost;
94  return queryWeight*queryWeight;
95  }
96 
97  public override void Normalize(float norm)
98  {
99  this.queryNorm = norm;
100  queryWeight *= this.queryNorm;
101  }
102 
103  public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
104  {
105  return new ConstantScorer(enclosingInstance, similarity, reader, this);
106  }
107 
108  public override Explanation Explain(IndexReader reader, int doc)
109  {
110 
111  var cs = new ConstantScorer(enclosingInstance, similarity, reader, this);
112  bool exists = cs.docIdSetIterator.Advance(doc) == doc;
113 
114  var result = new ComplexExplanation();
115 
116  if (exists)
117  {
118  result.Description = "ConstantScoreQuery(" + Enclosing_Instance.internalFilter + "), product of:";
119  result.Value = queryWeight;
120  System.Boolean tempAux = true;
121  result.Match = tempAux;
122  result.AddDetail(new Explanation(Enclosing_Instance.Boost, "boost"));
123  result.AddDetail(new Explanation(queryNorm, "queryNorm"));
124  }
125  else
126  {
127  result.Description = "ConstantScoreQuery(" + Enclosing_Instance.internalFilter + ") doesn't match id " + doc;
128  result.Value = 0;
129  System.Boolean tempAux2 = false;
130  result.Match = tempAux2;
131  }
132  return result;
133  }
134  }
135 
136  protected internal class ConstantScorer : Scorer
137  {
138  private void InitBlock(ConstantScoreQuery enclosingInstance)
139  {
140  this.enclosingInstance = enclosingInstance;
141  }
142  private ConstantScoreQuery enclosingInstance;
143  public ConstantScoreQuery Enclosing_Instance
144  {
145  get
146  {
147  return enclosingInstance;
148  }
149 
150  }
151  internal DocIdSetIterator docIdSetIterator;
152  internal float theScore;
153  internal int doc = - 1;
154 
155  public ConstantScorer(ConstantScoreQuery enclosingInstance, Similarity similarity, IndexReader reader, Weight w):base(similarity)
156  {
157  InitBlock(enclosingInstance);
158  theScore = w.Value;
159  DocIdSet docIdSet = Enclosing_Instance.internalFilter.GetDocIdSet(reader);
160  if (docIdSet == null)
161  {
162  docIdSetIterator = DocIdSet.EMPTY_DOCIDSET.Iterator();
163  }
164  else
165  {
166  DocIdSetIterator iter = docIdSet.Iterator();
167  if (iter == null)
168  {
169  docIdSetIterator = DocIdSet.EMPTY_DOCIDSET.Iterator();
170  }
171  else
172  {
173  docIdSetIterator = iter;
174  }
175  }
176  }
177 
178  public override int NextDoc()
179  {
180  return docIdSetIterator.NextDoc();
181  }
182 
183  public override int DocID()
184  {
185  return docIdSetIterator.DocID();
186  }
187 
188  public override float Score()
189  {
190  return theScore;
191  }
192 
193  public override int Advance(int target)
194  {
195  return docIdSetIterator.Advance(target);
196  }
197  }
198 
199  public override Weight CreateWeight(Searcher searcher)
200  {
201  return new ConstantScoreQuery.ConstantWeight(this, searcher);
202  }
203 
204  /// <summary>Prints a user-readable version of this query. </summary>
205  public override System.String ToString(string field)
206  {
207  return "ConstantScore(" + internalFilter + (Boost == 1.0?")":"^" + Boost);
208  }
209 
210  /// <summary>Returns true if <c>o</c> is equal to this. </summary>
211  public override bool Equals(System.Object o)
212  {
213  if (this == o)
214  return true;
215  if (!(o is ConstantScoreQuery))
216  return false;
217  ConstantScoreQuery other = (ConstantScoreQuery) o;
218  return this.Boost == other.Boost && internalFilter.Equals(other.internalFilter);
219  }
220 
221  /// <summary>Returns a hash code value for this object. </summary>
222  public override int GetHashCode()
223  {
224  // Simple add is OK since no existing filter hashcode has a float component.
225  return internalFilter.GetHashCode() + BitConverter.ToInt32(BitConverter.GetBytes(Boost), 0);
226  }
227 
228  override public System.Object Clone()
229  {
230  // {{Aroush-1.9}} is this all that we need to clone?!
232  clone.internalFilter = (Filter)this.internalFilter;
233  return clone;
234  }
235  }
236 }