Skip to content

Commit e8bb9ea

Browse files
authored
Add matching pattern to error in fields (backport of #76903) (#77191)
This adds the pattern into the error message returned when trying to fetch fields. So this: ``` POST _search { "fields": [ { "field": "*", "format": "date_time" } ] } ``` Will return an error message like ``` error fetching [foo] which matches [*]: Field [foo] of type [keyword] doesn't support formats ```
1 parent 51fde90 commit e8bb9ea

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search/330_fetch_fields.yml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ Test nested field with sibling field resolving to DocValueFetcher:
855855
- match: { hits.hits.0.fields.number.2 : 3 }
856856
- match: { hits.hits.0.fields.number.3 : 5 }
857857
- match: { hits.hits.0.fields.number.4 : 6 }
858+
858859
---
859860
Test token_count inside nested field doesn't fail:
860861
- skip:
@@ -892,3 +893,117 @@ Test token_count inside nested field doesn't fail:
892893
body:
893894
_source: false
894895
fields: [ "*" ]
896+
897+
---
898+
error includes field name:
899+
- skip:
900+
version: ' - 7.15.99'
901+
reason: 'error changed in 7.16.0'
902+
903+
- do:
904+
indices.create:
905+
index: test
906+
body:
907+
settings:
908+
index.number_of_shards: 1
909+
mappings:
910+
properties:
911+
keyword:
912+
type: keyword
913+
date:
914+
type: date
915+
916+
- do:
917+
index:
918+
index: test
919+
id: 1
920+
refresh: true
921+
body:
922+
keyword: "value"
923+
date: "1990-12-29T22:30:00.000Z"
924+
925+
- do:
926+
catch: '/error fetching \[keyword\]: Field \[keyword\] of type \[keyword\] doesn''t support formats./'
927+
search:
928+
index: test
929+
body:
930+
fields:
931+
- field: keyword
932+
format: "yyyy/MM/dd"
933+
934+
---
935+
error includes glob pattern:
936+
- skip:
937+
version: ' - 7.15.99'
938+
reason: 'error changed in 7.16.0'
939+
940+
- do:
941+
indices.create:
942+
index: test
943+
body:
944+
settings:
945+
index.number_of_shards: 1
946+
mappings:
947+
properties:
948+
dkeyword:
949+
type: keyword
950+
date:
951+
type: date
952+
953+
- do:
954+
index:
955+
index: test
956+
id: 1
957+
refresh: true
958+
body:
959+
dkeyword: "value"
960+
date: "1990-12-29T22:30:00.000Z"
961+
962+
- do:
963+
catch: '/error fetching \[dkeyword\] which matched \[d\*\]: Field \[dkeyword\] of type \[keyword\] doesn''t support formats./'
964+
search:
965+
index: test
966+
body:
967+
fields:
968+
- field: d*
969+
format: "yyyy/MM/dd"
970+
971+
972+
---
973+
error for flattened includes whole path:
974+
- skip:
975+
version: ' - 7.15.99'
976+
reason: 'error changed in 7.16.0'
977+
978+
- do:
979+
indices.create:
980+
index: test
981+
body:
982+
settings:
983+
index.number_of_shards: 1
984+
mappings:
985+
properties:
986+
flattened:
987+
type: flattened
988+
989+
date:
990+
type: date
991+
992+
- do:
993+
index:
994+
index: test
995+
id: 1
996+
refresh: true
997+
body:
998+
flattened:
999+
foo: bar
1000+
date: "1990-12-29T22:30:00.000Z"
1001+
1002+
- do:
1003+
catch: '/error fetching \[flattened.bar\]: Field \[flattened.bar\] of type \[flattened\] doesn''t support formats./'
1004+
search:
1005+
index: test
1006+
body:
1007+
fields:
1008+
- field: flattened.bar
1009+
format: "yyyy/MM/dd"

server/src/main/java/org/elasticsearch/search/fetch/subphase/FieldFetcher.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,17 @@ private static FieldFetcher create(SearchExecutionContext context,
9292
}
9393
// only add concrete fields if they are not beneath a known nested field
9494
if (nestedParentPath == null) {
95-
ValueFetcher valueFetcher = ft.valueFetcher(context, fieldAndFormat.format);
95+
ValueFetcher valueFetcher;
96+
try {
97+
valueFetcher = ft.valueFetcher(context, fieldAndFormat.format);
98+
} catch (IllegalArgumentException e) {
99+
StringBuilder error = new StringBuilder("error fetching [").append(field).append(']');
100+
if (isWildcardPattern) {
101+
error.append(" which matched [").append(fieldAndFormat.field).append(']');
102+
}
103+
error.append(": ").append(e.getMessage());
104+
throw new IllegalArgumentException(error.toString(), e);
105+
}
96106
fieldContexts.put(field, new FieldContext(field, valueFetcher));
97107
}
98108
}

x-pack/qa/runtime-fields/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ subprojects {
8585
'search/171_terms_query_with_types/*',
8686
'search/340_type_query/type query',
8787
'msearch/12_basic_with_types/*',
88+
// The error messages are different
89+
'search/330_fetch_fields/error includes field name',
90+
'search/330_fetch_fields/error includes glob pattern',
8891
/////// NOT SUPPORTED ///////
8992
].join(',')
9093
}

0 commit comments

Comments
 (0)