# enabled（开启字段）

**Elasticsearch**尝试着索引所有你提供的字段,但是有时候你**只想要存储这个字段而不需要索引**.例如,假设你正在使用 **Elasticsearch**作为 **web session** 的存储.你可能想要索引 **session ID** 和最后一次更新时间,但不需要在 **session** 数据本身上查询或运行聚合。

**enabled**设置只可以应用于映射类型和 [**object**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/object.html) 字段,导致 **Elasticsearch** 完全跳过字段内容的解析.这个 **JSON**仍然可以从 [**\_source**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-source-field.html) 字段中检索,但是不能以任何其他方式搜索或存储：

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "session": {
      "properties": {
        "user_id": {
          "type":  "keyword"
        },
        "last_updated": {
          "type": "date"
        },
        "session_data": { # 1
          "enabled": false
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
{
  "user_id": "kimchy",
  "session_data": {  # 2
    "arbitrary_object": {
      "some_array": [ "foo", "bar", { "baz": 2 } ]
    }
  },
  "last_updated": "2015-12-06T18:20:22"
}
'
curl -XPUT 'localhost:9200/my_index/session/session_2?pretty' -H 'Content-Type: application/json' -d'
{
  "user_id": "jpountz",
  "session_data": "none",  # 3
  "last_updated": "2015-12-06T18:22:13"
}
'
```

| 1 | session\_data 被禁用                     |
| - | ------------------------------------- |
| 2 | 任何数据都可以传递给 session\_data 字段,因为它将完全被忽略 |
| 3 | session\_data 会忽略不是JSON对象的值.          |

整个映射类型也可以被禁用,在这种情况下, document 存储在\_source字段中.这意味着它可以被检索，但是它的内容没有以任何方式被索引：

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "session": {  # 1
      "enabled": false
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
{
  "user_id": "kimchy",
  "session_data": {
    "arbitrary_object": {
      "some_array": [ "foo", "bar", { "baz": 2 } ]
    }
  },
  "last_updated": "2015-12-06T18:20:22"
}
'
curl -XGET 'localhost:9200/my_index/session/session_1?pretty' # 2
curl -XGET 'localhost:9200/my_index/_mapping?pretty' # 3
```

| 1 | 完整的 session 映射类型被禁用 |
| - | ------------------- |
| 2 | **document**可以被检索   |
| 3 | 检查映射确定没有字段被添加       |

> 建议：enabled 设置允许在同一索引中相同名称的字段有不同设置。 可以使用[**PUT mapping API**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html)在现有字段上更新已经存在字段的值。
