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 .WildcardQuery ;
26
+ import org .apache .lucene .util .BytesRef ;
27
+ import org .elasticsearch .common .Strings ;
28
+ import org .elasticsearch .common .io .stream .StreamInput ;
29
+ import org .elasticsearch .common .io .stream .StreamOutput ;
22
30
import org .elasticsearch .common .xcontent .XContentBuilder ;
31
+ import org .elasticsearch .index .mapper .MappedFieldType ;
32
+ import org .elasticsearch .index .query .support .QueryParsers ;
23
33
24
34
import java .io .IOException ;
35
+ import java .util .Objects ;
25
36
26
37
/**
27
38
* Implements the wildcard search query. Supported wildcards are <tt>*</tt>, which
@@ -35,9 +46,9 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
35
46
36
47
public static final String NAME = "wildcard" ;
37
48
38
- private final String name ;
49
+ private final String fieldName ;
39
50
40
- private final String wildcard ;
51
+ private final String value ;
41
52
42
53
private String rewrite ;
43
54
@@ -51,24 +62,41 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
51
62
* a Wildcard term should not start with one of the wildcards <tt>*</tt> or
52
63
* <tt>?</tt>.
53
64
*
54
- * @param name The field name
55
- * @param wildcard The wildcard query string
65
+ * @param fieldName The field name
66
+ * @param value The wildcard query string
56
67
*/
57
- public WildcardQueryBuilder (String name , String wildcard ) {
58
- this .name = name ;
59
- this .wildcard = wildcard ;
68
+ public WildcardQueryBuilder (String fieldName , String value ) {
69
+ this .fieldName = fieldName ;
70
+ this .value = value ;
71
+ }
72
+
73
+ public String fieldName () {
74
+ return fieldName ;
75
+ }
76
+
77
+ public String value () {
78
+ return value ;
60
79
}
61
80
62
81
public WildcardQueryBuilder rewrite (String rewrite ) {
63
82
this .rewrite = rewrite ;
64
83
return this ;
65
84
}
66
85
86
+ public String rewrite () {
87
+ return this .rewrite ;
88
+ }
89
+
90
+ @ Override
91
+ public String getName () {
92
+ return NAME ;
93
+ }
94
+
67
95
@ Override
68
96
public void doXContent (XContentBuilder builder , Params params ) throws IOException {
69
97
builder .startObject (NAME );
70
- builder .startObject (name );
71
- builder .field ("wildcard" , wildcard );
98
+ builder .startObject (fieldName );
99
+ builder .field ("wildcard" , value );
72
100
if (rewrite != null ) {
73
101
builder .field ("rewrite" , rewrite );
74
102
}
@@ -78,7 +106,60 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
78
106
}
79
107
80
108
@ Override
81
- public String getName () {
82
- return NAME ;
109
+ protected Query doToQuery (QueryParseContext parseContext ) throws IOException {
110
+ String indexFieldName ;
111
+ BytesRef valueBytes ;
112
+
113
+ MappedFieldType fieldType = parseContext .fieldMapper (fieldName );
114
+ if (fieldType != null ) {
115
+ indexFieldName = fieldType .names ().indexName ();
116
+ valueBytes = fieldType .indexedValueForSearch (value );
117
+ } else {
118
+ indexFieldName = fieldName ;
119
+ valueBytes = new BytesRef (value );
120
+ }
121
+
122
+ WildcardQuery query = new WildcardQuery (new Term (indexFieldName , valueBytes ));
123
+ MultiTermQuery .RewriteMethod rewriteMethod = QueryParsers .parseRewriteMethod (parseContext .parseFieldMatcher (), rewrite , null );
124
+ QueryParsers .setRewriteMethod (query , rewriteMethod );
125
+ return query ;
126
+ }
127
+
128
+ @ Override
129
+ public QueryValidationException validate () {
130
+ QueryValidationException validationException = null ;
131
+ if (Strings .isEmpty (this .fieldName )) {
132
+ validationException = addValidationError ("field name cannot be null or empty." , validationException );
133
+ }
134
+ if (this .value == null ) {
135
+ validationException = addValidationError ("wildcard cannot be null" , validationException );
136
+ }
137
+ return validationException ;
138
+ }
139
+
140
+ @ Override
141
+ protected WildcardQueryBuilder doReadFrom (StreamInput in ) throws IOException {
142
+ WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder (in .readString (), in .readString ());
143
+ wildcardQueryBuilder .rewrite = in .readOptionalString ();
144
+ return wildcardQueryBuilder ;
145
+ }
146
+
147
+ @ Override
148
+ protected void doWriteTo (StreamOutput out ) throws IOException {
149
+ out .writeString (fieldName );
150
+ out .writeString (value );
151
+ out .writeOptionalString (rewrite );
152
+ }
153
+
154
+ @ Override
155
+ protected int doHashCode () {
156
+ return Objects .hash (fieldName , value , rewrite );
157
+ }
158
+
159
+ @ Override
160
+ protected boolean doEquals (WildcardQueryBuilder other ) {
161
+ return Objects .equals (fieldName , other .fieldName ) &&
162
+ Objects .equals (value , other .value ) &&
163
+ Objects .equals (rewrite , other .rewrite );
83
164
}
84
165
}
0 commit comments