[英]ElasticSearch: When using field collapsing is it possible to return all documents that do not have a value for the collapse key?
[英]Elasticsearch: How to return all documents that have the highest value in a field?
我是Elasticsearch的新手,目前遇到一些基本問題的困難。 假設我有以下映射:
PUT /myindex/_mappings/people
{
"properties": {
"name": {"type": "keyword"},
"age" : {"type": "integer"},
}
}
帶有以下文件:
{"name": "Bob", "age": 20},
{"name": "Ben", "age": 25},
{"name": "Eli", "age": 30},
{"name": "Eva", "age": 20},
{"name": "Jan", "age": 21},
{"name": "Jim", "age": 20},
{"name": "Lea", "age": 30},
如何創建一個查詢,該查詢返回索引中最老的所有人? 換句話說,我期望Eli和Lea能夠歸還,因為他們都比其他人都年齡大30歲。
我正在使用Elasticsearch API 6.0.0 for javascript(我的應用程序是用nodejs編寫的)。 現在,我的解決方法是對數據庫執行2個請求。 首先是匯總最大年齡(應返回30),然后使用該最大年齡執行另一個請求:
GET /myindex/people/_search
{
"aggs": {
"max_age": {"max": {"field": "age"}}
}
}
GET /myindex/people/_search
{
"query": {"term": {"age": <max_age>}} // where <max_age> should be 30
}
顯然,這是非常低效的。 您能幫我制定一個完成所有這些的查詢嗎?
困難的是,我事先不知道有多少文檔具有最高的價值,這意味着我無法使用此處提到的“大小”方法“ 單個查詢來查找某個字段的最大價值的文檔 ”
提前致謝!
您可以像這樣將terms
和top_hits
聚合結合起來
GET /myindex/people/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age",
"order": {
"_term": "desc"
},
"size": 1
},
"aggs": {
"oldest_people": {
"top_hits": {
"from": 0,
"size": 9000
}
}
}
}
}
}
請注意, "order": { "_term": "desc" }
和"size": 1
僅從terms
聚合返回最大terms
的存儲桶。 然后,我們僅列出top_hits
前9000個(或任意數量的)文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.