如何过滤top_hits指标聚合结果[Elasticsearch]
我想按地址分组,然后按日期获取最新地址,然后按状态过滤此结果。
ex {address: 'A', date: '10-10-1991', status: 'sold'}
{address: 'A', date: '10-10-2016', status: 'active'}
{address: 'A', date: '10-10-1981', status: 'sold'}
{address: 'B', date: '10-10-2016', status: 'sold'}
{address: 'B', date: '10-10-1771', status: 'sold'}
{address: 'B', date: '10-10-1991', status: 'active'}
//Getting address with sold status should give me only this record {address: 'B', date: '10-10-2016', status: 'sold'} since most updated one in address A is an active status
我在elasticsearch中有此查询,但它最多只能按地址分组并获取最新日期。我无法按状态过滤此结果。
{ "size": 0,
"aggs": {
"group": {
"terms": {
"field": "address"
},
"aggs": {
"group_docs": {
"top_hits": {
"size": 1,
"sort": [
{
"updated_date": {
"order": "desc"
}
}]
}
}
}
}
}
}
// This query gives me this records
{address: 'A', date: '10-10-2016', status: 'active'}
{address: 'B', date: '10-10-2016', status: 'sold'}
我想使用Elasticsearch从该结果中获得出售状态
回答:
使用ES 2.x
使用ES可以做到这一点pipeline
aggregations。首先,我们需要汇总地址。然后,我们使用两种汇总,一种汇总获取最新日期,另一种汇总获取销售状态的最新日期。然后,我们检查两个日期是否都与存储桶选择器聚合匹配。这就是它的样子。
{ "size": 0,
"aggs": {
"unique_address": {
"terms": {
"field": "address",
"size": 10
},
"aggs": {
"latest_date": {
"max": {
"field": "date"
}
},
"filter_sold": {
"filter": {
"term": {
"status": "sold"
}
},
"aggs": {
"latest_sold_date": {
"max": {
"field": "date"
}
}
}
},
"should_we_consider": {
"bucket_selector": {
"buckets_path": {
"my_var1": "latest_date",
"my_var2": "filter_sold>latest_sold_date"
},
"script": "my_var1 == my_var2"
}
}
}
}
}
}
希望这可以帮助!!
以上是 如何过滤top_hits指标聚合结果[Elasticsearch] 的全部内容, 来源链接: utcz.com/qa/431815.html