天天看點

ES _source字段介紹——json文檔,去掉的話無法更新部分文檔,最重要的是無法reindex

The <code>_source</code> 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 <code>_source</code> 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 <code>_source</code> 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).

預設情況下,Elasticsearch 用 JSON 字元串來表示文檔主體儲存在 <code>_source</code> 字段中。像其他儲存的字段一樣,<code>_source</code> 字段也會在寫入硬碟前壓縮。

這幾乎始終是需要的功能,因為:

搜尋結果中能得到完整的文檔 —— 不需要額外去别的資料源中查詢文檔

如果缺少 <code>_source</code> 字段,部分 <code>更新</code> 請求不會起作用

當你的映射有變化,而且你需要重新索引資料時,你可以直接在 Elasticsearch 中操作而不需要重新從别的資料源中取回資料。

你可以從 <code>_source</code> 中通過 <code>get</code> 或 <code>search</code> 請求取回部分字段,而不是整個文檔。

這樣更容易排查錯誤,因為你可以準确的看到每個文檔中包含的内容,而不是隻能從一堆 ID 中猜測他們的内容。

即便如此,存儲 <code>_source</code> 字段還是要占用硬碟空間的。假如上面的理由對你來說不重要,你可以用下面的映射禁用 <code>_source</code> 字段:

在搜尋請求中你可以通過限定 <code>_source</code> 字段來請求指定字段:

這些字段會從 <code>_source</code> 中提取出來,而不是傳回整個 <code>_source</code> 字段。

儲存字段 除了索引字段的值,你也可以選擇 <code>儲存</code> 字段的原始值以備日後取回。使用 Lucene 做後端的使用者用儲存字段來選擇搜尋結果的傳回值,事實上,<code>_source</code> 字段就是一個儲存字段。 在 Elasticsearch 中,單獨設定儲存字段不是一個好做法。完整的文檔已經被儲存在 <code>_source</code> 字段中。通常最好的辦法會是使用 <code>_source</code> 參數來過濾你需要的字段。

本文轉自張昺華-sky部落格園部落格,原文連結:http://www.cnblogs.com//p/6428321.html,如需轉載請自行聯系原作者