对象字段类型

对象字段类型包含一个 JSON 对象(一组名称/值对)。JSON 对象中的值可以是另一个 JSON 对象。在映射对象字段时,无需指定 object 作为类型,因为 object 是默认类型。

示例

创建包含对象字段的映射:

PUT testindex1/_mappings
{
    "properties": {
      "patient": { 
        "properties" :
          {
            "name" : {
              "type" : "text"
            },
            "id" : {
              "type" : "keyword"
            }
          }   
      }
    }
}

索引一个包含对象字段的文档:

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

嵌套对象在内部存储为扁平的键/值对。要引用嵌套对象中的字段,请使用 父字段.子字段(例如,patient.id)。

搜索 ID 为 123456 的患者:

GET testindex1/_search
{
  "query": {
    "term" : {
      "patient.id" : "123456"
    }
  }
}

参数

下表列出了对象字段类型接受的参数。所有参数均为可选。

参数 描述
dynamic 指定是否可以向对象动态添加新字段。有效值为 truefalsestrictstrict_allow_templates。默认值为 true
enabled 一个布尔值,指定是否应解析对象的 JSON 内容。如果 enabled 设置为 false,则对象的内容不会被索引或搜索,但仍可从 _source 字段中检索。默认值为 true
properties 该对象的字段,可以是任何受支持的类型。如果 dynamic 设置为 true,则可以动态向此对象添加新属性。

dynamic 参数

dynamic 参数指定是否可以向已索引的对象动态添加新字段。

例如,您可以先创建一个仅包含一个字段的 patient 对象映射:

PUT testindex1/_mappings
{
    "properties": {
      "patient": { 
        "properties" :
          {
            "name" : {
              "type" : "text"
            }
          }   
      }
    }
}

然后索引一个在 patient 中包含新 id 字段的文档:

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

结果,id 字段被添加到映射中:

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

dynamic 参数具有以下有效值。

描述
true 可以动态向映射添加新字段。这是默认值。
false 无法动态向映射添加新字段。如果检测到新字段,则不会对其进行索引或搜索。但是,仍可从 _source 字段中检索它。
strict 当尝试动态向映射添加新字段时,会抛出异常。要向对象添加新字段,必须先将其添加到映射中。
strict_allow_templates 如果新检测到的字段与映射中任何预定义的动态模板匹配,则将其添加到映射中;如果不匹配,则抛出异常。

内部对象从其父对象继承 dynamic 参数值,除非它们声明了自己的 dynamic 参数值。