19
19
20
20
package org .elasticsearch .index .query ;
21
21
22
+ import org .apache .lucene .search .ConstantScoreQuery ;
22
23
import org .apache .lucene .search .Query ;
24
+ import org .elasticsearch .common .io .stream .StreamInput ;
25
+ import org .elasticsearch .common .io .stream .StreamOutput ;
23
26
import org .elasticsearch .common .xcontent .XContentBuilder ;
24
27
25
28
import java .io .IOException ;
29
32
* A query that wraps a filter and simply returns a constant score equal to the
30
33
* query boost for every document in the filter.
31
34
*/
32
- public class ConstantScoreQueryBuilder extends QueryBuilder implements BoostableQueryBuilder <ConstantScoreQueryBuilder > {
35
+ public class ConstantScoreQueryBuilder extends QueryBuilder < ConstantScoreQueryBuilder > implements BoostableQueryBuilder <ConstantScoreQueryBuilder > {
33
36
34
37
public static final String NAME = "constant_score" ;
35
38
36
39
private final QueryBuilder filterBuilder ;
37
40
38
- private float boost = - 1 ;
41
+ private float boost = 1.0f ;
39
42
40
- static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder ();
43
+ static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder (null );
41
44
42
45
/**
43
- * A query that wraps a query and simply returns a constant score equal to the
46
+ * A query that wraps another query and simply returns a constant score equal to the
44
47
* query boost for every document in the query.
45
48
*
46
49
* @param filterBuilder The query to wrap in a constant score query
47
50
*/
48
51
public ConstantScoreQueryBuilder (QueryBuilder filterBuilder ) {
49
- this .filterBuilder = Objects . requireNonNull ( filterBuilder ) ;
52
+ this .filterBuilder = filterBuilder ;
50
53
}
51
54
52
55
/**
53
- * private constructor only used for serialization
56
+ * @return the query that was wrapped in this constant score query
54
57
*/
55
- private ConstantScoreQueryBuilder () {
56
- this .filterBuilder = null ;
58
+ public QueryBuilder query () {
59
+ return this .filterBuilder ;
57
60
}
58
61
59
62
/**
@@ -66,20 +69,74 @@ public ConstantScoreQueryBuilder boost(float boost) {
66
69
return this ;
67
70
}
68
71
72
+ /**
73
+ * @return the boost factor
74
+ */
75
+ public float boost () {
76
+ return this .boost ;
77
+ }
78
+
69
79
@ Override
70
80
protected void doXContent (XContentBuilder builder , Params params ) throws IOException {
71
81
builder .startObject (NAME );
72
82
builder .field ("filter" );
73
83
filterBuilder .toXContent (builder , params );
84
+ builder .field ("boost" , boost );
85
+ builder .endObject ();
86
+ }
74
87
75
- if (boost != -1 ) {
76
- builder .field ("boost" , boost );
88
+ @ Override
89
+ public Query toQuery (QueryParseContext parseContext ) throws QueryParsingException , IOException {
90
+ // current DSL allows empty inner filter clauses, we ignore them
91
+ if (filterBuilder == null ) {
92
+ return null ;
77
93
}
78
- builder .endObject ();
94
+
95
+ Query innerFilter = filterBuilder .toQuery (parseContext );
96
+ if (innerFilter == null ) {
97
+ // return null so that parent queries (e.g. bool) also ignore this
98
+ return null ;
99
+ }
100
+
101
+ Query filter = new ConstantScoreQuery (filterBuilder .toQuery (parseContext ));
102
+ filter .setBoost (boost );
103
+ return filter ;
79
104
}
80
105
81
106
@ Override
82
107
public String queryId () {
83
108
return NAME ;
84
109
}
110
+
111
+ @ Override
112
+ public int hashCode () {
113
+ return Objects .hash (boost , filterBuilder );
114
+ }
115
+
116
+ @ Override
117
+ public boolean equals (Object obj ) {
118
+ if (this == obj ) {
119
+ return true ;
120
+ }
121
+ if (obj == null || getClass () != obj .getClass ()) {
122
+ return false ;
123
+ }
124
+ ConstantScoreQueryBuilder other = (ConstantScoreQueryBuilder ) obj ;
125
+ return Objects .equals (boost , other .boost ) &&
126
+ Objects .equals (filterBuilder , other .filterBuilder );
127
+ }
128
+
129
+ @ Override
130
+ public ConstantScoreQueryBuilder readFrom (StreamInput in ) throws IOException {
131
+ QueryBuilder innerFilterBuilder = in .readNamedWriteable ();
132
+ ConstantScoreQueryBuilder constantScoreQueryBuilder = new ConstantScoreQueryBuilder (innerFilterBuilder );
133
+ constantScoreQueryBuilder .boost = in .readFloat ();
134
+ return constantScoreQueryBuilder ;
135
+ }
136
+
137
+ @ Override
138
+ public void writeTo (StreamOutput out ) throws IOException {
139
+ out .writeNamedWriteable (this .filterBuilder );
140
+ out .writeFloat (boost );
141
+ }
85
142
}
0 commit comments