19
19
20
20
package org .elasticsearch .index .query ;
21
21
22
+ import org .apache .lucene .index .Term ;
23
+ import org .apache .lucene .search .MultiTermQuery ;
24
+ import org .apache .lucene .search .Query ;
25
+ import org .apache .lucene .search .RegexpQuery ;
22
26
import org .apache .lucene .util .automaton .Operations ;
27
+ import org .elasticsearch .common .Strings ;
28
+ import org .elasticsearch .common .io .stream .StreamInput ;
29
+ import org .elasticsearch .common .io .stream .StreamOutput ;
30
+ import org .elasticsearch .common .lucene .BytesRefs ;
23
31
import org .elasticsearch .common .xcontent .XContentBuilder ;
32
+ import org .elasticsearch .index .mapper .MappedFieldType ;
33
+ import org .elasticsearch .index .query .support .QueryParsers ;
24
34
25
35
import java .io .IOException ;
36
+ import java .util .Objects ;
26
37
27
38
/**
28
39
* A Query that does fuzzy matching for a specific value.
29
40
*/
30
41
public class RegexpQueryBuilder extends AbstractQueryBuilder <RegexpQueryBuilder > implements MultiTermQueryBuilder <RegexpQueryBuilder > {
31
42
32
43
public static final String NAME = "regexp" ;
33
- private final String name ;
34
- private final String regexp ;
35
44
36
- private int flags = RegexpQueryParser .DEFAULT_FLAGS_VALUE ;
45
+ public static final int DEFAULT_FLAGS_VALUE = RegexpFlag .ALL .value ();
46
+
47
+ public static final int DEFAULT_MAX_DETERMINIZED_STATES = Operations .DEFAULT_MAX_DETERMINIZED_STATES ;
48
+
49
+ private final String fieldName ;
50
+
51
+ private final String value ;
52
+
53
+ private int flagsValue = DEFAULT_FLAGS_VALUE ;
54
+
55
+ private int maxDeterminizedStates = DEFAULT_MAX_DETERMINIZED_STATES ;
37
56
38
57
private String rewrite ;
39
- private int maxDeterminizedStates = Operations .DEFAULT_MAX_DETERMINIZED_STATES ;
40
- private boolean maxDetermizedStatesSet ;
58
+
41
59
static final RegexpQueryBuilder PROTOTYPE = new RegexpQueryBuilder (null , null );
42
60
43
61
/**
44
- * Constructs a new term query.
45
- *
46
- * @param name The name of the field
47
- * @param regexp The regular expression
62
+ * Constructs a new regex query.
63
+ *
64
+ * @param fieldName The name of the field
65
+ * @param value The regular expression
66
+ */
67
+ public RegexpQueryBuilder (String fieldName , String value ) {
68
+ this .fieldName = fieldName ;
69
+ this .value = value ;
70
+ }
71
+
72
+ /** Returns the field name used in this query. */
73
+ public String fieldName () {
74
+ return this .fieldName ;
75
+ }
76
+
77
+ /**
78
+ * Returns the value used in this query.
48
79
*/
49
- public RegexpQueryBuilder (String name , String regexp ) {
50
- this .name = name ;
51
- this .regexp = regexp ;
80
+ public String value () {
81
+ return this .value ;
52
82
}
53
83
54
84
public RegexpQueryBuilder flags (RegexpFlag ... flags ) {
85
+ if (flags == null ) {
86
+ this .flagsValue = DEFAULT_FLAGS_VALUE ;
87
+ return this ;
88
+ }
55
89
int value = 0 ;
56
90
if (flags .length == 0 ) {
57
91
value = RegexpFlag .ALL .value ;
@@ -60,35 +94,47 @@ public RegexpQueryBuilder flags(RegexpFlag... flags) {
60
94
value |= flag .value ;
61
95
}
62
96
}
63
- this .flags = value ;
97
+ this .flagsValue = value ;
98
+ return this ;
99
+ }
100
+
101
+ public RegexpQueryBuilder flags (int flags ) {
102
+ this .flagsValue = flags ;
64
103
return this ;
65
104
}
66
105
106
+ public int flags () {
107
+ return this .flagsValue ;
108
+ }
109
+
67
110
/**
68
111
* Sets the regexp maxDeterminizedStates.
69
112
*/
70
113
public RegexpQueryBuilder maxDeterminizedStates (int value ) {
71
114
this .maxDeterminizedStates = value ;
72
- this .maxDetermizedStatesSet = true ;
73
115
return this ;
74
116
}
117
+
118
+ public int maxDeterminizedStates () {
119
+ return this .maxDeterminizedStates ;
120
+ }
75
121
76
122
public RegexpQueryBuilder rewrite (String rewrite ) {
77
123
this .rewrite = rewrite ;
78
124
return this ;
79
125
}
126
+
127
+ public String rewrite () {
128
+ return this .rewrite ;
129
+ }
80
130
81
131
@ Override
82
132
public void doXContent (XContentBuilder builder , Params params ) throws IOException {
83
133
builder .startObject (NAME );
84
- builder .startObject (name );
85
- builder .field ("value" , regexp );
86
- if (flags != -1 ) {
87
- builder .field ("flags_value" , flags );
88
- }
89
- if (maxDetermizedStatesSet ) {
90
- builder .field ("max_determinized_states" , maxDeterminizedStates );
91
- }
134
+ builder .startObject (fieldName );
135
+ builder .field ("value" , this .value );
136
+ builder .field ("flags_value" , flagsValue );
137
+ builder .field ("max_determinized_states" , maxDeterminizedStates );
92
138
if (rewrite != null ) {
93
139
builder .field ("rewrite" , rewrite );
94
140
}
@@ -101,4 +147,67 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
101
147
public String getName () {
102
148
return NAME ;
103
149
}
150
+
151
+ @ Override
152
+ public Query doToQuery (QueryParseContext parseContext ) throws QueryParsingException , IOException {
153
+ MultiTermQuery .RewriteMethod method = QueryParsers .parseRewriteMethod (parseContext .parseFieldMatcher (), rewrite , null );
154
+
155
+ Query query = null ;
156
+ MappedFieldType fieldType = parseContext .fieldMapper (fieldName );
157
+ if (fieldType != null ) {
158
+ query = fieldType .regexpQuery (value , flagsValue , maxDeterminizedStates , method , parseContext );
159
+ }
160
+ if (query == null ) {
161
+ RegexpQuery regexpQuery = new RegexpQuery (new Term (fieldName , BytesRefs .toBytesRef (value )), flagsValue , maxDeterminizedStates );
162
+ if (method != null ) {
163
+ regexpQuery .setRewriteMethod (method );
164
+ }
165
+ query = regexpQuery ;
166
+ }
167
+ return query ;
168
+ }
169
+
170
+ @ Override
171
+ public QueryValidationException validate () {
172
+ QueryValidationException validationException = null ;
173
+ if (Strings .isEmpty (this .fieldName )) {
174
+ validationException = addValidationError ("field name cannot be null or empty." , validationException );
175
+ }
176
+ if (this .value == null ) {
177
+ validationException = addValidationError ("query text cannot be null" , validationException );
178
+ }
179
+ return validationException ;
180
+ }
181
+
182
+ @ Override
183
+ public RegexpQueryBuilder doReadFrom (StreamInput in ) throws IOException {
184
+ RegexpQueryBuilder regexpQueryBuilder = new RegexpQueryBuilder (in .readString (), in .readString ());
185
+ regexpQueryBuilder .flagsValue = in .readVInt ();
186
+ regexpQueryBuilder .maxDeterminizedStates = in .readVInt ();
187
+ regexpQueryBuilder .rewrite = in .readOptionalString ();
188
+ return regexpQueryBuilder ;
189
+ }
190
+
191
+ @ Override
192
+ public void doWriteTo (StreamOutput out ) throws IOException {
193
+ out .writeString (fieldName );
194
+ out .writeString (value );
195
+ out .writeVInt (flagsValue );
196
+ out .writeVInt (maxDeterminizedStates );
197
+ out .writeOptionalString (rewrite );
198
+ }
199
+
200
+ @ Override
201
+ public int doHashCode () {
202
+ return Objects .hash (fieldName , value , flagsValue , maxDeterminizedStates , rewrite );
203
+ }
204
+
205
+ @ Override
206
+ public boolean doEquals (RegexpQueryBuilder other ) {
207
+ return Objects .equals (fieldName , other .fieldName ) &&
208
+ Objects .equals (value , other .value ) &&
209
+ Objects .equals (flagsValue , other .flagsValue ) &&
210
+ Objects .equals (maxDeterminizedStates , other .maxDeterminizedStates ) &&
211
+ Objects .equals (rewrite , other .rewrite );
212
+ }
104
213
}
0 commit comments