回答
是的!使用source filter。如果你正在使用JSON搜索,它會是這個樣子:
{
"_source": ["user", "message", ...],
"query": ...,
"size": ...
}
在ES 2.4和更早的版本,你也可以使用fields option to the search API:
{
"fields": ["user", "message", ...],
"query": ...,
"size": ...
}
這在ES棄用5+。無論如何,源代碼過濾器更強大!
確保將它們定義爲「存儲」:在映射中爲true。否則,ES將仍然加載_source文檔並從那裏加載字段。如果返回的數據對整個文檔的大小相對較小,可能會影響性能。 – 2013-08-18 10:19:31
你的意思是「存儲」:真正的 – sscarduzio 2014-02-17 16:50:23
這些是在conf文件中製作的還是在哪裏? – vbNewbie 2014-02-26 17:26:13
我發現文檔爲get api
是有幫助的 - 尤其是兩個部分,源過濾和領域:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
他們指出大約源過濾:
如果你只需要來自完整_source的一個或兩個字段,您可以使用_source_include & _source_exclude參數來包含或者 過濾出您需要的部分。這可以在 大文件特別有幫助,其中部分檢索可以節省網絡開銷
這完全符合我的用例。我結束了簡單過濾源像這樣(使用速記):
{
"_source": ["field_x", ..., "field_y"],
"query": {
...
}
}
僅供參考,它們在文檔狀態有關字段參數:
GET操作允許指定一組存儲的字段將通過傳遞fields參數返回 。
它似乎迎合已專門存儲的字段,它將每個字段放在數組中。如果指定的字段沒有被存儲,它將從_source中獲取每個字段,這可能導致「較慢」的檢索。我也遇到了麻煩,試圖讓它返回對象類型的字段。
因此,總而言之,您有兩個選擇,通過源過濾或[存儲]字段。
對我來說是訣竅。使用「字段」返回geo_point時遇到了問題,但「_source」工作正常,謝謝! – Yonnaled 2016-08-11 08:28:37
所有的REST API接受可用於 減少elasticsearch返回的響應filter_path參數。此參數以 逗號分隔的以點表示法表示的過濾器列表。
在Elasticsearch低於5.x上述方法已被棄用。 您可以使用_source方法,但是在某些情況下,存儲字段是有意義的。例如,如果您的文檔包含標題,日期和非常大的內容字段,則可能需要檢索標題和日期,而無需從大型_source字段中提取這些字段:
In這種情況下,您將使用:
{
"size": $INT_NUM_OF_DOCS_TO_RETURN,
"stored_fields":[
"doc.headline",
"doc.text",
"doc.timestamp_utc"
],
"query":{
"bool":{
"must":{
"term":{
"doc.topic":"news_on_things"
}
},
"filter":{
"range":{
"doc.timestamp_utc":{
"gte":1451606400000,
"lt":1483228800000,
"format":"epoch_millis"
}
}
}
}
},
"aggs":{
}
}
請參閱有關如何索引存儲字段的文檔。 Always Up for Upvote!
在java中你可以使用setFetchSource這樣的:
client.prepareSearch(index).setTypes(type)
.setFetchSource(new String[] { "field1", "field2" }, null)
For the ES versions 5.X and above you can a ES query something like this
GET /.../...
{
"_source": {
"includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ]
},
.
.
.
.
}
一個REST API GET請求可以用 '_source' 參數進行。
示例請求
http://localhost:9200/opt_pr/_search?q=SYMBOL:ITC AND OPTION_TYPE=CE AND TRADE_DATE=2017-02-10 AND EXPIRY_DATE=2017-02-23&_source=STRIKE_PRICE
響應
{
"took": 59,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 104,
"max_score": 7.3908954,
"hits": [
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLc",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 160
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLh",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 185
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLi",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 190
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLm",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 210
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLp",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 225
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLr",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 235
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLw",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 260
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uL5",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 305
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLd",
"_score": 7.381078,
"_source": {
"STRIKE_PRICE": 165
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLy",
"_score": 7.381078,
"_source": {
"STRIKE_PRICE": 270
}
}
]
}
}
在此另一種解決方案,現在用的是匹配表達
Source filtering
Allows to control how the _source field is returned with every hit.
測試與Elastiscsearch版本5.5
關鍵字「包括」定義了具體的字段。
GET /my_indice/my_indice_type/_search
{
"_source": {
"includes": [ "my_especific_field"]
},
"query": {
"bool": {
"must": [
{"match": {
"_id": "%my_id_here_without_percent%"
}
}
]
}
}
}
here you can specify whichever field you want in your output and also which you don't.
POST index_name/_search
{
"_source": {
"includes": [ "field_name", "field_name" ],
"excludes": [ "field_name" ]
},
"query" : {
"match" : { "field_name" : "value" }
}
}
- 1. 如何僅返回Strapi中選定的某些字段?
- 2. 僅從Lucene返回某些字段搜索
- 3. elasticsearch返回的字段
- 4. ElasticSearch:字段未返回
- 5. RailsAdmin篩選僅限某些字段
- 6. Elasticsearch完全匹配某些字段,但不是其他字段
- 7. 使用.populate()從Mongoose返回某些字段
- 8. Elasticsearch:從腳本字段返回空值
- 9. 返回elasticsearch中的時間戳字段
- 10. ElasticSearch NEST返回特定字段
- 11. 在elasticsearch中處理返回的字段
- 12. elasticsearch - 返回一個字段的令牌
- 13. 僅從MOQUI FrameWork中的restServices返回實體的一些字段?
- 14. Java RESTful API - JSON repsonse不返回對象的某些字段
- 15. Rblpapi - bdh函數在某些字段返回NULL
- 16. Mysql返回整行的某些字段的重複項
- 17. Elasticsearch同義詞僅適用於某個字段
- 18. 返回字典的某些總和值
- 19. PHP的某些字符返回奇HTML
- 20. Django的僅使用某些字段中ForeignKey的
- 21. 如何僅使用cancancan和activeadmin gem更新某些字段
- 22. Foursquare的API,僅返回選定字段
- 23. JPARepository僅返回單個字段
- 24. Type.GetFields() - 僅返回「公共常量」字段
- 25. 僅返回匹配的多值字段
- 26. Elasticsearch - 僅返回源數據部分
- 27. elasticsearch僅返回總點擊次數
- 28. 僅索引mongodb中ElasticSearch的一些字段
- 29. LINQ to EF返回所有字段,不僅僅是Select()中的那些
- 30. 如何忽略mongodb和elasticsearch中的某些字段?
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source,注意大公你也可以只排除某些領域 – 2016-05-25 13:30:37