Link Search Menu Expand Document Documentation Menu

动态

dynamic 参数指定是否可以将新检测到的字段动态添加到映射中。它接受下表中列出的参数。

参数 描述
true 指定新字段可以动态添加到映射中。默认值为 true
false 指定新字段不能动态添加到映射中。如果检测到新字段,则不会对其进行索引或搜索,但可以从 _source 字段中检索。
strict 抛出异常。当检测到新字段时,索引操作会失败。
strict_allow_templates 如果新字段与映射中预定义的动态模板匹配,则添加新字段。

示例:创建 dynamic 设置为 true 的索引

  1. 通过发送以下请求创建 dynamic 设置为 true 的索引
PUT testindex1
{
  "mappings": {
    "dynamic": true
  }
}

  1. 通过发送以下请求,索引一个包含两个字符串字段的对象字段 patient 的文档
PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  } 
}

  1. 通过发送以下请求确认映射按预期工作
GET testindex1/_mapping

对象字段 patient 和两个子字段 nameid 被添加到映射中,如下面的响应所示

{
  "testindex1": {
    "mappings": {
      "dynamic": "true",
      "properties": {
        "patient": {
          "properties": {
            "id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
}

示例:创建 dynamic 设置为 false 的索引

  1. 通过发送以下请求创建具有显式映射且 dynamic 设置为 false 的索引
PUT testindex1
{
  "mappings": {
    "dynamic": false,
    "properties": {
      "patient": {
        "properties": {
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

  1. 通过发送以下请求,索引一个包含两个字符串字段的对象字段 patient 和其他未映射字段的文档
PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  },
  "room": "room1",
  "floor": "1"
}

  1. 通过发送以下请求确认映射按预期工作
GET testindex1/_mapping

以下响应显示新字段 roomfloor 未添加到映射中,映射保持不变

{
  "testindex1": {
    "mappings": {
      "dynamic": "false",
      "properties": {
        "patient": {
          "properties": {
            "id": {
              "type": "keyword"
            },
            "name": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
  1. 通过发送以下请求从文档中获取未映射的字段 roomfloor
PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  },
  "room": "room1",
  "floor": "1"
}

以下请求搜索字段 roomfloor

POST testindex1/_search
{
  "query": {
    "term": {
      "room": "room1"
    }
  }
}

响应未返回任何结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

示例:创建 dynamic 设置为 strict 的索引

  1. 通过发送以下请求创建具有显式映射且 dynamic 设置为 strict 的索引
PUT testindex1
{
  "mappings": {
    "dynamic": strict,
    "properties": {
      "patient": {
        "properties": {
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

  1. 通过发送以下请求,索引一个包含两个字符串字段的对象字段 patient 和其他未映射字段的文档
PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  },
  "room": "room1",
  "floor": "1"
}

请注意,如下面的响应所示,抛出了一个异常

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
  },
  "status": 400
}

示例:创建 dynamic 设置为 strict_allow_templates 的索引

  1. 通过发送以下请求创建具有预定义动态模板且 dynamic 设置为 strict_allow_templates 的索引
PUT testindex1
{
  "mappings": {
    "dynamic": "strict_allow_templates",
    "dynamic_templates": [
      {
        "strings": {
          "match": "room*",
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ],
    "properties": {
      "patient": {
        "properties": {
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

  1. 通过发送以下请求,索引一个包含两个字符串字段的对象字段 patient 和一个匹配其中一个动态模板的新字段 room 的文档
PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  },
  "room": "room1"
}

索引成功,因为新字段 room 匹配动态模板。但是,新字段 floor 的索引失败,因为它不匹配任何动态模板且未显式映射,如下面的响应所示

PUT testindex1/_doc/1
{ 
  "patient": { 
    "name" : "John Doe",
    "id" : "123456"
  },
  "room": "room1",
  "floor": "1"
}