pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / MatchAllDocsQuery.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
24
25 import java.util.Set;
26 import java.io.IOException;
27
28 /**
29  * A query that matches all documents.
30  *
31  */
32 public class MatchAllDocsQuery extends Query {
33
34   public MatchAllDocsQuery() {
35     this(null);
36   }
37
38   private final String normsField;
39
40   /**
41    * @param normsField Field used for normalization factor (document boost). Null if nothing.
42    */
43   public MatchAllDocsQuery(String normsField) {
44     this.normsField = normsField;
45   }
46
47   private class MatchAllScorer extends Scorer {
48     final TermDocs termDocs;
49     final float score;
50     final byte[] norms;
51     private int doc = -1;
52     
53     MatchAllScorer(IndexReader reader, Similarity similarity, Weight w,
54         byte[] norms) throws IOException {
55       super(similarity,w);
56       this.termDocs = reader.termDocs(null);
57       score = w.getValue();
58       this.norms = norms;
59     }
60
61     @Override
62     public int docID() {
63       return doc;
64     }
65
66     @Override
67     public int nextDoc() throws IOException {
68       return doc = termDocs.next() ? termDocs.doc() : NO_MORE_DOCS;
69     }
70     
71     @Override
72     public float score() {
73       return norms == null ? score : score * getSimilarity().decodeNormValue(norms[docID()]);
74     }
75
76     @Override
77     public int advance(int target) throws IOException {
78       return doc = termDocs.skipTo(target) ? termDocs.doc() : NO_MORE_DOCS;
79     }
80   }
81
82   private class MatchAllDocsWeight extends Weight {
83     private Similarity similarity;
84     private float queryWeight;
85     private float queryNorm;
86
87     public MatchAllDocsWeight(Searcher searcher) {
88       this.similarity = searcher.getSimilarity();
89     }
90
91     @Override
92     public String toString() {
93       return "weight(" + MatchAllDocsQuery.this + ")";
94     }
95
96     @Override
97     public Query getQuery() {
98       return MatchAllDocsQuery.this;
99     }
100
101     @Override
102     public float getValue() {
103       return queryWeight;
104     }
105
106     @Override
107     public float sumOfSquaredWeights() {
108       queryWeight = getBoost();
109       return queryWeight * queryWeight;
110     }
111
112     @Override
113     public void normalize(float queryNorm) {
114       this.queryNorm = queryNorm;
115       queryWeight *= this.queryNorm;
116     }
117
118     @Override
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);
122     }
123
124     @Override
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"));
131       }
132       queryExpl.addDetail(new Explanation(queryNorm,"queryNorm"));
133
134       return queryExpl;
135     }
136   }
137
138   @Override
139   public Weight createWeight(Searcher searcher) {
140     return new MatchAllDocsWeight(searcher);
141   }
142
143   @Override
144   public void extractTerms(Set<Term> terms) {
145   }
146
147   @Override
148   public String toString(String field) {
149     StringBuilder buffer = new StringBuilder();
150     buffer.append("*:*");
151     buffer.append(ToStringUtils.boost(getBoost()));
152     return buffer.toString();
153   }
154
155   @Override
156   public boolean equals(Object o) {
157     if (!(o instanceof MatchAllDocsQuery))
158       return false;
159     MatchAllDocsQuery other = (MatchAllDocsQuery) o;
160     return this.getBoost() == other.getBoost();
161   }
162
163   @Override
164   public int hashCode() {
165     return Float.floatToIntBits(getBoost()) ^ 0x1AA71190;
166   }
167 }