|
2 | 2 | layout: default |
3 | 3 | title: Coerce |
4 | 4 | parent: Mapping parameters |
5 | | - |
6 | 5 | nav_order: 15 |
7 | 6 | has_children: false |
8 | 7 | has_toc: false |
9 | 8 | --- |
10 | 9 |
|
11 | 10 | # Coerce |
12 | 11 |
|
13 | | -The `coerce` mapping parameter controls how values are converted to the expected field data type during indexing. This parameter lets you verify that your data is formatted and indexed properly, following the expected field types. This improves the accuracy of your search results. |
| 12 | +The `coerce` mapping parameter controls whether OpenSearch attempts to normalize and convert values to match the field's data type during indexing. |
14 | 13 |
|
15 | | ---- |
| 14 | +Data is not always consistent. Depending on how it's produced, a number might be rendered as a true JSON number like 10, but it might also be rendered as a string like "10". Similarly, a number that should be an integer might be rendered as a floating point like 10.0 or even as a string like "10.0". |
| 15 | + |
| 16 | +Coercion attempts to transform these inconsistencies to fit the field's data type: |
| 17 | + |
| 18 | +- **Strings are coerced to numbers**: `"10"` becomes `10`. |
| 19 | +- **Floating-point numbers are coerced to integers by truncating**: `10.0` becomes `10`. |
| 20 | + |
| 21 | +The `coerce` parameter can be updated on existing fields using the Update Mapping API. |
| 22 | +{: .tip} |
16 | 23 |
|
17 | 24 | ## Examples |
18 | 25 |
|
19 | 26 | The following examples demonstrate how to use the `coerce` mapping parameter. |
20 | 27 |
|
21 | | -#### Indexing a document with `coerce` enabled |
| 28 | +### Field-level coercion |
| 29 | + |
| 30 | +Create an index with different coercion settings for comparison. Coercion is enabled by default: |
22 | 31 |
|
23 | 32 | ```json |
24 | | -PUT products |
| 33 | +PUT /data_quality_demo |
25 | 34 | { |
26 | 35 | "mappings": { |
27 | 36 | "properties": { |
28 | | - "price": { |
| 37 | + "price_with_coercion": { |
| 38 | + "type": "integer" |
| 39 | + }, |
| 40 | + "price_without_coercion": { |
29 | 41 | "type": "integer", |
30 | | - "coerce": true |
| 42 | + "coerce": false |
31 | 43 | } |
32 | 44 | } |
33 | 45 | } |
34 | 46 | } |
| 47 | +``` |
| 48 | +{% include copy-curl.html %} |
| 49 | + |
| 50 | +Index a document with coercion enabled: |
35 | 51 |
|
36 | | -PUT products/_doc/1 |
| 52 | +```json |
| 53 | +PUT /data_quality_demo/_doc/1 |
37 | 54 | { |
38 | | - "name": "Product A", |
39 | | - "price": "19.99" |
| 55 | + "price_with_coercion": "10" |
40 | 56 | } |
41 | 57 | ``` |
42 | 58 | {% include copy-curl.html %} |
43 | 59 |
|
44 | | -In this example, the `price` field is defined as an `integer` type with `coerce` set to `true`. When indexing the document, the string value `19.99` is coerced to the integer `19`. |
| 60 | +To match the integer field type, the string `"10"` is is successfully converted to the integer `10`. |
45 | 61 |
|
46 | | -#### Indexing a document with `coerce` disabled |
| 62 | +Attempt to index a document with coercion disabled: |
47 | 63 |
|
48 | 64 | ```json |
49 | | -PUT orders |
50 | | -{ |
51 | | - "mappings": { |
52 | | - "properties": { |
53 | | - "quantity": { |
54 | | - "type": "integer", |
55 | | - "coerce": false |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -PUT orders/_doc/1 |
| 65 | +PUT /data_quality_demo/_doc/2 |
62 | 66 | { |
63 | | - "item": "Widget", |
64 | | - "quantity": "10" |
| 67 | + "price_without_coercion": "10" |
65 | 68 | } |
66 | 69 | ``` |
67 | 70 | {% include copy-curl.html %} |
68 | 71 |
|
69 | | -In this example, the `quantity` field is defined as an `integer` type with `coerce` set to `false`. When indexing the document, the string value `10` is not coerced, and the document is rejected because of the type mismatch. |
| 72 | +This document is rejected because coercion is disabled and the string `"10"` doesn't match the expected integer type. |
70 | 73 |
|
71 | | -#### Setting the index-level coercion setting |
| 74 | +### Index-level coercion setting |
| 75 | + |
| 76 | +You can set a default coercion policy for the entire index as follows: |
72 | 77 |
|
73 | 78 | ```json |
74 | | -PUT inventory |
| 79 | +PUT /strict_data_index |
75 | 80 | { |
76 | 81 | "settings": { |
77 | 82 | "index.mapping.coerce": false |
78 | 83 | }, |
79 | 84 | "mappings": { |
80 | 85 | "properties": { |
81 | | - "stock_count": { |
| 86 | + "flexible_field": { |
82 | 87 | "type": "integer", |
83 | 88 | "coerce": true |
84 | 89 | }, |
85 | | - "sku": { |
86 | | - "type": "keyword" |
| 90 | + "strict_field": { |
| 91 | + "type": "integer" |
87 | 92 | } |
88 | 93 | } |
89 | 94 | } |
90 | 95 | } |
| 96 | +``` |
| 97 | +{% include copy-curl.html %} |
| 98 | + |
| 99 | +Index a document containing a `flexible_field`: |
| 100 | + |
| 101 | +```json |
| 102 | +PUT /strict_data_index/_doc/1 |
| 103 | +{ |
| 104 | + "flexible_field": "10" |
| 105 | +} |
| 106 | +``` |
| 107 | +{% include copy-curl.html %} |
| 108 | + |
| 109 | +The `flexible_field` overrides the index-level setting and enables coercion, so the string `"10"` is successfully converted to the integer `10`. |
91 | 110 |
|
92 | | -PUT inventory/_doc/1 |
| 111 | +Index another document containing a `strict_field`: |
| 112 | + |
| 113 | +```json |
| 114 | +PUT /strict_data_index/_doc/2 |
93 | 115 | { |
94 | | - "sku": "ABC123", |
95 | | - "stock_count": "50" |
| 116 | + "strict_field": "10" |
96 | 117 | } |
97 | 118 | ``` |
98 | 119 | {% include copy-curl.html %} |
99 | 120 |
|
100 | | -In this example, the index-level `index.mapping.coerce` setting is set to `false`, which disables coercion for the index. However, the `stock_count` field overrides this setting and enables coercion for this specific field. |
| 121 | +This document is rejected because the `strict_field` inherits the index-level coercion setting (`false`), and the string `"10"` cannot be stored in an integer field without coercion. |
0 commit comments