![](/img/trans.png)
[英]Elasticsearch - How to return distinct documents for certain fields
[英]Make elasticsearch only return certain fields?
我正在使用 elasticsearch 來索引我的文檔。
是否可以指示它只返回特定字段而不是它存儲的整個 json 文檔?
是的,使用更好的選項source filter 。 如果您使用 JSON 進行搜索,它將如下所示:
{
"_source": ["user", "message", ...],
"query": ...,
"size": ...
}
在 ES 2.4 及更早版本中,您還可以使用搜索 API的fields 選項:
{
"fields": ["user", "message", ...],
"query": ...,
"size": ...
}
這在 ES 5+ 中已被棄用。 無論如何,源過濾器更強大!
我發現get api
的文檔很有幫助 - 特別是兩個部分,源過濾和字段: https : //www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-get.html#get-源過濾
他們說明了源過濾:
如果您只需要完整 _source 中的一兩個字段,您可以使用 _source_include 和 _source_exclude 參數來包含或過濾掉您需要的部分。 這對於部分檢索可以節省網絡開銷的大型文檔特別有用
這非常適合我的用例。 我最終只是像這樣過濾了來源(使用速記):
{
"_source": ["field_x", ..., "field_y"],
"query": {
...
}
}
僅供參考,他們在關於fields參數的文檔中說明:
get 操作允許指定將通過傳遞 fields 參數返回的一組存儲字段。
它似乎迎合了專門存儲的字段,它將每個字段放在一個數組中。 如果未存儲指定的字段,它將從 _source 中獲取每個字段,這可能會導致“更慢”的檢索。 我也很難讓它返回對象類型的字段。
因此,總而言之,您有兩種選擇,通過源過濾或 [存儲] 字段。
對於 ES 版本 5.X 及更高版本,您可以像這樣進行 ES 查詢:
GET /.../...
{
"_source": {
"includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ]
},
.
.
.
.
}
在 Elasticsearch 5.x 中,不推薦使用上述方法。 您可以使用 _source 方法,但在某些情況下,存儲字段是有意義的。 例如,如果您有一個包含標題、日期和非常大的內容字段的文檔,您可能只想檢索標題和日期,而不必從大的 _source 字段中提取這些字段:
在這種情況下,您將使用:
{
"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":{
}
}
請參閱有關如何索引存儲字段的文檔。 總是很高興為一個Upvote!
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" }
}
}
所有 REST API 都接受filter_path參數,該參數可用於減少由elasticsearch返回的響應。 此參數采用逗號分隔的過濾器列表,用點表示法表示。
這是另一個解決方案,現在使用匹配表達式
源過濾允許控制每次點擊時返回 _source 字段的方式。
使用 Elasticsearch 5.5 版測試
關鍵字includes
定義特定字段。
GET /my_indice/my_indice_type/_search
{
"_source": {
"includes": [
"my_especific_field"
]
},
"query": {
"bool": {
"must": [
{
"match": {
"_id": "%my_id_here_without_percent%"
}
}
]
}
}
}
可以使用“_source”參數發出 REST API GET 請求。
示例請求
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
}
}
]
}
}
是的,通過使用源過濾器,您可以完成此操作,這是文檔源過濾器
示例請求
POST index_name/_search
{
"_source":["field1","filed2".....]
}
輸出將是
{
"took": 57,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "index_name",
"_type": "index1",
"_id": "1",
"_score": 1,
"_source": {
"field1": "a",
"field2": "b"
},
{
"field1": "c",
"field2": "d"
},....
}
]
}
}
在 Java 中,您可以像這樣使用 setFetchSource:
client.prepareSearch(index).setTypes(type)
.setFetchSource(new String[] { "field1", "field2" }, null)
例如,您有一個包含三個字段的文檔:
PUT movie/_doc/1
{
"name":"The Lion King",
"language":"English",
"score":"9.3"
}
如果要返回name
和score
,可以使用以下命令:
GET movie/_doc/1?_source_includes=name,score
如果你想獲得一些匹配模式的字段:
GET movie/_doc/1?_source_includes=*re
也許排除一些字段:
GET movie/_doc/1?_source_excludes=score
有幾種方法可用於實現特定領域的結果。 一種可以是通過source方法。 另一種根據我們的興趣接收更清晰、更概括的答案也很有用的方法是filter_path :
文檔 Json:
"hits" : [
{
"_index" : "xxxxxx",
"_type" : "_doc",
"_id" : "xxxxxx",
"_score" : xxxxxx,
"_source" : {
"year" : 2020,
"created_at" : "2020-01-29",
"url" : "www.github.com/mbarr0987",
"name":"github"
}
}
詢問:
GET bot1/_search?filter_path=hits.hits._source.url
{
"query": {
"bool": {
"must": [
{"term": {"name.keyword":"github" }}
]
}
}
}
輸出:
{
"hits" : {
"hits" : [
{
"_source" : {
"url" : "www.github.com/mbarr0987"
}
}
]
}
}
如果您了解 sql,請編寫查詢以獲取代碼的值,例如 sql 查詢等效和 elasticsearch 查詢
POST /_sql/translate
{
"query": "select name,surname from users"
}
結果是,仔細查看包含鍵
{
"size" : 1000,
"_source" : {
"includes" : [
"name",
"surname"
],
"excludes" : [ ]
},
"sort" : [
{
"_doc" : {
"order" : "asc"
}
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.