19
19
20
20
package org .elasticsearch .index .query ;
21
21
22
+ import org .apache .lucene .search .Query ;
23
+ import org .apache .lucene .queries .BoostingQuery ;
24
+ import org .elasticsearch .common .io .stream .StreamInput ;
25
+ import org .elasticsearch .common .io .stream .StreamOutput ;
22
26
import org .elasticsearch .common .xcontent .XContentBuilder ;
23
27
24
28
import java .io .IOException ;
29
+ import java .util .Objects ;
25
30
26
31
/**
27
32
* The BoostingQuery class can be used to effectively demote results that match a given query.
35
40
* multiplied by the supplied "boost" parameter, so this should be less than 1 to achieve a
36
41
* demoting effect
37
42
*/
38
- public class BoostingQueryBuilder extends QueryBuilder implements BoostableQueryBuilder <BoostingQueryBuilder > {
43
+ public class BoostingQueryBuilder extends QueryBuilder < BoostingQueryBuilder > implements BoostableQueryBuilder <BoostingQueryBuilder > {
39
44
40
45
public static final String NAME = "boosting" ;
41
46
@@ -45,34 +50,74 @@ public class BoostingQueryBuilder extends QueryBuilder implements BoostableQuery
45
50
46
51
private float negativeBoost = -1 ;
47
52
48
- private float boost = - 1 ;
53
+ private float boost = 1.0f ;
49
54
50
55
static final BoostingQueryBuilder PROTOTYPE = new BoostingQueryBuilder ();
51
56
52
57
public BoostingQueryBuilder () {
53
58
}
54
59
60
+ /**
61
+ * Add the positive query for this boosting query.
62
+ */
55
63
public BoostingQueryBuilder positive (QueryBuilder positiveQuery ) {
56
64
this .positiveQuery = positiveQuery ;
57
65
return this ;
58
66
}
59
67
68
+ /**
69
+ * Get the positive query for this boosting query.
70
+ */
71
+ public QueryBuilder positive () {
72
+ return this .positiveQuery ;
73
+ }
74
+
75
+ /**
76
+ * Add the negative query for this boosting query.
77
+ */
60
78
public BoostingQueryBuilder negative (QueryBuilder negativeQuery ) {
61
79
this .negativeQuery = negativeQuery ;
62
80
return this ;
63
81
}
64
82
83
+ /**
84
+ * Get the negative query for this boosting query.
85
+ */
86
+ public QueryBuilder negative () {
87
+ return this .negativeQuery ;
88
+ }
89
+
90
+ /**
91
+ * Set the negative boost factor.
92
+ */
65
93
public BoostingQueryBuilder negativeBoost (float negativeBoost ) {
66
94
this .negativeBoost = negativeBoost ;
67
95
return this ;
68
96
}
69
97
98
+ /**
99
+ * Get the negative boost factor.
100
+ */
101
+ public float negativeBoost () {
102
+ return this .negativeBoost ;
103
+ }
104
+
105
+ /**
106
+ * Set the boost factor.
107
+ */
70
108
@ Override
71
109
public BoostingQueryBuilder boost (float boost ) {
72
110
this .boost = boost ;
73
111
return this ;
74
112
}
75
113
114
+ /**
115
+ * Get the boost factor.
116
+ */
117
+ public float boost () {
118
+ return this .boost ;
119
+ }
120
+
76
121
@ Override
77
122
protected void doXContent (XContentBuilder builder , Params params ) throws IOException {
78
123
if (positiveQuery == null ) {
@@ -81,25 +126,87 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
81
126
if (negativeQuery == null ) {
82
127
throw new IllegalArgumentException ("boosting query requires negative query to be set" );
83
128
}
84
- if (negativeBoost == -1 ) {
85
- throw new IllegalArgumentException ("boosting query requires negativeBoost to be set" );
86
- }
87
129
builder .startObject (NAME );
88
130
builder .field ("positive" );
89
131
positiveQuery .toXContent (builder , params );
90
132
builder .field ("negative" );
91
133
negativeQuery .toXContent (builder , params );
92
-
93
134
builder .field ("negative_boost" , negativeBoost );
94
-
95
- if (boost != -1 ) {
96
- builder .field ("boost" , boost );
97
- }
135
+ builder .field ("boost" , boost );
98
136
builder .endObject ();
99
137
}
100
138
139
+ @ Override
140
+ public QueryValidationException validate () {
141
+ QueryValidationException validationException = null ;
142
+ if (negativeBoost < 0 ) {
143
+ validationException = QueryValidationException
144
+ .addValidationError ("[boosting] query requires negativeBoost to be set to positive value" , validationException );
145
+ }
146
+ return validationException ;
147
+ };
148
+
101
149
@ Override
102
150
public String queryId () {
103
151
return NAME ;
104
152
}
153
+
154
+ @ Override
155
+ public Query toQuery (QueryParseContext parseContext ) throws QueryParsingException , IOException {
156
+
157
+ // make upstream queries ignore this query by returning `null`
158
+ // if either inner query builder is null or returns null-Query
159
+ if (positiveQuery == null || negativeQuery == null ) {
160
+ return null ;
161
+ }
162
+ Query positive = positiveQuery .toQuery (parseContext );
163
+ Query negative = negativeQuery .toQuery (parseContext );
164
+ if (positive == null || negative == null ) {
165
+ return null ;
166
+ }
167
+
168
+ BoostingQuery boostingQuery = new BoostingQuery (positive , negative , negativeBoost );
169
+ boostingQuery .setBoost (boost );
170
+ return boostingQuery ;
171
+ }
172
+
173
+ @ Override
174
+ public int hashCode () {
175
+ return Objects .hash (this .boost , this .negativeBoost , this .positiveQuery , this .negativeQuery );
176
+ }
177
+
178
+ @ Override
179
+ public boolean equals (Object obj ) {
180
+ if (this == obj ) {
181
+ return true ;
182
+ }
183
+ if (obj == null || getClass () != obj .getClass ()) {
184
+ return false ;
185
+ }
186
+ BoostingQueryBuilder other = (BoostingQueryBuilder ) obj ;
187
+ return Objects .equals (this .boost , other .boost ) &&
188
+ Objects .equals (this .negativeBoost , other .negativeBoost ) &&
189
+ Objects .equals (this .positiveQuery , other .positiveQuery ) &&
190
+ Objects .equals (this .negativeQuery , other .negativeQuery );
191
+ }
192
+
193
+ @ Override
194
+ public BoostingQueryBuilder readFrom (StreamInput in ) throws IOException {
195
+ QueryBuilder positiveQuery = in .readNamedWriteable ();
196
+ QueryBuilder negativeQuery = in .readNamedWriteable ();
197
+ BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder ();
198
+ boostingQuery .positive (positiveQuery );
199
+ boostingQuery .negative (negativeQuery );
200
+ boostingQuery .boost = in .readFloat ();
201
+ boostingQuery .negativeBoost = in .readFloat ();
202
+ return boostingQuery ;
203
+ }
204
+
205
+ @ Override
206
+ public void writeTo (StreamOutput out ) throws IOException {
207
+ out .writeNamedWriteable (this .positiveQuery );
208
+ out .writeNamedWriteable (this .negativeQuery );
209
+ out .writeFloat (this .boost );
210
+ out .writeFloat (this .negativeBoost );
211
+ }
105
212
}
0 commit comments