gpt4 book ai didi

elasticsearch - 如何筛选 top_hits 指标聚合结果 [Elasticsearch]

转载 作者:行者123 更新时间:2023-11-29 02:54:23 24 4
gpt4 key购买 nike

我想按地址分组,然后按日期获取最新地址,然后按状态过滤此结果。

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 中使用 pipeline aggregations 是可能的。首先,我们需要聚合地址。然后我们使用两个聚合,一个获取 latest_date,另一个获取销售状态的最新日期。然后我们检查两个日期是否与 bucket selector aggregation 匹配.这是它的样子。

{
"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"
}
}
}
}
}
}

希望这对您有所帮助!

关于elasticsearch - 如何筛选 top_hits 指标聚合结果 [Elasticsearch],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com