coerce.asciidoc 2.3 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. * Lon/lat geo-points will be normalized to a standard -180:180 / -90:90 coordinate system.
  13. For instance:
  14. [source,js]
  15. --------------------------------------------------
  16. PUT my_index
  17. {
  18. "mappings": {
  19. "my_type": {
  20. "properties": {
  21. "number_one": {
  22. "type": "integer"
  23. },
  24. "number_two": {
  25. "type": "integer",
  26. "coerce": false
  27. }
  28. }
  29. }
  30. }
  31. }
  32. PUT my_index/my_type/1
  33. {
  34. "number_one": "10" <1>
  35. }
  36. PUT my_index/my_type/2
  37. {
  38. "number_two": "10" <2>
  39. }
  40. --------------------------------------------------
  41. // AUTOSENSE
  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. },
  64. "number_two": {
  65. "type": "integer",
  66. "coerce": true
  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. // AUTOSENSE
  78. <1> This document will be rejected because the `number_one` field inherits the index-level coercion setting.
  79. <2> The `number_two` field overrides the index level setting to enable coercion.