僅供參考,我在Mac上使用ES 1.7.2。如何彙總ElasticSearch中返回結果的數量?
我一直在試圖獲得結果集上的聚合,但我得到的是所有記錄上的聚合。
比方說,我想要返回200輛福特福克斯SE和200輛車,我想知道所有車型的Trims和每輛Trim有多少輛車。所以基本上這些修剪計數,但也得到了200回的結果。
這裏是我到目前爲止所(我使用感/奇蹟......更容易測試):
GET jdbc/_search
{
"size": 200,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{ "term": { "listing.model": "Focus" }},
{ "term": { "listing.make": "Ford" }}
]
}
}
}
},
"aggs": {
"trims": {
"terms": { "field": "listing.trim" }
},
"trim_SE": {
"filter": {
"term": { "listing.trim": "SE" }
},
"aggs": {
"trims": {
"terms": { "field": "listing.trim"}
}
}
}
},
"post_filter": {
"term": { "listing.trim": "SE" }
}
}
所以我得到20個結果回來,像這樣:
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10338,
"max_score": 1,
"hits": [
{
"_index": "myindex",
"_type": "mytype",
"_id": "472",
"_score": 1,
"_source": {
"listing": {
"vin": "111111111111",
"year": 2013,
"make": "Ford",
"model": "Focus",
"trim": "SE",
}
}
},
{...}
]
}
"aggregations": {
"trims": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 30,
"buckets": [
{
"key": "SE",
"doc_count": 10338
},
{
"key": "SEL",
"doc_count": 1000
},
{
"key": "Titanium",
"doc_count": 874
},
{
"key": "SES",
"doc_count": 585
},
{
"key": "S",
"doc_count": 554
},
{
"key": "",
"doc_count": 447
},
{
"key": "ST",
"doc_count": 339
},
{
"key": "ZTS",
"doc_count": 60
},
{
"key": "LX",
"doc_count": 56
},
{
"key": "Electric",
"doc_count": 18
}
]
},
"trim_SE": {
"doc_count": 10338,
"trims": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "SE",
"doc_count": 10338
}
]
}
}
}
}
但正如你所看到的,裁剪顯示的數量比200多,這意味着它正在對所有車輛進行聚合。
我需要一些幫助,我找不到任何實際上使這項工作。
感謝您的幫助!
在未來,不要猶豫,馬上創建使用https://www.found.no/play/一個reproductible情況。這是分享彈性搜索問題的最佳工具。 – thibpat
超級有趣的工具!感謝分享!我一定會仔細看看。 –