# properties （属性）

## properties （属性） <a href="#properties-shu-xing-properties-shu-xing" id="properties-shu-xing-properties-shu-xing"></a>

type（类型）映射、[object fields](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/object.html)（对象字段）和[**nested fields**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/nested.html)（嵌入字段）包含的 **sub-fields**（子字段），称之为 **properties**（属性）。这些 **properties**（属性）可以为任意 **datatype**（数据类型），包括 **object**（对象）和 **nested**（嵌入数据）。**properties**（属性）可以通过以下方式加入:

* 通过创建索引是明确地定义他们。
* 通过使用

  [**PUT mapping**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html)

  &#x20;**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**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html)加入已经存在的字段。

## dot notation （“.”符号） <a href="#properties-shu-xing-dotnotation.-fu-hao" id="properties-shu-xing-dotnotation.-fu-hao"></a>

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

```
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
          }
        }
      }
    }
  }
}
'
```

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