enabled(开启字段)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/getting-started.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : @您的名字,ApacheCNApache中文网

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

enabled 设置只可以应用于映射类型和 object 字段,导致 Elasticsearch 完全跳过字段内容的解析.这个 JSON 仍然可以从 _source 字段中检索,但是不能以任何其他方式搜索或存储:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "session": {
  5. "properties": {
  6. "user_id": {
  7. "type": "keyword"
  8. },
  9. "last_updated": {
  10. "type": "date"
  11. },
  12. "session_data": { # 1
  13. "enabled": false
  14. }
  15. }
  16. }
  17. }
  18. }
  19. '
  20. curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
  21. {
  22. "user_id": "kimchy",
  23. "session_data": { # 2
  24. "arbitrary_object": {
  25. "some_array": [ "foo", "bar", { "baz": 2 } ]
  26. }
  27. },
  28. "last_updated": "2015-12-06T18:20:22"
  29. }
  30. '
  31. curl -XPUT 'localhost:9200/my_index/session/session_2?pretty' -H 'Content-Type: application/json' -d'
  32. {
  33. "user_id": "jpountz",
  34. "session_data": "none", # 3
  35. "last_updated": "2015-12-06T18:22:13"
  36. }
  37. '

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

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

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "session": { # 1
  5. "enabled": false
  6. }
  7. }
  8. }
  9. '
  10. curl -XPUT 'localhost:9200/my_index/session/session_1?pretty' -H 'Content-Type: application/json' -d'
  11. {
  12. "user_id": "kimchy",
  13. "session_data": {
  14. "arbitrary_object": {
  15. "some_array": [ "foo", "bar", { "baz": 2 } ]
  16. }
  17. },
  18. "last_updated": "2015-12-06T18:20:22"
  19. }
  20. '
  21. curl -XGET 'localhost:9200/my_index/session/session_1?pretty' # 2
  22. curl -XGET 'localhost:9200/my_index/_mapping?pretty' # 3

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

建议

enabled 设置允许在同一索引中相同名称的字段有不同设置。 可以使用 PUT mapping API 在现有字段上更新已经存在字段的值。