properties (属性)

properties (属性)

type(类型)映射、object fields(对象字段)和nested fields(嵌入字段)包含的 sub-fields(子字段),称之为 properties(属性)。这些 properties(属性)可以为任意 datatype(数据类型),包括 object(对象)和 nested(嵌入数据)。properties(属性)可以通过以下方式加入:

  • 通过创建索引是明确地定义他们。

  • 通过使用

    PUT mapping

    API

    添加或更新映射类型时明确地定义他们。

  • 索引包含新字段的文档时动态的加入。

下面举例演示如何将 properties(属性)加入 mapping type(映射类型)、object field(对象字段)和 nested field(嵌入字段)中:

curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": { #1
      "properties": {
        "manager": { #2
          "properties": {
            "age":  { "type": "integer" },
            "name": { "type": "text"  }
          }
        },
        "employees": { #3
          "type": "nested",
          "properties": {
            "age":  { "type": "integer" },
            "name": { "type": "text"  }
          }
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'#4
{
  "region": "US",
  "manager": {
    "name": "Alice White",
    "age": 30
  },
  "employees": [
    {
      "name": "John Smith",
      "age": 34
    },
    {
      "name": "Peter Brown",
      "age": 26
    }
  ]
}
'

1

my_type 映射类型下的properties(属性)

2

manager 对象字段下的properties(属性)

3

employee 嵌入字段下的properties(属性)

4

对应上述映射的一个示例文档

注意

同一索引下的相同名字的字段可以设置不同的properties(属性)。新的properties(属性)可以通过PUT mapping API加入已经存在的字段。

dot notation (“.”符号)

通过使用“.”符号可以使内嵌字段引入查询、聚合等功能中:

curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "manager.name": "Alice White" #1
    }
  },
  "aggs": {
    "Employees": {
      "nested": {
        "path": "employees"
      },
      "aggs": {
        "Employee Ages": {
          "histogram": {
            "field": "employees.age", #2
            "interval": 5
          }
        }
      }
    }
  }
}
'

重点: 内嵌字段必须使用完整的路径

Last updated