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;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.BooleanQuery;
24 import org.apache.lucene.search.DefaultSimilarity;
25 import org.apache.lucene.search.Query;
26 import org.apache.lucene.search.Searcher;
27 import org.apache.lucene.search.Similarity;
29 * The BoostingQuery class can be used to effectively demote results that match a given query.
30 * Unlike the "NOT" clause, this still selects documents that contain undesirable terms,
31 * but reduces their overall score:
33 * Query balancedQuery = new BoostingQuery(positiveQuery, negativeQuery, 0.01f);
34 * In this scenario the positiveQuery contains the mandatory, desirable criteria which is used to
35 * select all matching documents, and the negativeQuery contains the undesirable elements which
36 * are simply used to lessen the scores. Documents that match the negativeQuery have their score
37 * multiplied by the supplied "boost" parameter, so this should be less than 1 to achieve a
40 * This code was originally made available here: [WWW] http://marc.theaimsgroup.com/?l=lucene-user&m=108058407130459&w=2
41 * and is documented here: http://wiki.apache.org/lucene-java/CommunityContributions
43 public class BoostingQuery extends Query {
44 private float boost; // the amount to boost by
45 private Query match; // query to match
46 private Query context; // boost when matches too
48 public BoostingQuery(Query match, Query context, float boost) {
50 this.context = (Query)context.clone(); // clone before boost
53 this.context.setBoost(0.0f); // ignore context-only matches
57 public Query rewrite(IndexReader reader) throws IOException {
58 BooleanQuery result = new BooleanQuery() {
61 public Similarity getSimilarity(Searcher searcher) {
62 return new DefaultSimilarity() {
65 public float coord(int overlap, int max) {
68 case 1: // matched only one clause
69 return 1.0f; // use the score as-is
71 case 2: // matched both clauses
72 return boost; // multiply by boost
83 result.add(match, BooleanClause.Occur.MUST);
84 result.add(context, BooleanClause.Occur.SHOULD);
90 public int hashCode() {
93 result = prime * result + Float.floatToIntBits(boost);
94 result = prime * result + ((context == null) ? 0 : context.hashCode());
95 result = prime * result + ((match == null) ? 0 : match.hashCode());
100 public boolean equals(Object obj) {
105 if (getClass() != obj.getClass())
107 BoostingQuery other = (BoostingQuery) obj;
108 if (Float.floatToIntBits(boost) != Float.floatToIntBits(other.boost))
110 if (context == null) {
111 if (other.context != null)
113 } else if (!context.equals(other.context))
116 if (other.match != null)
118 } else if (!match.equals(other.match))
124 public String toString(String field) {
125 return match.toString(field) + "/" + context.toString(field);