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
;
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.
29 /// <version> $Id: ConstantScoreQuery.cs,v 1.2 2006/10/02 17:09:02 joeshaw Exp $
32 public class ConstantScoreQuery
: Query
34 protected internal Filter filter
;
36 public ConstantScoreQuery(Filter filter
)
41 public override Query
Rewrite(IndexReader reader
)
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
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()
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();
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"));
116 result
.SetDescription("ConstantScoreQuery(" + Enclosing_Instance
.filter
+ ") doesn't match id " + doc
);
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);
155 public override int Doc()
160 public override float Score()
165 public override bool SkipTo(int target
)
167 doc
= SupportClass
.Number
.NextSetBit(bits
, target
); // requires JDK 1.4
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
)
195 if (!(o
is ConstantScoreQuery
))
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
;