1 package org.apache.lucene.search;
4 * Copyright 2004 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Iterator;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.Term;
29 * A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum
30 * score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.
31 * This is useful when searching for a word in multiple fields with different boost factors (so that the fields cannot be
32 * combined equivalently into a single search field). We want the primary score to be the one associated with the highest boost,
33 * not the sum of the field scores (as BooleanQuery would give).
34 * If the query is "albino elephant" this ensures that "albino" matching one field and "elephant" matching
35 * another gets a higher score than "albino" matching both fields.
36 * To get this result, use both BooleanQuery and DisjunctionMaxQuery: for each term a DisjunctionMaxQuery searches for it in
37 * each field, while the set of these DisjunctionMaxQuery's is combined into a BooleanQuery.
38 * The tie breaker capability allows results that include the same term in multiple fields to be judged better than results that
39 * include this term in only the best of those multiple fields, without confusing this with the better case of two different terms
40 * in the multiple fields.
42 public class DisjunctionMaxQuery extends Query implements Iterable<Query> {
45 private ArrayList<Query> disjuncts = new ArrayList<Query>();
47 /* Multiple of the non-max disjunct scores added into our final score. Non-zero values support tie-breaking. */
48 private float tieBreakerMultiplier = 0.0f;
50 /** Creates a new empty DisjunctionMaxQuery. Use add() to add the subqueries.
51 * @param tieBreakerMultiplier the score of each non-maximum disjunct for a document is multiplied by this weight
52 * and added into the final score. If non-zero, the value should be small, on the order of 0.1, which says that
53 * 10 occurrences of word in a lower-scored field that is also in a higher scored field is just as good as a unique
54 * word in the lower scored field (i.e., one that is not in any higher scored field.
56 public DisjunctionMaxQuery(float tieBreakerMultiplier) {
57 this.tieBreakerMultiplier = tieBreakerMultiplier;
61 * Creates a new DisjunctionMaxQuery
62 * @param disjuncts a Collection<Query> of all the disjuncts to add
63 * @param tieBreakerMultiplier the weight to give to each matching non-maximum disjunct
65 public DisjunctionMaxQuery(Collection<Query> disjuncts, float tieBreakerMultiplier) {
66 this.tieBreakerMultiplier = tieBreakerMultiplier;
70 /** Add a subquery to this disjunction
71 * @param query the disjunct added
73 public void add(Query query) {
77 /** Add a collection of disjuncts to this disjunction
80 public void add(Collection<Query> disjuncts) {
81 this.disjuncts.addAll(disjuncts);
84 /** An Iterator<Query> over the disjuncts */
85 public Iterator<Query> iterator() {
86 return disjuncts.iterator();
90 * Expert: the Weight for DisjunctionMaxQuery, used to
91 * normalize, score and explain these queries.
93 * <p>NOTE: this API and implementation is subject to
94 * change suddenly in the next release.</p>
96 protected class DisjunctionMaxWeight extends Weight {
97 /** The Similarity implementation. */
98 protected Similarity similarity;
100 /** The Weights for our subqueries, in 1-1 correspondence with disjuncts */
101 protected ArrayList<Weight> weights = new ArrayList<Weight>(); // The Weight's for our subqueries, in 1-1 correspondence with disjuncts
103 /** Construct the Weight for this Query searched by searcher. Recursively construct subquery weights. */
104 public DisjunctionMaxWeight(Searcher searcher) throws IOException {
105 this.similarity = searcher.getSimilarity();
106 for (Query disjunctQuery : disjuncts) {
107 weights.add(disjunctQuery.createWeight(searcher));
111 /** Return our associated DisjunctionMaxQuery */
113 public Query getQuery() { return DisjunctionMaxQuery.this; }
115 /** Return our boost */
117 public float getValue() { return getBoost(); }
119 /** Compute the sub of squared weights of us applied to our subqueries. Used for normalization. */
121 public float sumOfSquaredWeights() throws IOException {
122 float max = 0.0f, sum = 0.0f;
123 for (Weight currentWeight : weights) {
124 float sub = currentWeight.sumOfSquaredWeights();
126 max = Math.max(max, sub);
129 float boost = getBoost();
130 return (((sum - max) * tieBreakerMultiplier * tieBreakerMultiplier) + max) * boost * boost;
133 /** Apply the computed normalization factor to our subqueries */
135 public void normalize(float norm) {
136 norm *= getBoost(); // Incorporate our boost
137 for (Weight wt : weights) {
142 /** Create the scorer used to score our associated DisjunctionMaxQuery */
144 public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
145 boolean topScorer) throws IOException {
146 Scorer[] scorers = new Scorer[weights.size()];
148 for (Weight w : weights) {
149 Scorer subScorer = w.scorer(reader, true, false);
150 if (subScorer != null && subScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
151 scorers[idx++] = subScorer;
154 if (idx == 0) return null; // all scorers did not have documents
155 DisjunctionMaxScorer result = new DisjunctionMaxScorer(this, tieBreakerMultiplier, similarity, scorers, idx);
159 /** Explain the score we computed for doc */
161 public Explanation explain(IndexReader reader, int doc) throws IOException {
162 if (disjuncts.size() == 1) return weights.get(0).explain(reader,doc);
163 ComplexExplanation result = new ComplexExplanation();
164 float max = 0.0f, sum = 0.0f;
165 result.setDescription(tieBreakerMultiplier == 0.0f ? "max of:" : "max plus " + tieBreakerMultiplier + " times others of:");
166 for (Weight wt : weights) {
167 Explanation e = wt.explain(reader, doc);
169 result.setMatch(Boolean.TRUE);
172 max = Math.max(max, e.getValue());
175 result.setValue(max + (sum - max) * tieBreakerMultiplier);
179 } // end of DisjunctionMaxWeight inner class
181 /** Create the Weight used to score us */
183 public Weight createWeight(Searcher searcher) throws IOException {
184 return new DisjunctionMaxWeight(searcher);
187 /** Optimize our representation and our subqueries representations
188 * @param reader the IndexReader we query
189 * @return an optimized copy of us (which may not be a copy if there is nothing to optimize) */
191 public Query rewrite(IndexReader reader) throws IOException {
192 int numDisjunctions = disjuncts.size();
193 if (numDisjunctions == 1) {
194 Query singleton = disjuncts.get(0);
195 Query result = singleton.rewrite(reader);
196 if (getBoost() != 1.0f) {
197 if (result == singleton) result = (Query)result.clone();
198 result.setBoost(getBoost() * result.getBoost());
202 DisjunctionMaxQuery clone = null;
203 for (int i = 0 ; i < numDisjunctions; i++) {
204 Query clause = disjuncts.get(i);
205 Query rewrite = clause.rewrite(reader);
206 if (rewrite != clause) {
207 if (clone == null) clone = (DisjunctionMaxQuery)this.clone();
208 clone.disjuncts.set(i, rewrite);
211 if (clone != null) return clone;
215 /** Create a shallow copy of us -- used in rewriting if necessary
216 * @return a copy of us (but reuse, don't copy, our subqueries) */
217 @Override @SuppressWarnings("unchecked")
218 public Object clone() {
219 DisjunctionMaxQuery clone = (DisjunctionMaxQuery)super.clone();
220 clone.disjuncts = (ArrayList<Query>) this.disjuncts.clone();
226 public void extractTerms(Set<Term> terms) {
227 for (Query query : disjuncts) {
228 query.extractTerms(terms);
233 * @param field the field to which we are applied
234 * @return a string that shows what we do, of the form "(disjunct1 | disjunct2 | ... | disjunctn)^boost"
237 public String toString(String field) {
238 StringBuilder buffer = new StringBuilder();
240 int numDisjunctions = disjuncts.size();
241 for (int i = 0 ; i < numDisjunctions; i++) {
242 Query subquery = disjuncts.get(i);
243 if (subquery instanceof BooleanQuery) { // wrap sub-bools in parens
245 buffer.append(subquery.toString(field));
248 else buffer.append(subquery.toString(field));
249 if (i != numDisjunctions-1) buffer.append(" | ");
252 if (tieBreakerMultiplier != 0.0f) {
254 buffer.append(tieBreakerMultiplier);
256 if (getBoost() != 1.0) {
258 buffer.append(getBoost());
260 return buffer.toString();
263 /** Return true iff we represent the same query as o
264 * @param o another object
265 * @return true iff o is a DisjunctionMaxQuery with the same boost and the same subqueries, in the same order, as us
268 public boolean equals(Object o) {
269 if (! (o instanceof DisjunctionMaxQuery) ) return false;
270 DisjunctionMaxQuery other = (DisjunctionMaxQuery)o;
271 return this.getBoost() == other.getBoost()
272 && this.tieBreakerMultiplier == other.tieBreakerMultiplier
273 && this.disjuncts.equals(other.disjuncts);
276 /** Compute a hash code for hashing us
277 * @return the hash code
280 public int hashCode() {
281 return Float.floatToIntBits(getBoost())
282 + Float.floatToIntBits(tieBreakerMultiplier)
283 + disjuncts.hashCode();