coerce.asciidoc 2.2 KB

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