19
19
20
20
package org .elasticsearch .index .query ;
21
21
22
+ import com .google .common .collect .Iterables ;
23
+
24
+ import org .apache .lucene .queries .TermsQuery ;
25
+ import org .apache .lucene .search .Query ;
26
+ import org .apache .lucene .util .BytesRef ;
27
+ import org .elasticsearch .common .io .stream .StreamInput ;
28
+ import org .elasticsearch .common .io .stream .StreamOutput ;
29
+ import org .elasticsearch .common .io .stream .Streamable ;
30
+ import org .elasticsearch .common .lucene .search .Queries ;
22
31
import org .elasticsearch .common .xcontent .XContentBuilder ;
32
+ import org .elasticsearch .index .mapper .Uid ;
33
+ import org .elasticsearch .index .mapper .internal .UidFieldMapper ;
23
34
24
35
import java .io .IOException ;
25
36
import java .util .ArrayList ;
26
37
import java .util .Arrays ;
38
+ import java .util .Collection ;
27
39
import java .util .List ;
40
+ import java .util .Objects ;
28
41
29
42
/**
30
43
* A query that will return only documents matching specific ids (and a type).
31
44
*/
32
- public class IdsQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder <IdsQueryBuilder > {
45
+ public class IdsQueryBuilder extends BaseQueryBuilder implements Streamable , BoostableQueryBuilder <IdsQueryBuilder > {
33
46
34
- private final List <String > types ;
47
+ private List <String > types = new ArrayList <>() ;
35
48
36
- private List <String > values = new ArrayList <>();
49
+ private List <String > ids = new ArrayList <>();
37
50
38
- private float boost = - 1 ;
51
+ private float boost = 1.0f ;
39
52
40
53
private String queryName ;
41
54
42
55
public IdsQueryBuilder (String ... types ) {
43
- this .types = types == null ? null : Arrays .asList (types );
56
+ this .types = (types == null || types .length == 0 ) ? new ArrayList <String >() : Arrays .asList (types );
57
+ }
58
+
59
+ /**
60
+ * Get the types used in this query
61
+ * @return the types
62
+ */
63
+ public Collection <String > types () {
64
+ return this .types ;
44
65
}
45
66
46
67
/**
47
- * Adds ids to the filter .
68
+ * Adds ids to the query .
48
69
*/
49
70
public IdsQueryBuilder addIds (String ... ids ) {
50
- values .addAll (Arrays .asList (ids ));
71
+ this . ids .addAll (Arrays .asList (ids ));
51
72
return this ;
52
73
}
53
74
54
75
/**
55
- * Adds ids to the filter .
76
+ * Adds ids to the query .
56
77
*/
57
78
public IdsQueryBuilder ids (String ... ids ) {
58
79
return addIds (ids );
59
80
}
60
81
82
+ /**
83
+ * Gets the ids for the query.
84
+ */
85
+ public Collection <String > ids () {
86
+ return this .ids ;
87
+ }
88
+
61
89
/**
62
90
* Sets the boost for this query. Documents matching this query will (in addition to the normal
63
91
* weightings) have their score multiplied by the boost provided.
@@ -69,13 +97,27 @@ public IdsQueryBuilder boost(float boost) {
69
97
}
70
98
71
99
/**
72
- * Sets the query name for the filter that can be used when searching for matched_filters per hit.
100
+ * Gets the boost for this query.
101
+ */
102
+ public float boost () {
103
+ return this .boost ;
104
+ }
105
+
106
+ /**
107
+ * Sets the query name for the query that can be used when searching for matched_filters per hit.
73
108
*/
74
109
public IdsQueryBuilder queryName (String queryName ) {
75
110
this .queryName = queryName ;
76
111
return this ;
77
112
}
78
113
114
+ /**
115
+ * Gets the query name for the query.
116
+ */
117
+ public String queryName () {
118
+ return this .queryName ;
119
+ }
120
+
79
121
@ Override
80
122
protected void doXContent (XContentBuilder builder , Params params ) throws IOException {
81
123
builder .startObject (IdsQueryParser .NAME );
@@ -84,14 +126,14 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
84
126
builder .field ("type" , types .get (0 ));
85
127
} else {
86
128
builder .startArray ("types" );
87
- for (Object type : types ) {
129
+ for (String type : types ) {
88
130
builder .value (type );
89
131
}
90
132
builder .endArray ();
91
133
}
92
134
}
93
135
builder .startArray ("values" );
94
- for (Object value : values ) {
136
+ for (String value : ids ) {
95
137
builder .value (value );
96
138
}
97
139
builder .endArray ();
@@ -108,4 +150,66 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
108
150
protected String parserName () {
109
151
return IdsQueryParser .NAME ;
110
152
}
153
+
154
+ public Query toQuery (QueryParseContext parseContext ) throws IOException , QueryParsingException {
155
+ if (this .ids .isEmpty ()) {
156
+ return Queries .newMatchNoDocsQuery ();
157
+ }
158
+
159
+ Collection <String > typesForQuery = this .types ;
160
+ if (typesForQuery == null || typesForQuery .isEmpty ()) {
161
+ typesForQuery = parseContext .queryTypes ();
162
+ } else if (typesForQuery .size () == 1 && Iterables .getFirst (typesForQuery , null ).equals ("_all" )) {
163
+ typesForQuery = parseContext .mapperService ().types ();
164
+ }
165
+
166
+ TermsQuery query = new TermsQuery (UidFieldMapper .NAME , Uid .createTypeUids (typesForQuery , ids ));
167
+ query .setBoost (boost );
168
+ if (queryName != null ) {
169
+ parseContext .addNamedQuery (queryName , query );
170
+ }
171
+ return query ;
172
+ }
173
+
174
+ @ Override
175
+ public QueryValidationException validate () {
176
+ // all fields can be empty or null
177
+ return null ;
178
+ }
179
+
180
+ @ Override
181
+ public void readFrom (StreamInput in ) throws IOException {
182
+ this .types = in .readStringList ();
183
+ this .ids = in .readStringList ();
184
+ queryName = in .readOptionalString ();
185
+ boost = in .readFloat ();
186
+ }
187
+
188
+ @ Override
189
+ public void writeTo (StreamOutput out ) throws IOException {
190
+ out .writeStringList (this .types );
191
+ out .writeStringList (this .ids );
192
+ out .writeOptionalString (queryName );
193
+ out .writeFloat (boost );
194
+ }
195
+
196
+ @ Override
197
+ public int hashCode () {
198
+ return Objects .hash (ids , types , boost , queryName );
199
+ }
200
+
201
+ @ Override
202
+ public boolean equals (Object obj ) {
203
+ if (this == obj ) {
204
+ return true ;
205
+ }
206
+ if (obj == null || getClass () != obj .getClass ()) {
207
+ return false ;
208
+ }
209
+ IdsQueryBuilder other = (IdsQueryBuilder ) obj ;
210
+ return Objects .equals (ids , other .ids ) &&
211
+ Objects .equals (types , other .types ) &&
212
+ Objects .equals (boost , other .boost ) &&
213
+ Objects .equals (queryName , other .queryName );
214
+ }
111
215
}
0 commit comments