摘自:https://es.xiaoleilu.com/070_Index_Mgmt/31_Metadata_source.html
The _source
field stores the JSON you send to Elasticsearch and you can choose to only return certain fields if needed, which is perfect for your use case. I have never heard that the stored fields will be faster for searches. The _source
field could be bigger on disk space, but if you have to store every field there is no need to use stored fields over the _source
field. If you do disable the source field it will mean:
- You won’t be able to do partial updates
- You won’t be able to re-index your data from the JSON in your Elasticsearch cluster, you’ll have to re-index from the data source (which is usually a lot slower).
元數據:_source 字段
默認情況下,Elasticsearch 用 JSON 字符串來表示文檔主體保存在 _source
字段中。像其他保存的字段一樣,_source
字段也會在寫入硬盤前壓縮。
這幾乎始終是需要的功能,因為:
-
搜索結果中能得到完整的文檔 —— 不需要額外去別的數據源中查詢文檔
-
如果缺少
_source
字段,部分更新
請求不會起作用 -
當你的映射有變化,而且你需要重新索引數據時,你可以直接在 Elasticsearch 中操作而不需要重新從別的數據源中取回數據。
-
你可以從
_source
中通過get
或search
請求取回部分字段,而不是整個文檔。 -
這樣更容易排查錯誤,因為你可以准確的看到每個文檔中包含的內容,而不是只能從一堆 ID 中猜測他們的內容。
即便如此,存儲 _source
字段還是要占用硬盤空間的。假如上面的理由對你來說不重要,你可以用下面的映射禁用 _source
字段:
PUT /my_index
{
"mappings": {
"my_type": {
"_source": {
"enabled": false
}
}
}
}
在搜索請求中你可以通過限定 _source
字段來請求指定字段:
GET /_search
{
"query": { "match_all": {}},
"_source": [ "title", "created" ]
}
這些字段會從 _source
中提取出來,而不是返回整個 _source
字段。
儲存字段
除了索引字段的值,你也可以選擇
儲存
字段的原始值以備日后取回。使用 Lucene 做后端的用戶用儲存字段來選擇搜索結果的返回值,事實上,_source
字段就是一個儲存字段。在 Elasticsearch 中,單獨設置儲存字段不是一個好做法。完整的文檔已經被保存在
_source
字段中。通常最好的辦法會是使用_source
參數來過濾你需要的字段。