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 org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
25 /** @lucene.internal Only public to be accessible by spans package. */
26 public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewrite<Q> {
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.
36 * <p><b>NOTE</b>: This rewrite method will hit {@link
37 * BooleanQuery.TooManyClauses} if the number of terms
38 * exceeds {@link BooleanQuery#getMaxClauseCount}.
40 * @see #setRewriteMethod */
41 public final static ScoringRewrite<BooleanQuery> SCORING_BOOLEAN_QUERY_REWRITE = new ScoringRewrite<BooleanQuery>() {
43 protected BooleanQuery getTopLevelQuery() {
44 return new BooleanQuery(true);
48 protected void addClause(BooleanQuery topLevel, Term term, float boost) {
49 final TermQuery tq = new TermQuery(term);
51 topLevel.add(tq, BooleanClause.Occur.SHOULD);
54 // Make sure we are still a singleton even after deserializing
55 protected Object readResolve() {
56 return SCORING_BOOLEAN_QUERY_REWRITE;
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
65 * <p><b>NOTE</b>: This rewrite method will hit {@link
66 * BooleanQuery.TooManyClauses} if the number of terms
67 * exceeds {@link BooleanQuery#getMaxClauseCount}.
69 * @see #setRewriteMethod */
70 public final static RewriteMethod CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE = new RewriteMethod() {
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())
77 // strip the scores off
78 final Query result = new ConstantScoreQuery(bq);
79 result.setBoost(query.getBoost());
83 // Make sure we are still a singleton even after deserializing
84 protected Object readResolve() {
85 return CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE;
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);
100 query.incTotalNumberOfTerms(size[0]);