1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.TermDocs;
23 import org.apache.lucene.util.ToStringUtils;
26 import java.io.IOException;
29 * A query that matches all documents.
32 public class MatchAllDocsQuery extends Query {
34 public MatchAllDocsQuery() {
38 private final String normsField;
41 * @param normsField Field used for normalization factor (document boost). Null if nothing.
43 public MatchAllDocsQuery(String normsField) {
44 this.normsField = normsField;
47 private class MatchAllScorer extends Scorer {
48 final TermDocs termDocs;
53 MatchAllScorer(IndexReader reader, Similarity similarity, Weight w,
54 byte[] norms) throws IOException {
56 this.termDocs = reader.termDocs(null);
67 public int nextDoc() throws IOException {
68 return doc = termDocs.next() ? termDocs.doc() : NO_MORE_DOCS;
72 public float score() {
73 return norms == null ? score : score * getSimilarity().decodeNormValue(norms[docID()]);
77 public int advance(int target) throws IOException {
78 return doc = termDocs.skipTo(target) ? termDocs.doc() : NO_MORE_DOCS;
82 private class MatchAllDocsWeight extends Weight {
83 private Similarity similarity;
84 private float queryWeight;
85 private float queryNorm;
87 public MatchAllDocsWeight(Searcher searcher) {
88 this.similarity = searcher.getSimilarity();
92 public String toString() {
93 return "weight(" + MatchAllDocsQuery.this + ")";
97 public Query getQuery() {
98 return MatchAllDocsQuery.this;
102 public float getValue() {
107 public float sumOfSquaredWeights() {
108 queryWeight = getBoost();
109 return queryWeight * queryWeight;
113 public void normalize(float queryNorm) {
114 this.queryNorm = queryNorm;
115 queryWeight *= this.queryNorm;
119 public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
120 return new MatchAllScorer(reader, similarity, this,
121 normsField != null ? reader.norms(normsField) : null);
125 public Explanation explain(IndexReader reader, int doc) {
126 // explain query weight
127 Explanation queryExpl = new ComplexExplanation
128 (true, getValue(), "MatchAllDocsQuery, product of:");
129 if (getBoost() != 1.0f) {
130 queryExpl.addDetail(new Explanation(getBoost(),"boost"));
132 queryExpl.addDetail(new Explanation(queryNorm,"queryNorm"));
139 public Weight createWeight(Searcher searcher) {
140 return new MatchAllDocsWeight(searcher);
144 public void extractTerms(Set<Term> terms) {
148 public String toString(String field) {
149 StringBuilder buffer = new StringBuilder();
150 buffer.append("*:*");
151 buffer.append(ToStringUtils.boost(getBoost()));
152 return buffer.toString();
156 public boolean equals(Object o) {
157 if (!(o instanceof MatchAllDocsQuery))
159 MatchAllDocsQuery other = (MatchAllDocsQuery) o;
160 return this.getBoost() == other.getBoost();
164 public int hashCode() {
165 return Float.floatToIntBits(getBoost()) ^ 0x1AA71190;