_source field

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-source-field.html

译文链接 : http://www.apache.wiki/display/Elasticsearch/_source+field

贡献者 : 朱彦安ApacheCNApache中文网

_source字段包含在索引时传递的原始 JSON 文档正文。该 _source 字段本身不被索引(因此是不可搜索的),但它被存储,以便在执行撷取请求时可以返回,例如 get search

禁用_source字段

虽然很方便,但是 _source 字段确实在索引中有不小的存储开销。 因此,可以使用如下方式禁用 :

  1. curl -XPUT 'localhost:9200/tweets?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "tweet": {
  5. "_source": {
  6. "enabled": false
  7. }
  8. }
  9. }
  10. }
  11. '

警告 :

在禁用_source 字段之前请考虑

用户经常禁用 _source 字段而不考虑后果,然后为此后悔。 如果 _source 字段不可用,则不支持许多功能 :

  • updateupdate_by_queryreindex APIs.
  • 高亮
  • 将索引从一 个Elasticsearch 索引 reindex(重索引)到另一个索引的能力,以更改映射或分析,或将索引升级到新的主要版本。
  • 通过查看索引时使用的原始文档来调试查询或聚合的能力。
  • 潜在的未来可能会自动修复索引损坏的能力。

提示

如果磁盘空间是瓶颈,应是增加压缩级别而不是禁用 _source

The metrics use case

指标用例与其他基于时间或日志记录的用例不同,因为有许多小型文档只包含数字,日期或关键字。

没有更新,没有高亮的请求,并且数据快速老化,因此不需要 reindex

搜索请求通常使用简单查询来按日期或标签过滤数据集,结果作为聚合返回。

在这种情况下,禁用 _source 字段将节省空间并减少 I/O

建议在 metrics case 下禁用 _all 字段。

包含 / 排除 _source 中的字段

专家级功能是在文档索引后但在 _source 字段存储之前修剪 _source 字段的内容。

警告

_source 中删除字段与禁用 _source 有类似的缺点,尤其是您不能将文档从一个 Elasticsearch 索引重新索引到另一个。

考虑使用 source filtering(源代码)过滤。

可以使用 includes / excludes 参数(也可以接受通配符),如下所示 :

  1. curl -XPUT 'localhost:9200/logs?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "event": {
  5. "_source": {
  6. "includes": [
  7. "*.count",
  8. "meta.*"
  9. ],
  10. "excludes": [
  11. "meta.description",
  12. "meta.other.*"
  13. ]
  14. }
  15. }
  16. }
  17. }
  18. '
  19. curl -XPUT 'localhost:9200/logs/event/1?pretty' -H 'Content-Type: application/json' -d'
  20. {
  21. "requests": {
  22. "count": 10,
  23. "foo": "bar" # 1
  24. },
  25. "meta": {
  26. "name": "Some metric",
  27. "description": "Some metric description", # 2
  28. "other": {
  29. "foo": "one", # 3
  30. "baz": "two" # 4
  31. }
  32. }
  33. }
  34. '
  35. curl -XGET 'localhost:9200/logs/event/_search?pretty' -H 'Content-Type: application/json' -d'
  36. {
  37. "query": {
  38. "match": {
  39. "meta.other.foo": "one" # 5
  40. }
  41. }
  42. }
  43. '

| 1234 | 这些字段将从存储的 _source 字段中删除。 | | 5 | 我们仍然可以搜索这个字段,即使它不在存储的 _source。 |