> 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/3237dynamicff08-dong-tai-she-zhi-ff09.md).

# dynamic（动态设置）

默认情况下,字段会动态的添加到 document或者 document 的inner objects（内部对象），只需要通过索引包含这个新字段的 document。例如:

```
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d' # 1
{
  "username": "johnsmith",
  "name": {
    "first": "John",
    "last": "Smith"
  }
}
'
curl -XGET 'localhost:9200/my_index/_mapping?pretty' # 2
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d' # 3
{
  "username": "marywhite",
  "email": "mary@white.com",
  "name": {
    "first": "Mary",
    "middle": "Alice",
    "last": "White"
  }
}
'
curl -XGET 'localhost:9200/my_index/_mapping?pretty' # 4
```

| 1 | 这个文档介绍了字符串字段**username ,**&#x5BF9;象字段name，和两个在**name**对象下的字符串字段 ,可以称为**name.first**和**name.last**。 |
| - | -------------------------------------------------------------------------------------------------- |
| 2 | 检查映射来验证上面的**PUT**。                                                                                 |
| 3 | 这个文档添加两个字符串字段**email**和**name.middle**。                                                            |
| 4 | 检查映射来验证上面的改动.                                                                                      |

新字段如何被检测和添加到映射的细节可以见 [***Dynamic Mapping***](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/dynamic-mapping.html)**.**

**dynamic**设置控制着新的字段是否可以动态的添加.有三个配置参数：

| **true**   | 新检测的字段被添加到映射中.（默认配置）    |
| ---------- | ----------------------- |
| **false**  | 新检测的字段将被忽略。 必须明确添加新字段。  |
| **strict** | 如果检测到新字段，将抛出异常并且文档是拒绝的。 |

dynamic 可以在映射类型级别和每个[inner object](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/object.html)（内部对象）进行设置。 inner object（内部对象）继承其父对象和来自于映射类型的设置. 例如 ：

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "dynamic": false, # 1
      "properties": {
        "user": {  # 2
          "properties": {
            "name": {
              "type": "text"
            },
            "social_networks": {  # 3
              "dynamic": true,
              "properties": {}
            }
          }
        }
      }
    }
  }
}
'
```

| 1 | **dynamic mapping**（动态映射）在类型级别被禁用,所以不会有新的顶级字段动态添加.                    |
| - | --------------------------------------------------------------------- |
| 2 | **user**对象继承类型级别设置.                                                   |
| 3 | **user.social\_networks**对象允许动态映射，因此可以将新字段添加到该**inner object**（内部对象）。 |

> 建议
>
> **dynamic**设置允许在同一索引中相同名称的字段的有不同设置。 可以使用[PUT mapping API](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html)（PUT映射API）在现有字段上更新字段的值。
