使用Elasticsearch版本为5.4.2
我想构建一个Elasticsearch查询来满足三个条件。
我做了1和2。但是我找不到解决3。在查询中可以执行1到3吗?以防万一,我将在PHP框架之一Laravel5.4上使用结果。
我的数据格式如下:
"_index": "timelines",
"_type": "timeline"
"_source": {
"gameId": 152735348,
"participantId": 3,
"championId": 35,
"role": "NONE",
"lane": "JUNGLE",
"win": 1,
"itemId": 1036,
"timestamp": 571200
}我当前的Elasticsearch查询是这样的
GET timelines/_search?size=0&pretty
{
"query": {
"bool": {
"must": [
{ "match": { "championId": 22 }}
]
}
},
"aggs": {
"games": {
"terms": {
"field": "gameId"
},
"aggs": {
"items": {
"terms": {
"field": "itemId",
"order" : { "min_buying_time" : "asc" }
},
"aggs": {
"min_buying_time": {
"min": {
"field": "timestamp"
}
}
}
}
}
}
}
}发布于 2017-08-08 11:18:38
正如@S nke Liebau所说,管道聚合是关键,但是如果您想要计算每个项目的所有游戏的平均最短时间,您应该首先通过itemID进行聚合。以下代码应该会有所帮助:
POST misko/_search
{
"query": {
"bool": {
"must": [
{ "match": { "championId": 22 }}
]
}
},
"aggs": {
"items": {
"terms": {
"field": "itemId"
},
"aggs": {
"games": {
"terms": {
"field": "gameId"
},
"aggs": {
"min_buying_time": {
"min": {
"field": "timestamp"
}
}
}
},
"avg_min_time": {
"avg_bucket": {
"buckets_path": "games>min_buying_time"
}
}
}
}
}
}https://stackoverflow.com/questions/45565227
复制相似问题