1919
2020package org .elasticsearch .index .search ;
2121
22+ import org .elasticsearch .ElasticsearchParseException ;
2223import 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 ;
2824import 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 ;
3425import org .elasticsearch .index .query .QueryShardContext ;
26+ import org .elasticsearch .index .query .QueryShardException ;
3527
3628import java .util .Collection ;
3729import java .util .HashMap ;
38- import java .util .HashSet ;
3930import java .util .List ;
4031import java .util .Map ;
41- import java .util .Set ;
4232
4333/**
4434 * Helpers to extract and expand field names and boosts
4535 */
4636public 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-
6337 private QueryParserHelper () {}
6438
6539 /**
@@ -85,22 +59,6 @@ public static Map<String, Float> parseFieldsAndWeights(List<String> fields) {
8559 return fieldsAndWeights ;
8660 }
8761
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- DocumentMapper mapper = mapperService .documentMapper ();
95- if (mapper != null ) {
96- Mapper fieldMapper = mapper .mappers ().getMapper (field );
97- if (fieldMapper != null ) {
98- return fieldMapper ;
99- }
100- }
101- return null ;
102- }
103-
10462 public static Map <String , Float > resolveMappingFields (QueryShardContext context ,
10563 Map <String , Float > fieldsAndWeights ) {
10664 return resolveMappingFields (context , fieldsAndWeights , null );
@@ -138,8 +96,7 @@ public static Map<String, Float> resolveMappingFields(QueryShardContext context,
13896 * @param fieldOrPattern The field name or the pattern to resolve
13997 * @param weight The weight for the field
14098 * @param acceptAllTypes Whether all field type should be added when a pattern is expanded.
141- * If false, only {@link #ALLOWED_QUERY_MAPPER_TYPES} are accepted and other field types
142- * are discarded from the query.
99+ * If false, only searchable field types are added.
143100 * @param acceptMetadataField Whether metadata fields should be added when a pattern is expanded.
144101 */
145102 public static Map <String , Float > resolveMappingField (QueryShardContext context , String fieldOrPattern , float weight ,
@@ -154,8 +111,7 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
154111 * @param fieldOrPattern The field name or the pattern to resolve
155112 * @param weight The weight for the field
156113 * @param acceptAllTypes Whether all field type should be added when a pattern is expanded.
157- * If false, only {@link #ALLOWED_QUERY_MAPPER_TYPES} are accepted and other field types
158- * are discarded from the query.
114+ * If false, only searchable field types are added.
159115 * @param acceptMetadataField Whether metadata fields should be added when a pattern is expanded.
160116 * @param fieldSuffix The suffix name to add to the expanded field names if a mapping exists for that name.
161117 * The original name of the field is kept if adding the suffix to the field name does not point to a valid field
@@ -177,18 +133,20 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
177133 continue ;
178134 }
179135
180- // Ignore fields that are not in the allowed mapper types. Some
181- // types do not support term queries, and thus we cannot generate
182- // a special query for them.
183- String mappingType = fieldType .typeName ();
184- if (acceptAllTypes == false && ALLOWED_QUERY_MAPPER_TYPES .contains (mappingType ) == false ) {
136+ if (acceptMetadataField == false && fieldType .name ().startsWith ("_" )) {
137+ // Ignore metadata fields
185138 continue ;
186139 }
187140
188- // Ignore metadata fields.
189- Mapper mapper = getFieldMapper (context .getMapperService (), fieldName );
190- if (acceptMetadataField == false && mapper instanceof MetadataFieldMapper ) {
191- continue ;
141+ if (acceptAllTypes == false ) {
142+ try {
143+ fieldType .termQuery ("" , context );
144+ } catch (QueryShardException |UnsupportedOperationException e ) {
145+ // field type is never searchable with term queries (eg. geo point): ignore
146+ continue ;
147+ } catch (IllegalArgumentException |ElasticsearchParseException e ) {
148+ // other exceptions are parsing errors or not indexed fields: keep
149+ }
192150 }
193151 fields .put (fieldName , weight );
194152 }
0 commit comments