QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / ConstantScoreQuery.cs
bloba11afbd037f2f415e6f3fccf9575f5d68cc658a9
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
20 namespace Lucene.Net.Search
23 /// <summary> A query that wraps a filter and simply returns a constant score equal to the
24 /// query boost for every document in the filter.
25 ///
26 /// </summary>
27 /// <author> yonik
28 /// </author>
29 /// <version> $Id: ConstantScoreQuery.cs,v 1.2 2006/10/02 17:09:02 joeshaw Exp $
30 /// </version>
31 [Serializable]
32 public class ConstantScoreQuery : Query
34 protected internal Filter filter;
36 public ConstantScoreQuery(Filter filter)
38 this.filter = filter;
41 public override Query Rewrite(IndexReader reader)
43 return this;
46 [Serializable]
47 protected internal class ConstantWeight : Weight
49 private void InitBlock(ConstantScoreQuery enclosingInstance)
51 this.enclosingInstance = enclosingInstance;
53 private ConstantScoreQuery enclosingInstance;
54 public ConstantScoreQuery Enclosing_Instance
56 get
58 return enclosingInstance;
62 private Searcher searcher;
63 private float queryNorm;
64 private float queryWeight;
66 public ConstantWeight(ConstantScoreQuery enclosingInstance, Searcher searcher)
68 InitBlock(enclosingInstance);
69 this.searcher = searcher;
72 public virtual Query GetQuery()
74 return Enclosing_Instance;
77 public virtual float GetValue()
79 return queryWeight;
82 public virtual float SumOfSquaredWeights()
84 queryWeight = Enclosing_Instance.GetBoost();
85 return queryWeight * queryWeight;
88 public virtual void Normalize(float norm)
90 this.queryNorm = norm;
91 queryWeight *= this.queryNorm;
94 public virtual Scorer Scorer(IndexReader reader)
96 return new ConstantScorer(enclosingInstance, Enclosing_Instance.GetSimilarity(searcher), reader, this);
99 public virtual Explanation Explain(IndexReader reader, int doc)
102 ConstantScorer cs = (ConstantScorer) Scorer(reader);
103 bool exists = cs.bits.Get(doc);
105 Explanation result = new Explanation();
107 if (exists)
109 result.SetDescription("ConstantScoreQuery(" + Enclosing_Instance.filter + "), product of:");
110 result.SetValue(queryWeight);
111 result.AddDetail(new Explanation(Enclosing_Instance.GetBoost(), "boost"));
112 result.AddDetail(new Explanation(queryNorm, "queryNorm"));
114 else
116 result.SetDescription("ConstantScoreQuery(" + Enclosing_Instance.filter + ") doesn't match id " + doc);
117 result.SetValue(0);
119 return result;
123 protected internal class ConstantScorer : Scorer
125 private void InitBlock(ConstantScoreQuery enclosingInstance)
127 this.enclosingInstance = enclosingInstance;
129 private ConstantScoreQuery enclosingInstance;
130 public ConstantScoreQuery Enclosing_Instance
134 return enclosingInstance;
138 internal System.Collections.BitArray bits;
139 internal float theScore;
140 internal int doc = - 1;
142 public ConstantScorer(ConstantScoreQuery enclosingInstance, Similarity similarity, IndexReader reader, Weight w) : base(similarity)
144 InitBlock(enclosingInstance);
145 theScore = w.GetValue();
146 bits = Enclosing_Instance.filter.Bits(reader);
149 public override bool Next()
151 doc = SupportClass.Number.NextSetBit(bits, doc + 1);
152 return doc >= 0;
155 public override int Doc()
157 return doc;
160 public override float Score()
162 return theScore;
165 public override bool SkipTo(int target)
167 doc = SupportClass.Number.NextSetBit(bits, target); // requires JDK 1.4
168 return doc >= 0;
171 public override Explanation Explain(int doc)
173 throw new System.NotSupportedException();
178 protected internal override Weight CreateWeight(Searcher searcher)
180 return new ConstantScoreQuery.ConstantWeight(this, searcher);
184 /// <summary>Prints a user-readable version of this query. </summary>
185 public override System.String ToString(System.String field)
187 return "ConstantScore(" + filter.ToString() + (GetBoost() == 1.0 ? ")" : "^" + GetBoost());
190 /// <summary>Returns true if <code>o</code> is equal to this. </summary>
191 public override bool Equals(System.Object o)
193 if (this == o)
194 return true;
195 if (!(o is ConstantScoreQuery))
196 return false;
197 ConstantScoreQuery other = (ConstantScoreQuery) o;
198 return this.GetBoost() == other.GetBoost() && filter.Equals(other.filter);
201 /// <summary>Returns a hash code value for this object. </summary>
202 public override int GetHashCode()
204 // Simple add is OK since no existing filter hashcode has a float component.
205 return filter.GetHashCode() + BitConverter.ToInt32(BitConverter.GetBytes(GetBoost()), 0);
208 override public System.Object Clone()
210 // {{Aroush-1.9}} is this all that we need to clone?!
211 ConstantScoreQuery clone = (ConstantScoreQuery) base.Clone();
212 clone.filter = (Filter) this.filter;
213 return clone;