2012-03-07 49 views
234

我正在使用elasticsearch來索引我的文檔。使elasticsearch僅返回某些字段?

是否可以指示它只返回特定的字段而不是它存儲的整個json文檔?

+0

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source,注意大公你也可以只排除某些領域 – 2016-05-25 13:30:37

回答

312

是的!使用source filter。如果你正在使用JSON搜索,它會是這個樣子:

{ 
    "_source": ["user", "message", ...], 
    "query": ..., 
    "size": ... 
} 

在ES 2.4和更早的版本,你也可以使用fields option to the search API

{ 
    "fields": ["user", "message", ...], 
    "query": ..., 
    "size": ... 
} 

這在ES棄用5+。無論如何,源代碼過濾器更強大!

+8

確保將它們定義爲「存儲」:在映射中爲true。否則,ES將仍然加載_source文檔並從那裏加載字段。如果返回的數據對整個文檔的大小相對較小,可能會影響性能。 – 2013-08-18 10:19:31

+6

你的意思是「存儲」:真正的 – sscarduzio 2014-02-17 16:50:23

+0

這些是在conf文件中製作的還是在哪裏? – vbNewbie 2014-02-26 17:26:13

64

我發現文檔爲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中獲取每個字段,這可能導致「較慢」的檢索。我也遇到了麻煩,試圖讓它返回對象類型的字段。

因此,總而言之,您有兩個選擇,通過源過濾或[存儲]字段。

+0

對我來說是訣竅。使用「字段」返回geo_point時遇到了問題,但「_source」工作正常,謝謝! – Yonnaled 2016-08-11 08:28:37

4

在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!

0

在java中你可以使用setFetchSource這樣的:

client.prepareSearch(index).setTypes(type) 
      .setFetchSource(new String[] { "field1", "field2" }, null) 
9
For the ES versions 5.X and above you can a ES query something like this 

    GET /.../... 
    { 
     "_source": { 
     "includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ] 
     }, 
     . 
     . 
     . 
     . 
    } 
0

一個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 
      } 
     } 
    ] 
} 

}

2
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" } 
     } 
    } 
相關問題