pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / ScoringRewrite.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 org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
24
25 /** @lucene.internal Only public to be accessible by spans package. */
26 public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
27
28   /** A rewrite method that first translates each term into
29    *  {@link BooleanClause.Occur#SHOULD} clause in a
30    *  BooleanQuery, and keeps the scores as computed by the
31    *  query.  Note that typically such scores are
32    *  meaningless to the user, and require non-trivial CPU
33    *  to compute, so it's almost always better to use {@link
34    *  MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT} instead.
35    *
36    *  <p><b>NOTE</b>: This rewrite method will hit {@link
37    *  BooleanQuery.TooManyClauses} if the number of terms
38    *  exceeds {@link BooleanQuery#getMaxClauseCount}.
39    *
40    *  @see #setRewriteMethod */
41   public final static ScoringRewrite<BooleanQuery> SCORING_BOOLEAN_QUERY_REWRITE = new ScoringRewrite<BooleanQuery>() {
42     @Override
43     protected BooleanQuery getTopLevelQuery() {
44       return new BooleanQuery(true);
45     }
46     
47     @Override
48     protected void addClause(BooleanQuery topLevel, Term term, float boost) {
49       final TermQuery tq = new TermQuery(term);
50       tq.setBoost(boost);
51       topLevel.add(tq, BooleanClause.Occur.SHOULD);
52     }
53     
54     // Make sure we are still a singleton even after deserializing
55     protected Object readResolve() {
56       return SCORING_BOOLEAN_QUERY_REWRITE;
57     }    
58   };
59   
60   /** Like {@link #SCORING_BOOLEAN_QUERY_REWRITE} except
61    *  scores are not computed.  Instead, each matching
62    *  document receives a constant score equal to the
63    *  query's boost.
64    * 
65    *  <p><b>NOTE</b>: This rewrite method will hit {@link
66    *  BooleanQuery.TooManyClauses} if the number of terms
67    *  exceeds {@link BooleanQuery#getMaxClauseCount}.
68    *
69    *  @see #setRewriteMethod */
70   public final static RewriteMethod CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE = new RewriteMethod() {
71     @Override
72     public Query rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
73       final BooleanQuery bq = SCORING_BOOLEAN_QUERY_REWRITE.rewrite(reader, query);
74       // TODO: if empty boolean query return NullQuery?
75       if (bq.clauses().isEmpty())
76         return bq;
77       // strip the scores off
78       final Query result = new ConstantScoreQuery(bq);
79       result.setBoost(query.getBoost());
80       return result;
81     }
82
83     // Make sure we are still a singleton even after deserializing
84     protected Object readResolve() {
85       return CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE;
86     }
87   };
88
89   @Override
90   public Q rewrite(final IndexReader reader, final MultiTermQuery query) throws IOException {
91     final Q result = getTopLevelQuery();
92     final int[] size = new int[1]; // "trick" to be able to make it final
93     collectTerms(reader, query, new TermCollector() {
94       public boolean collect(Term t, float boost) throws IOException {
95         addClause(result, t, query.getBoost() * boost);
96         size[0]++;
97         return true;
98       }
99     });
100     query.incTotalNumberOfTerms(size[0]);
101     return result;
102   }
103 }