19
19
20
20
package org .elasticsearch .index .search ;
21
21
22
+ import org .elasticsearch .ElasticsearchParseException ;
22
23
import org .elasticsearch .common .regex .Regex ;
23
- import org .elasticsearch .index .mapper .DateFieldMapper ;
24
- import org .elasticsearch .index .mapper .DocumentMapper ;
25
- import org .elasticsearch .index .mapper .FieldMapper ;
26
- import org .elasticsearch .index .mapper .IpFieldMapper ;
27
- import org .elasticsearch .index .mapper .KeywordFieldMapper ;
28
24
import org .elasticsearch .index .mapper .MappedFieldType ;
29
- import org .elasticsearch .index .mapper .Mapper ;
30
- import org .elasticsearch .index .mapper .MapperService ;
31
- import org .elasticsearch .index .mapper .MetadataFieldMapper ;
32
- import org .elasticsearch .index .mapper .NumberFieldMapper ;
33
- import org .elasticsearch .index .mapper .TextFieldMapper ;
34
25
import org .elasticsearch .index .query .QueryShardContext ;
26
+ import org .elasticsearch .index .query .QueryShardException ;
35
27
36
28
import java .util .Collection ;
37
29
import java .util .HashMap ;
38
- import java .util .HashSet ;
39
30
import java .util .List ;
40
31
import java .util .Map ;
41
- import java .util .Set ;
42
32
43
33
/**
44
34
* Helpers to extract and expand field names and boosts
45
35
*/
46
36
public final class QueryParserHelper {
47
- // Mapping types the "all-ish" query can be executed against
48
- // TODO: Fix the API so that we don't need a hardcoded list of types
49
- private static final Set <String > ALLOWED_QUERY_MAPPER_TYPES ;
50
-
51
- static {
52
- ALLOWED_QUERY_MAPPER_TYPES = new HashSet <>();
53
- ALLOWED_QUERY_MAPPER_TYPES .add (DateFieldMapper .CONTENT_TYPE );
54
- ALLOWED_QUERY_MAPPER_TYPES .add (IpFieldMapper .CONTENT_TYPE );
55
- ALLOWED_QUERY_MAPPER_TYPES .add (KeywordFieldMapper .CONTENT_TYPE );
56
- for (NumberFieldMapper .NumberType nt : NumberFieldMapper .NumberType .values ()) {
57
- ALLOWED_QUERY_MAPPER_TYPES .add (nt .typeName ());
58
- }
59
- ALLOWED_QUERY_MAPPER_TYPES .add ("scaled_float" );
60
- ALLOWED_QUERY_MAPPER_TYPES .add (TextFieldMapper .CONTENT_TYPE );
61
- }
62
-
63
37
private QueryParserHelper () {}
64
38
65
39
/**
@@ -85,21 +59,6 @@ public static Map<String, Float> parseFieldsAndWeights(List<String> fields) {
85
59
return fieldsAndWeights ;
86
60
}
87
61
88
- /**
89
- * Get a {@link FieldMapper} associated with a field name or null.
90
- * @param mapperService The mapper service where to find the mapping.
91
- * @param field The field name to search.
92
- */
93
- public static Mapper getFieldMapper (MapperService mapperService , String field ) {
94
- for (DocumentMapper mapper : mapperService .docMappers (true )) {
95
- Mapper fieldMapper = mapper .mappers ().getMapper (field );
96
- if (fieldMapper != null ) {
97
- return fieldMapper ;
98
- }
99
- }
100
- return null ;
101
- }
102
-
103
62
public static Map <String , Float > resolveMappingFields (QueryShardContext context ,
104
63
Map <String , Float > fieldsAndWeights ) {
105
64
return resolveMappingFields (context , fieldsAndWeights , null );
@@ -136,8 +95,7 @@ public static Map<String, Float> resolveMappingFields(QueryShardContext context,
136
95
* @param fieldOrPattern The field name or the pattern to resolve
137
96
* @param weight The weight for the field
138
97
* @param acceptAllTypes Whether all field type should be added when a pattern is expanded.
139
- * If false, only {@link #ALLOWED_QUERY_MAPPER_TYPES} are accepted and other field types
140
- * are discarded from the query.
98
+ * If false, only searchable field types are added.
141
99
* @param acceptMetadataField Whether metadata fields should be added when a pattern is expanded.
142
100
*/
143
101
public static Map <String , Float > resolveMappingField (QueryShardContext context , String fieldOrPattern , float weight ,
@@ -152,8 +110,7 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
152
110
* @param fieldOrPattern The field name or the pattern to resolve
153
111
* @param weight The weight for the field
154
112
* @param acceptAllTypes Whether all field type should be added when a pattern is expanded.
155
- * If false, only {@link #ALLOWED_QUERY_MAPPER_TYPES} are accepted and other field types
156
- * are discarded from the query.
113
+ * If false, only searchable field types are added.
157
114
* @param acceptMetadataField Whether metadata fields should be added when a pattern is expanded.
158
115
* @param fieldSuffix The suffix name to add to the expanded field names if a mapping exists for that name.
159
116
* The original name of the field is kept if adding the suffix to the field name does not point to a valid field
@@ -175,18 +132,20 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
175
132
continue ;
176
133
}
177
134
178
- // Ignore fields that are not in the allowed mapper types. Some
179
- // types do not support term queries, and thus we cannot generate
180
- // a special query for them.
181
- String mappingType = fieldType .typeName ();
182
- if (acceptAllTypes == false && ALLOWED_QUERY_MAPPER_TYPES .contains (mappingType ) == false ) {
135
+ if (acceptMetadataField == false && fieldType .name ().startsWith ("_" )) {
136
+ // Ignore metadata fields
183
137
continue ;
184
138
}
185
139
186
- // Ignore metadata fields.
187
- Mapper mapper = getFieldMapper (context .getMapperService (), fieldName );
188
- if (acceptMetadataField == false && mapper instanceof MetadataFieldMapper ) {
189
- continue ;
140
+ if (acceptAllTypes == false ) {
141
+ try {
142
+ fieldType .termQuery ("" , context );
143
+ } catch (QueryShardException |UnsupportedOperationException e ) {
144
+ // field type is never searchable with term queries (eg. geo point): ignore
145
+ continue ;
146
+ } catch (IllegalArgumentException |ElasticsearchParseException e ) {
147
+ // other exceptions are parsing errors or not indexed fields: keep
148
+ }
190
149
}
191
150
fields .put (fieldName , weight );
192
151
}
0 commit comments