KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2005 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 import org.apache.lucene.search.Explanation;
21 import org.apache.lucene.search.Query;
22 import org.apache.lucene.search.Scorer;
23 import org.apache.lucene.search.Searcher;
24 import org.apache.lucene.search.Similarity;
25 import org.apache.lucene.search.Weight;
26 import org.apache.lucene.util.ToStringUtils;
27
28 /**
29  * A query that matches all documents.
30  *
31  * @author John Wang
32  */

33 public class MatchAllDocsQuery extends Query {
34
35   public MatchAllDocsQuery() {
36   }
37
38   private class MatchAllScorer extends Scorer {
39
40     final IndexReader reader;
41     int id;
42     final int maxId;
43     final float score;
44
45     MatchAllScorer(IndexReader reader, Similarity similarity, Weight w) {
46       super(similarity);
47       this.reader = reader;
48       id = -1;
49       maxId = reader.maxDoc() - 1;
50       score = w.getValue();
51     }
52
53     public Explanation explain(int doc) {
54       return null; // not called... see MatchAllDocsWeight.explain()
55
}
56
57     public int doc() {
58       return id;
59     }
60
61     public boolean next() {
62       while (id < maxId) {
63         id++;
64         if (!reader.isDeleted(id)) {
65           return true;
66         }
67       }
68       return false;
69     }
70
71     public float score() {
72       return score;
73     }
74
75     public boolean skipTo(int target) {
76       id = target - 1;
77       return next();
78     }
79
80   }
81
82   private class MatchAllDocsWeight implements Weight {
83     private Searcher searcher;
84     private float queryWeight;
85     private float queryNorm;
86
87     public MatchAllDocsWeight(Searcher searcher) {
88       this.searcher = searcher;
89     }
90
91     public String JavaDoc toString() {
92       return "weight(" + MatchAllDocsQuery.this + ")";
93     }
94
95     public Query getQuery() {
96       return MatchAllDocsQuery.this;
97     }
98
99     public float getValue() {
100       return queryWeight;
101     }
102
103     public float sumOfSquaredWeights() {
104       queryWeight = getBoost();
105       return queryWeight * queryWeight;
106     }
107
108     public void normalize(float queryNorm) {
109       this.queryNorm = queryNorm;
110       queryWeight *= this.queryNorm;
111     }
112
113     public Scorer scorer(IndexReader reader) {
114       return new MatchAllScorer(reader, getSimilarity(searcher), this);
115     }
116
117     public Explanation explain(IndexReader reader, int doc) {
118       // explain query weight
119
Explanation queryExpl = new Explanation();
120       queryExpl.setDescription("MatchAllDocsQuery, product of:");
121       queryExpl.setValue(getValue());
122       if (getBoost() != 1.0f) {
123         queryExpl.addDetail(new Explanation(getBoost(),"boost"));
124       }
125       queryExpl.addDetail(new Explanation(queryNorm,"queryNorm"));
126
127       return queryExpl;
128     }
129   }
130
131   protected Weight createWeight(Searcher searcher) {
132     return new MatchAllDocsWeight(searcher);
133   }
134
135   public String JavaDoc toString(String JavaDoc field) {
136     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137     buffer.append("MatchAllDocsQuery");
138     buffer.append(ToStringUtils.boost(getBoost()));
139     return buffer.toString();
140   }
141
142   public boolean equals(Object JavaDoc o) {
143     if (!(o instanceof MatchAllDocsQuery))
144       return false;
145     MatchAllDocsQuery other = (MatchAllDocsQuery) o;
146     return this.getBoost() == other.getBoost();
147   }
148
149   public int hashCode() {
150     return Float.floatToIntBits(getBoost()) ^ 0x1AA71190;
151   }
152 }
153
Popular Tags