# store（存储）

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

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

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

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "properties": {
        "title": {
          "type": "text",
          "store": true #1
        },
        "date": {
          "type": "date",
          "store": true #2
        },
        "content": {
          "type": "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "title":   "Some short title",
  "date":    "2015-01-01",
  "content": "A very long content field..."
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "stored_fields": [ "title", "date" ] #3
}
'
```

| 1 , 2 | **title**和**date**字段将被存储。    |
| ----- | ---------------------------- |
| 3     | 这个请求将返回**title**和**date**的值。 |

> 备注
>
> **存储的字段将作为数组返回**
>
> 为了保持一致性，存储的字段将总是作为数据返回，因为没有办法知道原始字段是单个值、多值还是空数组。
>
> 如果你需要原始值，你应该从 `_source`字段返回。

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

## sore解析

store 的意思是，是否在 \_source 之外在独立存储一份，这里要说一下 \_source 这是源文档，当你索引数据的时候， elasticsearch 会保存一份源文档到 \_source ，如果文档的某一字段设置了 store 为 yes (默认为 no)，这时候会在 \_source 存储之外再为这个字段独立进行存储，这么做的目的主要是针对内容比较多的字段，放到 \_source 返回的话，因为\_source 是把所有字段保存为一份文档，命中后读取只需要一次 IO，包含内容特别多的字段会很占带宽影响性能，通常我们也不需要完整的内容返回(可能只关心摘要)，这时候就没必要放到 \_source 里一起返回了(当然也可以在查询时指定返回字段)。

资料:

[ elasticsearch 设置 mapping 时的 store 属性](http://blog.csdn.net/helllochun/article/details/52136954)

[elasticsearch中mapping的\_source和store的笔记 ](http://www.cnblogs.com/zklidd/p/6149120.html)
