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 java.io.IOException;
21 import java.util.PriorityQueue;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.Term;
27 * Base rewrite method for collecting only the top terms
28 * via a priority queue.
29 * @lucene.internal Only public to be accessible by spans package.
31 public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
33 private final int size;
36 * Create a TopTermsBooleanQueryRewrite for
37 * at most <code>size</code> terms.
39 * NOTE: if {@link BooleanQuery#getMaxClauseCount} is smaller than
40 * <code>size</code>, then it will be used instead.
42 public TopTermsRewrite(int size) {
46 /** return the maximum priority queue size */
47 public int getSize() {
51 /** return the maximum size of the priority queue (for boolean rewrites this is BooleanQuery#getMaxClauseCount). */
52 protected abstract int getMaxSize();
55 public Q rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
56 final int maxSize = Math.min(size, getMaxSize());
57 final PriorityQueue<ScoreTerm> stQueue = new PriorityQueue<ScoreTerm>();
58 collectTerms(reader, query, new TermCollector() {
59 public boolean collect(Term t, float boost) {
60 // ignore uncompetitive hits
61 if (stQueue.size() >= maxSize && boost <= stQueue.peek().boost)
63 // add new entry in PQ
67 // possibly drop entries from queue
68 st = (stQueue.size() > maxSize) ? stQueue.poll() : new ScoreTerm();
73 private ScoreTerm st = new ScoreTerm();
76 final Q q = getTopLevelQuery();
77 for (final ScoreTerm st : stQueue) {
78 addClause(q, st.term, query.getBoost() * st.boost); // add to query
80 query.incTotalNumberOfTerms(stQueue.size());
86 public int hashCode() {
91 public boolean equals(Object obj) {
92 if (this == obj) return true;
93 if (obj == null) return false;
94 if (getClass() != obj.getClass()) return false;
95 final TopTermsRewrite other = (TopTermsRewrite) obj;
96 if (size != other.size) return false;
100 private static class ScoreTerm implements Comparable<ScoreTerm> {
104 public int compareTo(ScoreTerm other) {
105 if (this.boost == other.boost)
106 return other.term.compareTo(this.term);
108 return Float.compare(this.boost, other.boost);