store(存储)

默认情况下,字段值会被索引使他们能搜索,但他们不会被 stored(存储)。意思就是这个字段能查询,但不能取回他的原始值。

但这没有关系。这个字段值已经是 **[_source](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-source-field.html "_source field") **字段的一部分,他是被默认存储的。如果你只想取回一个字段或者少部分字段的值,而不是整个 **[_source](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-source-field.html "_source field")**,可以通过 **[source filtering](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-request-source-filtering.html "Source filtering")** 达到目的。

在这种情况下可以有意识的去 store(存储)一个字段。例如,你有一个包含title(标题), date(时间)和一个很大的 content(内容)字段,你仅仅只想取回 titledate ,而不需要从整个 _source字段提取内容:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "title": {
  7. "type": "text",
  8. "store": true #1
  9. },
  10. "date": {
  11. "type": "date",
  12. "store": true #2
  13. },
  14. "content": {
  15. "type": "text"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. '
  22. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  23. {
  24. "title": "Some short title",
  25. "date": "2015-01-01",
  26. "content": "A very long content field..."
  27. }
  28. '
  29. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  30. {
  31. "stored_fields": [ "title", "date" ] #3
  32. }
  33. '

| 1 2 | titledate 字段将被存储。 | | 3 | 这个请求将返回 titledate 的值。 |

备注

存储的字段将作为数组返回

为了保持一致性,存储的字段将总是作为数据返回,因为没有办法知道原始字段是单个值、多值还是空数组。

如果你需要原始值,你应该从 _source字段返回。

另一种情况存储字段,是存在没有存入 _source的字段(例如 **[copy_to](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/copy-to.html "copy_to") **字段)。

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

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10027606(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 郭峰ApacheCNApache中文网