Skip to content

Commit 0e80549

Browse files
authored
Refactor coerce mapping parameter documentation (#11020)
* Refactor coerce maping parameter documentation Signed-off-by: Fanit Kolchina <[email protected]> * Clarify intro sentences Signed-off-by: Fanit Kolchina <[email protected]> * Update _field-types/mapping-parameters/coerce.md Signed-off-by: kolchfa-aws <[email protected]> --------- Signed-off-by: Fanit Kolchina <[email protected]> Signed-off-by: kolchfa-aws <[email protected]>
1 parent e4c2613 commit 0e80549

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

_field-types/mapping-parameters/coerce.md

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,99 +2,120 @@
22
layout: default
33
title: Coerce
44
parent: Mapping parameters
5-
65
nav_order: 15
76
has_children: false
87
has_toc: false
98
---
109

1110
# Coerce
1211

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.
1413

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}
1623

1724
## Examples
1825

1926
The following examples demonstrate how to use the `coerce` mapping parameter.
2027

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:
2231

2332
```json
24-
PUT products
33+
PUT /data_quality_demo
2534
{
2635
"mappings": {
2736
"properties": {
28-
"price": {
37+
"price_with_coercion": {
38+
"type": "integer"
39+
},
40+
"price_without_coercion": {
2941
"type": "integer",
30-
"coerce": true
42+
"coerce": false
3143
}
3244
}
3345
}
3446
}
47+
```
48+
{% include copy-curl.html %}
49+
50+
Index a document with coercion enabled:
3551

36-
PUT products/_doc/1
52+
```json
53+
PUT /data_quality_demo/_doc/1
3754
{
38-
"name": "Product A",
39-
"price": "19.99"
55+
"price_with_coercion": "10"
4056
}
4157
```
4258
{% include copy-curl.html %}
4359

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`.
4561

46-
#### Indexing a document with `coerce` disabled
62+
Attempt to index a document with coercion disabled:
4763

4864
```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
6266
{
63-
"item": "Widget",
64-
"quantity": "10"
67+
"price_without_coercion": "10"
6568
}
6669
```
6770
{% include copy-curl.html %}
6871

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.
7073

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:
7277

7378
```json
74-
PUT inventory
79+
PUT /strict_data_index
7580
{
7681
"settings": {
7782
"index.mapping.coerce": false
7883
},
7984
"mappings": {
8085
"properties": {
81-
"stock_count": {
86+
"flexible_field": {
8287
"type": "integer",
8388
"coerce": true
8489
},
85-
"sku": {
86-
"type": "keyword"
90+
"strict_field": {
91+
"type": "integer"
8792
}
8893
}
8994
}
9095
}
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`.
91110

92-
PUT inventory/_doc/1
111+
Index another document containing a `strict_field`:
112+
113+
```json
114+
PUT /strict_data_index/_doc/2
93115
{
94-
"sku": "ABC123",
95-
"stock_count": "50"
116+
"strict_field": "10"
96117
}
97118
```
98119
{% include copy-curl.html %}
99120

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

Comments
 (0)