/
FieldQueryBuilder.java
326 lines (286 loc) · 10.5 KB
/
FieldQueryBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.query;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @author kimchy (shay.banon)
*/
public class FieldQueryBuilder extends BaseQueryBuilder {
public static enum Operator {
OR,
AND
}
private final String name;
private final Object query;
private Operator defaultOperator;
private String analyzer;
private Boolean autoGeneratePhraseQueries;
private Boolean allowLeadingWildcard;
private Boolean lowercaseExpandedTerms;
private Boolean enablePositionIncrements;
private Boolean analyzeWildcard;
private float fuzzyMinSim = -1;
private float boost = -1;
private int fuzzyPrefixLength = -1;
private int phraseSlop = -1;
private boolean extraSet = false;
private String rewrite;
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, String query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, int query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, long query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, float query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, double query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, boolean query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, Object query) {
this.name = name;
this.query = query;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
public FieldQueryBuilder boost(float boost) {
this.boost = boost;
extraSet = true;
return this;
}
/**
* Sets the boolean operator of the query parser used to parse the query string.
*
* <p>In default mode ({@link FieldQueryBuilder.Operator#OR}) terms without any modifiers
* are considered optional: for example <code>capital of Hungary</code> is equal to
* <code>capital OR of OR Hungary</code>.
*
* <p>In {@link FieldQueryBuilder.Operator#AND} mode terms are considered to be in conjunction: the
* above mentioned query is parsed as <code>capital AND of AND Hungary</code>
*/
public FieldQueryBuilder defaultOperator(Operator defaultOperator) {
this.defaultOperator = defaultOperator;
extraSet = true;
return this;
}
/**
* The optional analyzer used to analyze the query string. Note, if a field has search analyzer
* defined for it, then it will be used automatically. Defaults to the smart search analyzer.
*/
public FieldQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
extraSet = true;
return this;
}
/**
* Set to true if phrase queries will be automatically generated
* when the analyzer returns more than one term from whitespace
* delimited text.
* NOTE: this behavior may not be suitable for all languages.
* <p>
* Set to false if phrase queries should only be generated when
* surrounded by double quotes.
*/
public void autoGeneratePhraseQueries(boolean autoGeneratePhraseQueries) {
this.autoGeneratePhraseQueries = autoGeneratePhraseQueries;
}
/**
* Should leading wildcards be allowed or not. Defaults to <tt>true</tt>.
*/
public FieldQueryBuilder allowLeadingWildcard(boolean allowLeadingWildcard) {
this.allowLeadingWildcard = allowLeadingWildcard;
extraSet = true;
return this;
}
/**
* Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
* lower-cased or not. Default is <tt>true</tt>.
*/
public FieldQueryBuilder lowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
extraSet = true;
return this;
}
/**
* Set to <tt>true</tt> to enable position increments in result query. Defaults to
* <tt>true</tt>.
*
* <p>When set, result phrase and multi-phrase queries will be aware of position increments.
* Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.
*/
public FieldQueryBuilder enablePositionIncrements(boolean enablePositionIncrements) {
this.enablePositionIncrements = enablePositionIncrements;
extraSet = true;
return this;
}
/**
* Set the minimum similarity for fuzzy queries. Default is 0.5f.
*/
public FieldQueryBuilder fuzzyMinSim(float fuzzyMinSim) {
this.fuzzyMinSim = fuzzyMinSim;
extraSet = true;
return this;
}
/**
* Set the prefix length for fuzzy queries. Default is 0.
*/
public FieldQueryBuilder fuzzyPrefixLength(int fuzzyPrefixLength) {
this.fuzzyPrefixLength = fuzzyPrefixLength;
extraSet = true;
return this;
}
/**
* Sets the default slop for phrases. If zero, then exact phrase matches
* are required. Default value is zero.
*/
public FieldQueryBuilder phraseSlop(int phraseSlop) {
this.phraseSlop = phraseSlop;
extraSet = true;
return this;
}
/**
* Set to <tt>true</tt> to enable analysis on wildcard and prefix queries.
*/
public FieldQueryBuilder analyzeWildcard(boolean analyzeWildcard) {
this.analyzeWildcard = analyzeWildcard;
extraSet = true;
return this;
}
public FieldQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite;
extraSet = true;
return this;
}
@Override public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(FieldQueryParser.NAME);
if (!extraSet) {
builder.field(name, query);
} else {
builder.startObject(name);
builder.field("query", query);
if (defaultOperator != null) {
builder.field("default_operator", defaultOperator.name().toLowerCase());
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (autoGeneratePhraseQueries != null) {
builder.field("auto_generate_phrase_queries", autoGeneratePhraseQueries);
}
if (allowLeadingWildcard != null) {
builder.field("allow_leading_wildcard", allowLeadingWildcard);
}
if (lowercaseExpandedTerms != null) {
builder.field("lowercase_expanded_terms", lowercaseExpandedTerms);
}
if (enablePositionIncrements != null) {
builder.field("enable_position_increments", enablePositionIncrements);
}
if (fuzzyMinSim != -1) {
builder.field("fuzzy_min_sim", fuzzyMinSim);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (fuzzyPrefixLength != -1) {
builder.field("fuzzy_prefix_length", fuzzyPrefixLength);
}
if (phraseSlop != -1) {
builder.field("phrase_slop", phraseSlop);
}
if (analyzeWildcard != null) {
builder.field("analyze_wildcard", analyzeWildcard);
}
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
builder.endObject();
}
builder.endObject();
}
}