add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / TopTermsRewrite.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 java.io.IOException;
21 import java.util.PriorityQueue;
22
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.Term;
25
26 /**
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.
30  */
31 public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
32
33   private final int size;
34   
35   /** 
36    * Create a TopTermsBooleanQueryRewrite for 
37    * at most <code>size</code> terms.
38    * <p>
39    * NOTE: if {@link BooleanQuery#getMaxClauseCount} is smaller than 
40    * <code>size</code>, then it will be used instead. 
41    */
42   public TopTermsRewrite(int size) {
43     this.size = size;
44   }
45   
46   /** return the maximum priority queue size */
47   public int getSize() {
48     return size;
49   }
50   
51   /** return the maximum size of the priority queue (for boolean rewrites this is BooleanQuery#getMaxClauseCount). */
52   protected abstract int getMaxSize();
53   
54   @Override
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)
62           return true;
63         // add new entry in PQ
64         st.term = t;
65         st.boost = boost;
66         stQueue.offer(st);
67         // possibly drop entries from queue
68         st = (stQueue.size() > maxSize) ? stQueue.poll() : new ScoreTerm();
69         return true;
70       }
71       
72       // reusable instance
73       private ScoreTerm st = new ScoreTerm();
74     });
75     
76     final Q q = getTopLevelQuery();
77     for (final ScoreTerm st : stQueue) {
78       addClause(q, st.term, query.getBoost() * st.boost); // add to query
79     }
80     query.incTotalNumberOfTerms(stQueue.size());
81     
82     return q;
83   }
84
85   @Override
86   public int hashCode() {
87     return 31 * size;
88   }
89
90   @Override
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;
97     return true;
98   }
99   
100   private static class ScoreTerm implements Comparable<ScoreTerm> {
101     public Term term;
102     public float boost;
103     
104     public int compareTo(ScoreTerm other) {
105       if (this.boost == other.boost)
106         return other.term.compareTo(this.term);
107       else
108         return Float.compare(this.boost, other.boost);
109     }
110   }
111   
112 }