Link Search Menu Expand Document Documentation Menu

对象字段类型

1.0 版引入

对象字段类型包含一个 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 参数值。

剩余 350 字符

有问题?

想要贡献?