KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > ConstantScoreQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.index.IndexReader;
20
21 import java.io.IOException JavaDoc;
22 import java.util.BitSet JavaDoc;
23
24 /**
25  * 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  *
28  * @author yonik
29  * @version $Id$
30  */

31 public class ConstantScoreQuery extends Query {
32   protected final Filter filter;
33
34   public ConstantScoreQuery(Filter filter) {
35     this.filter=filter;
36   }
37
38   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
39     return this;
40   }
41
42   protected class ConstantWeight implements Weight {
43     private Searcher searcher;
44     private float queryNorm;
45     private float queryWeight;
46
47     public ConstantWeight(Searcher searcher) {
48       this.searcher = searcher;
49     }
50
51     public Query getQuery() {
52       return ConstantScoreQuery.this;
53     }
54
55     public float getValue() {
56       return queryWeight;
57     }
58
59     public float sumOfSquaredWeights() throws IOException JavaDoc {
60       queryWeight = getBoost();
61       return queryWeight * queryWeight;
62     }
63
64     public void normalize(float norm) {
65       this.queryNorm = norm;
66       queryWeight *= this.queryNorm;
67     }
68
69     public Scorer scorer(IndexReader reader) throws IOException JavaDoc {
70       return new ConstantScorer(getSimilarity(searcher), reader, this);
71     }
72
73     public Explanation explain(IndexReader reader, int doc) throws IOException JavaDoc {
74
75       ConstantScorer cs = (ConstantScorer)scorer(reader);
76       boolean exists = cs.bits.get(doc);
77
78       Explanation result = new Explanation();
79
80       if (exists) {
81         result.setDescription("ConstantScoreQuery(" + filter
82         + "), product of:");
83         result.setValue(queryWeight);
84         result.addDetail(new Explanation(getBoost(), "boost"));
85         result.addDetail(new Explanation(queryNorm,"queryNorm"));
86       } else {
87         result.setDescription("ConstantScoreQuery(" + filter
88         + ") doesn't match id " + doc);
89         result.setValue(0);
90       }
91       return result;
92     }
93   }
94
95   protected class ConstantScorer extends Scorer {
96     final BitSet JavaDoc bits;
97     final float theScore;
98     int doc=-1;
99
100     public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException JavaDoc {
101       super(similarity);
102       theScore = w.getValue();
103       bits = filter.bits(reader);
104     }
105
106     public boolean next() throws IOException JavaDoc {
107       doc = bits.nextSetBit(doc+1);
108       return doc >= 0;
109     }
110
111     public int doc() {
112       return doc;
113     }
114
115     public float score() throws IOException JavaDoc {
116       return theScore;
117     }
118
119     public boolean skipTo(int target) throws IOException JavaDoc {
120       doc = bits.nextSetBit(target); // requires JDK 1.4
121
return doc >= 0;
122     }
123
124     public Explanation explain(int doc) throws IOException JavaDoc {
125       throw new UnsupportedOperationException JavaDoc();
126     }
127   }
128
129
130   protected Weight createWeight(Searcher searcher) {
131     return new ConstantScoreQuery.ConstantWeight(searcher);
132   }
133
134
135   /** Prints a user-readable version of this query. */
136   public String JavaDoc toString(String JavaDoc field)
137   {
138     return "ConstantScore(" + filter.toString()
139       + (getBoost()==1.0 ? ")" : "^" + getBoost());
140   }
141
142   /** Returns true if <code>o</code> is equal to this. */
143   public boolean equals(Object JavaDoc o) {
144     if (this == o) return true;
145     if (!(o instanceof ConstantScoreQuery)) return false;
146     ConstantScoreQuery other = (ConstantScoreQuery)o;
147     return this.getBoost()==other.getBoost() && filter.equals(other.filter);
148   }
149
150   /** Returns a hash code value for this object. */
151   public int hashCode() {
152     // Simple add is OK since no existing filter hashcode has a float component.
153
return filter.hashCode() + Float.floatToIntBits(getBoost());
154   }
155
156 }
157
158
159
Popular Tags