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 data type of a field. For instance:

  • Strings will be coerced to numbers.
  • Floating points will be truncated for integer values.

For instance:

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "number_one": {
  6. "type": "integer"
  7. },
  8. "number_two": {
  9. "type": "integer",
  10. "coerce": false
  11. }
  12. }
  13. }
  14. }
  15. PUT my-index-000001/_doc/1
  16. {
  17. "number_one": "10"
  18. }
  19. PUT my-index-000001/_doc/2
  20. {
  21. "number_two": "10"
  22. }

The number_one field will contain the integer 10.

This document will be rejected because coercion is disabled.

The coerce setting value can be updated on existing fields using the PUT mapping API.

Index-level default

The index.mapping.coerce setting can be set on the index level to disable coercion globally across all mapping types:

  1. PUT my-index-000001
  2. {
  3. "settings": {
  4. "index.mapping.coerce": false
  5. },
  6. "mappings": {
  7. "properties": {
  8. "number_one": {
  9. "type": "integer",
  10. "coerce": true
  11. },
  12. "number_two": {
  13. "type": "integer"
  14. }
  15. }
  16. }
  17. }
  18. PUT my-index-000001/_doc/1
  19. { "number_one": "10" }
  20. PUT my-index-000001/_doc/2
  21. { "number_two": "10" }

The number_one field overrides the index level setting to enable coercion.

This document will be rejected because the number_two field inherits the index-level coercion setting.