coerce

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. resp = client.indices.create(
  2. index="my-index-000001",
  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. print(resp)
  16. resp1 = client.index(
  17. index="my-index-000001",
  18. id="1",
  19. document={
  20. "number_one": "10"
  21. },
  22. )
  23. print(resp1)
  24. resp2 = client.index(
  25. index="my-index-000001",
  26. id="2",
  27. document={
  28. "number_two": "10"
  29. },
  30. )
  31. print(resp2)
  1. response = client.indices.create(
  2. index: 'my-index-000001',
  3. body: {
  4. mappings: {
  5. properties: {
  6. number_one: {
  7. type: 'integer'
  8. },
  9. number_two: {
  10. type: 'integer',
  11. coerce: false
  12. }
  13. }
  14. }
  15. }
  16. )
  17. puts response
  18. response = client.index(
  19. index: 'my-index-000001',
  20. id: 1,
  21. body: {
  22. number_one: '10'
  23. }
  24. )
  25. puts response
  26. response = client.index(
  27. index: 'my-index-000001',
  28. id: 2,
  29. body: {
  30. number_two: '10'
  31. }
  32. )
  33. puts response
  1. const response = await client.indices.create({
  2. index: "my-index-000001",
  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. console.log(response);
  16. const response1 = await client.index({
  17. index: "my-index-000001",
  18. id: 1,
  19. document: {
  20. number_one: "10",
  21. },
  22. });
  23. console.log(response1);
  24. const response2 = await client.index({
  25. index: "my-index-000001",
  26. id: 2,
  27. document: {
  28. number_two: "10",
  29. },
  30. });
  31. console.log(response2);
  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 update 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. resp = client.indices.create(
  2. index="my-index-000001",
  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. print(resp)
  19. resp1 = client.index(
  20. index="my-index-000001",
  21. id="1",
  22. document={
  23. "number_one": "10"
  24. },
  25. )
  26. print(resp1)
  27. resp2 = client.index(
  28. index="my-index-000001",
  29. id="2",
  30. document={
  31. "number_two": "10"
  32. },
  33. )
  34. print(resp2)
  1. response = client.indices.create(
  2. index: 'my-index-000001',
  3. body: {
  4. settings: {
  5. 'index.mapping.coerce' => false
  6. },
  7. mappings: {
  8. properties: {
  9. number_one: {
  10. type: 'integer',
  11. coerce: true
  12. },
  13. number_two: {
  14. type: 'integer'
  15. }
  16. }
  17. }
  18. }
  19. )
  20. puts response
  21. response = client.index(
  22. index: 'my-index-000001',
  23. id: 1,
  24. body: {
  25. number_one: '10'
  26. }
  27. )
  28. puts response
  29. response = client.index(
  30. index: 'my-index-000001',
  31. id: 2,
  32. body: {
  33. number_two: '10'
  34. }
  35. )
  36. puts response
  1. const response = await client.indices.create({
  2. index: "my-index-000001",
  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. console.log(response);
  19. const response1 = await client.index({
  20. index: "my-index-000001",
  21. id: 1,
  22. document: {
  23. number_one: "10",
  24. },
  25. });
  26. console.log(response1);
  27. const response2 = await client.index({
  28. index: "my-index-000001",
  29. id: 2,
  30. document: {
  31. number_two: "10",
  32. },
  33. });
  34. console.log(response2);
  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.