> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/mapping/323mapping-parametersff08-ying-she-can-shu-ff09/3234coerceff08-qiang-zhi-lei-xing-zhuan-huan-ff09.md).

# Coerce（强制类型转换）

数据不总是干净的.根据它的生成方式,一个数字可能会在 `JSON body`中呈现为一个真正的 **JSON** 数字。例如5,但它也可能呈现为字符串,例如 **"5"** 。或者,应该是整型的数字被替代地呈现为浮点型.例如, **5.0** 或&#x8005;**"5.0"**.

`Coercion`尝试着清理脏数据让字段符合相应的数据类型.例如 :

* 字符串被强制转换为数字.
* 浮点型被截断为整型.

例&#x5982;**:**

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "properties": {
        "number_one": {
          "type": "integer"
        },
        "number_two": {
          "type": "integer",
          "coerce": false
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "number_one": "10"  # 1
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{
  "number_two": "10"  # 2
}
'
```

| 1 | `number_one` 字段会包含整型 `10`.                        |
| - | ------------------------------------------------- |
| 2 | `document`（文档）会拒绝因为`coercion`是关闭的,所以无法将字符串转换成整数型. |

> 提醒：
>
> **coerce** 配置允许在相同的索引中具有相同名称的字段有不同的配置.可以通过[PUT mapping API](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html)来更新已存在字段上的 **coerce** 值.

## Index-level default（索引级别的默认参数） <a href="#coerce-qiang-zhi-lei-xing-zhuan-huan-indexleveldefault-suo-yin-ji-bie-de-mo-ren-can-shu" id="coerce-qiang-zhi-lei-xing-zhuan-huan-indexleveldefault-suo-yin-ji-bie-de-mo-ren-can-shu"></a>

`index.mapping.coerce`配置可以在**index level**（索引级别）来设置,以便在所有的`Mapping Types` 全局关闭`coercion配置`.

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.mapping.coerce": false
  },
  "mappings": {
    "my_type": {
      "properties": {
        "number_one": {
          "type": "integer",
          "coerce": true
        },
        "number_two": {
          "type": "integer"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{ "number_one": "10" } # 1
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{ "number_two": "10" } # 2
'
```

| 1 | `number_one` 字段覆盖了**index level**（索引级别）的设置并开启了 `coercion`.                |
| - | ------------------------------------------------------------------------- |
| 2 | `document`（文档）会拒绝请求,因为 `number_two` 字段继承了 `index-level（索引级别）coercion`的设置. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/mapping/323mapping-parametersff08-ying-she-can-shu-ff09/3234coerceff08-qiang-zhi-lei-xing-zhuan-huan-ff09.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
