add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queries / src / java / org / apache / lucene / search / BoostingQuery.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
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;
28 /**
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:
32  *
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 
38  * demoting effect
39  * 
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
42  */
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
47
48     public BoostingQuery(Query match, Query context, float boost) {
49       this.match = match;
50       this.context = (Query)context.clone();        // clone before boost
51       this.boost = boost;
52
53       this.context.setBoost(0.0f);                      // ignore context-only matches
54     }
55
56     @Override
57     public Query rewrite(IndexReader reader) throws IOException {
58       BooleanQuery result = new BooleanQuery() {
59
60         @Override
61         public Similarity getSimilarity(Searcher searcher) {
62           return new DefaultSimilarity() {
63
64             @Override
65             public float coord(int overlap, int max) {
66               switch (overlap) {
67
68               case 1:                               // matched only one clause
69                 return 1.0f;                        // use the score as-is
70
71               case 2:                               // matched both clauses
72                 return boost;                       // multiply by boost
73
74               default:
75                 return 0.0f;
76                 
77               }
78             }
79           };
80         }
81       };
82
83       result.add(match, BooleanClause.Occur.MUST);
84       result.add(context, BooleanClause.Occur.SHOULD);
85
86       return result;
87     }
88
89     @Override
90     public int hashCode() {
91       final int prime = 31;
92       int result = 1;
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());
96       return result;
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101       if (this == obj)
102         return true;
103       if (obj == null)
104         return false;
105       if (getClass() != obj.getClass())
106         return false;
107       BoostingQuery other = (BoostingQuery) obj;
108       if (Float.floatToIntBits(boost) != Float.floatToIntBits(other.boost))
109         return false;
110       if (context == null) {
111         if (other.context != null)
112           return false;
113       } else if (!context.equals(other.context))
114         return false;
115       if (match == null) {
116         if (other.match != null)
117           return false;
118       } else if (!match.equals(other.match))
119         return false;
120       return true;
121     }
122
123     @Override
124     public String toString(String field) {
125       return match.toString(field) + "/" + context.toString(field);
126     }
127   }