27
27
import org .elasticsearch .common .regex .Regex ;
28
28
import org .elasticsearch .common .unit .Fuzziness ;
29
29
import org .elasticsearch .common .xcontent .XContentParser ;
30
+ import org .elasticsearch .index .mapper .MapperService ;
30
31
import org .elasticsearch .index .query .support .QueryParsers ;
31
32
import org .elasticsearch .index .search .MatchQuery ;
32
33
import org .elasticsearch .index .search .MultiMatchQuery ;
33
34
34
35
import java .io .IOException ;
35
36
import java .util .Map ;
37
+ import java .util .TreeMap ;
36
38
37
39
/**
38
40
* Same as {@link MatchQueryParser} but has support for multiple fields.
@@ -73,10 +75,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
73
75
} else if ("fields" .equals (currentFieldName )) {
74
76
if (token == XContentParser .Token .START_ARRAY ) {
75
77
while ((token = parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
76
- extractFieldAndBoost ( parseContext , parser , fieldNameWithBoosts );
78
+ parseFieldAndBoost ( parser , fieldNameWithBoosts );
77
79
}
78
80
} else if (token .isValue ()) {
79
- extractFieldAndBoost ( parseContext , parser , fieldNameWithBoosts );
81
+ parseFieldAndBoost ( parser , fieldNameWithBoosts );
80
82
} else {
81
83
throw new QueryParsingException (parseContext , "[" + NAME + "] query does not support [" + currentFieldName + "]" );
82
84
}
@@ -160,7 +162,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
160
162
}
161
163
}
162
164
}
163
- Query query = multiMatchQuery .parse (type , fieldNameWithBoosts , value , minimumShouldMatch );
165
+
166
+ Map <String , Float > newFieldsBoosts = handleFieldsMatchPattern (parseContext .mapperService (), fieldNameWithBoosts );
167
+
168
+ Query query = multiMatchQuery .parse (type , newFieldsBoosts , value , minimumShouldMatch );
164
169
if (query == null ) {
165
170
return null ;
166
171
}
@@ -172,7 +177,23 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
172
177
return query ;
173
178
}
174
179
175
- private void extractFieldAndBoost (QueryParseContext parseContext , XContentParser parser , Map <String , Float > fieldNameWithBoosts ) throws IOException {
180
+ private static Map <String , Float > handleFieldsMatchPattern (MapperService mapperService , Map <String , Float > fieldsBoosts ) {
181
+ Map <String , Float > newFieldsBoosts = new TreeMap <>();
182
+ for (Map .Entry <String , Float > fieldBoost : fieldsBoosts .entrySet ()) {
183
+ String fField = fieldBoost .getKey ();
184
+ Float fBoost = fieldBoost .getValue ();
185
+ if (Regex .isSimpleMatchPattern (fField )) {
186
+ for (String field : mapperService .simpleMatchToIndexNames (fField )) {
187
+ newFieldsBoosts .put (field , fBoost );
188
+ }
189
+ } else {
190
+ newFieldsBoosts .put (fField , fBoost );
191
+ }
192
+ }
193
+ return newFieldsBoosts ;
194
+ }
195
+
196
+ private static void parseFieldAndBoost (XContentParser parser , Map <String , Float > fieldsBoosts ) throws IOException {
176
197
String fField = null ;
177
198
Float fBoost = null ;
178
199
char [] fieldText = parser .textCharacters ();
@@ -188,13 +209,6 @@ private void extractFieldAndBoost(QueryParseContext parseContext, XContentParser
188
209
if (fField == null ) {
189
210
fField = parser .text ();
190
211
}
191
-
192
- if (Regex .isSimpleMatchPattern (fField )) {
193
- for (String field : parseContext .mapperService ().simpleMatchToIndexNames (fField )) {
194
- fieldNameWithBoosts .put (field , fBoost );
195
- }
196
- } else {
197
- fieldNameWithBoosts .put (fField , fBoost );
198
- }
212
+ fieldsBoosts .put (fField , fBoost );
199
213
}
200
214
}
0 commit comments