001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.IndexReader;
021: import org.apache.lucene.search.Explanation;
022: import org.apache.lucene.search.Query;
023: import org.apache.lucene.search.Scorer;
024: import org.apache.lucene.search.Searcher;
025: import org.apache.lucene.search.Similarity;
026: import org.apache.lucene.search.Weight;
027: import org.apache.lucene.util.ToStringUtils;
028:
029: import java.util.Set;
030:
031: /**
032: * A query that matches all documents.
033: *
034: * @author John Wang
035: */
036: public class MatchAllDocsQuery extends Query {
037:
038: public MatchAllDocsQuery() {
039: }
040:
041: private class MatchAllScorer extends Scorer {
042:
043: final IndexReader reader;
044: int id;
045: final int maxId;
046: final float score;
047:
048: MatchAllScorer(IndexReader reader, Similarity similarity,
049: Weight w) {
050: super (similarity);
051: this .reader = reader;
052: id = -1;
053: maxId = reader.maxDoc() - 1;
054: score = w.getValue();
055: }
056:
057: public Explanation explain(int doc) {
058: return null; // not called... see MatchAllDocsWeight.explain()
059: }
060:
061: public int doc() {
062: return id;
063: }
064:
065: public boolean next() {
066: while (id < maxId) {
067: id++;
068: if (!reader.isDeleted(id)) {
069: return true;
070: }
071: }
072: return false;
073: }
074:
075: public float score() {
076: return score;
077: }
078:
079: public boolean skipTo(int target) {
080: id = target - 1;
081: return next();
082: }
083:
084: }
085:
086: private class MatchAllDocsWeight implements Weight {
087: private Similarity similarity;
088: private float queryWeight;
089: private float queryNorm;
090:
091: public MatchAllDocsWeight(Searcher searcher) {
092: this .similarity = searcher.getSimilarity();
093: }
094:
095: public String toString() {
096: return "weight(" + MatchAllDocsQuery.this + ")";
097: }
098:
099: public Query getQuery() {
100: return MatchAllDocsQuery.this ;
101: }
102:
103: public float getValue() {
104: return queryWeight;
105: }
106:
107: public float sumOfSquaredWeights() {
108: queryWeight = getBoost();
109: return queryWeight * queryWeight;
110: }
111:
112: public void normalize(float queryNorm) {
113: this .queryNorm = queryNorm;
114: queryWeight *= this .queryNorm;
115: }
116:
117: public Scorer scorer(IndexReader reader) {
118: return new MatchAllScorer(reader, similarity, this );
119: }
120:
121: public Explanation explain(IndexReader reader, int doc) {
122: // explain query weight
123: Explanation queryExpl = new ComplexExplanation(true,
124: getValue(), "MatchAllDocsQuery, product of:");
125: if (getBoost() != 1.0f) {
126: queryExpl
127: .addDetail(new Explanation(getBoost(), "boost"));
128: }
129: queryExpl
130: .addDetail(new Explanation(queryNorm, "queryNorm"));
131:
132: return queryExpl;
133: }
134: }
135:
136: protected Weight createWeight(Searcher searcher) {
137: return new MatchAllDocsWeight(searcher);
138: }
139:
140: public void extractTerms(Set terms) {
141: }
142:
143: public String toString(String field) {
144: StringBuffer buffer = new StringBuffer();
145: buffer.append("MatchAllDocsQuery");
146: buffer.append(ToStringUtils.boost(getBoost()));
147: return buffer.toString();
148: }
149:
150: public boolean equals(Object o) {
151: if (!(o instanceof MatchAllDocsQuery))
152: return false;
153: MatchAllDocsQuery other = (MatchAllDocsQuery) o;
154: return this .getBoost() == other.getBoost();
155: }
156:
157: public int hashCode() {
158: return Float.floatToIntBits(getBoost()) ^ 0x1AA71190;
159: }
160: }
|