coerce.asciidoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. [[coerce]]
  2. === `coerce`
  3. Data is not always clean. Depending on how it is produced a number might be
  4. rendered in the JSON body as a true JSON number, e.g. `5`, but it might also
  5. be rendered as a string, e.g. `"5"`. Alternatively, a number that should be
  6. an integer might instead be rendered as a floating point, e.g. `5.0`, or even
  7. `"5.0"`.
  8. Coercion attempts to clean up dirty values to fit the datatype of a field.
  9. For instance:
  10. * Strings will be coerced to numbers.
  11. * Floating points will be truncated for integer values.
  12. For instance:
  13. [source,js]
  14. --------------------------------------------------
  15. PUT my_index
  16. {
  17. "mappings": {
  18. "properties": {
  19. "number_one": {
  20. "type": "integer"
  21. },
  22. "number_two": {
  23. "type": "integer",
  24. "coerce": false
  25. }
  26. }
  27. }
  28. }
  29. PUT my_index/_doc/1
  30. {
  31. "number_one": "10" <1>
  32. }
  33. PUT my_index/_doc/2
  34. {
  35. "number_two": "10" <2>
  36. }
  37. --------------------------------------------------
  38. // CONSOLE
  39. // TEST[catch:bad_request]
  40. <1> The `number_one` field will contain the integer `10`.
  41. <2> This document will be rejected because coercion is disabled.
  42. TIP: The `coerce` setting is allowed to have different settings for fields of
  43. the same name in the same index. Its value can be updated on existing fields
  44. using the <<indices-put-mapping,PUT mapping API>>.
  45. [[coerce-setting]]
  46. ==== Index-level default
  47. The `index.mapping.coerce` setting can be set on the index level to disable
  48. coercion globally across all mapping types:
  49. [source,js]
  50. --------------------------------------------------
  51. PUT my_index
  52. {
  53. "settings": {
  54. "index.mapping.coerce": false
  55. },
  56. "mappings": {
  57. "properties": {
  58. "number_one": {
  59. "type": "integer",
  60. "coerce": true
  61. },
  62. "number_two": {
  63. "type": "integer"
  64. }
  65. }
  66. }
  67. }
  68. PUT my_index/_doc/1
  69. { "number_one": "10" } <1>
  70. PUT my_index/_doc/2
  71. { "number_two": "10" } <2>
  72. --------------------------------------------------
  73. // CONSOLE
  74. // TEST[catch:bad_request]
  75. <1> The `number_one` field overrides the index level setting to enable coercion.
  76. <2> This document will be rejected because the `number_two` field inherits the index-level coercion setting.