2015-08-06 11:24:29 -04:00
|
|
|
[[coerce]]
|
|
|
|
=== `coerce`
|
|
|
|
|
|
|
|
Data is not always clean. Depending on how it is produced a number might be
|
|
|
|
rendered in the JSON body as a true JSON number, e.g. `5`, but it might also
|
|
|
|
be rendered as a string, e.g. `"5"`. Alternatively, a number that should be
|
|
|
|
an integer might instead be rendered as a floating point, e.g. `5.0`, or even
|
|
|
|
`"5.0"`.
|
|
|
|
|
|
|
|
Coercion attempts to clean up dirty values to fit the datatype of a field.
|
|
|
|
For instance:
|
|
|
|
|
|
|
|
* Strings will be coerced to numbers.
|
|
|
|
* Floating points will be truncated for integer values.
|
|
|
|
|
|
|
|
For instance:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2019-01-22 09:13:52 -05:00
|
|
|
PUT my_index
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"number_one": {
|
|
|
|
"type": "integer"
|
|
|
|
},
|
|
|
|
"number_two": {
|
|
|
|
"type": "integer",
|
|
|
|
"coerce": false
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"number_one": "10" <1>
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/2
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"number_two": "10" <2>
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-09-14 15:24:03 -04:00
|
|
|
// TEST[catch:bad_request]
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
<1> The `number_one` field will contain the integer `10`.
|
|
|
|
<2> This document will be rejected because coercion is disabled.
|
|
|
|
|
2019-06-20 03:15:43 -04:00
|
|
|
TIP: The `coerce` setting value can be updated on existing fields
|
2015-08-12 15:21:37 -04:00
|
|
|
using the <<indices-put-mapping,PUT mapping API>>.
|
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
[[coerce-setting]]
|
|
|
|
==== Index-level default
|
|
|
|
|
|
|
|
The `index.mapping.coerce` setting can be set on the index level to disable
|
|
|
|
coercion globally across all mapping types:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2019-01-22 09:13:52 -05:00
|
|
|
PUT my_index
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"settings": {
|
|
|
|
"index.mapping.coerce": false
|
|
|
|
},
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"number_one": {
|
|
|
|
"type": "integer",
|
|
|
|
"coerce": true
|
|
|
|
},
|
|
|
|
"number_two": {
|
|
|
|
"type": "integer"
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{ "number_one": "10" } <1>
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/2
|
2015-08-06 11:24:29 -04:00
|
|
|
{ "number_two": "10" } <2>
|
|
|
|
--------------------------------------------------
|
2017-09-14 15:24:03 -04:00
|
|
|
// TEST[catch:bad_request]
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
<1> The `number_one` field overrides the index level setting to enable coercion.
|
|
|
|
<2> This document will be rejected because the `number_two` field inherits the index-level coercion setting.
|