coerce.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "my_type": {
  19. "properties": {
  20. "number_one": {
  21. "type": "integer"
  22. },
  23. "number_two": {
  24. "type": "integer",
  25. "coerce": false
  26. }
  27. }
  28. }
  29. }
  30. }
  31. PUT my_index/my_type/1
  32. {
  33. "number_one": "10" <1>
  34. }
  35. PUT my_index/my_type/2
  36. {
  37. "number_two": "10" <2>
  38. }
  39. --------------------------------------------------
  40. // AUTOSENSE
  41. <1> The `number_one` field will contain the integer `10`.
  42. <2> This document will be rejected because coercion is disabled.
  43. TIP: The `coerce` setting is allowed to have different settings for fields of
  44. the same name in the same index. Its value can be updated on existing fields
  45. using the <<indices-put-mapping,PUT mapping API>>.
  46. [[coerce-setting]]
  47. ==== Index-level default
  48. The `index.mapping.coerce` setting can be set on the index level to disable
  49. coercion globally across all mapping types:
  50. [source,js]
  51. --------------------------------------------------
  52. PUT my_index
  53. {
  54. "settings": {
  55. "index.mapping.coerce": false
  56. },
  57. "mappings": {
  58. "my_type": {
  59. "properties": {
  60. "number_one": {
  61. "type": "integer"
  62. },
  63. "number_two": {
  64. "type": "integer",
  65. "coerce": true
  66. }
  67. }
  68. }
  69. }
  70. }
  71. PUT my_index/my_type/1
  72. { "number_one": "10" } <1>
  73. PUT my_index/my_type/2
  74. { "number_two": "10" } <2>
  75. --------------------------------------------------
  76. // AUTOSENSE
  77. <1> This document will be rejected because the `number_one` field inherits the index-level coercion setting.
  78. <2> The `number_two` field overrides the index level setting to enable coercion.