如何解决弹性搜索将过滤器应用于汇总数据
我有一个包含用户位置的索引,我想为每个用户选择最新位置,然后将一些复杂的过滤器应用于最新位置。我设法使用汇总和排序来获取最新的位置,但是此后我找不到方法对其应用过滤器。
Filter或post_filter不会产生预期的结果,因为它们应用于匹配而不是汇总的位置。我看到了一些有关Bucket脚本聚合的评论,但是可用的示例非常简单。 在Elastic Search中可以做些什么吗?这是SQL中非常简单的脚本。任何帮助表示赞赏!
示例ES查询以汇总位置:
GET locations/_search { "query": { "bool": { "must": { "match_all": {} } } },"aggs": { "users": { "terms": { "field": "user_id" },"aggs": { "userLocations": { "top_hits": { "sort": [{ "created": { "order": "desc" } }],"size": 1 } } } } } }
示例SQL查询(简化了,不基于实际数据):
SELECT subquery.* FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY UserId ORDER BY Created DESC) AS "RowNumber",UserId,Latitute,Longitude,Active,OrganizationId
FROM Locations
) subquery
WHERE subquery.RowNumber = 1 AND Latitude < 10 AND Longitude > 0 AND Active = 1 AND OrganizationId = 123
样本映射:
{
"mappings": {
"properties": {
"user_id": {
"type": "integer"
},"location": {
"type": "geo_point"
},"location_source_type": {
"type": "integer"
},"created": {
"type": "date"
}
"active": {
"type": "integer"
},"org_id": {
"type": "integer"
}
}
}
}
样本数据:
用户ID位置location_source_type创建的活动org_id
1(0,0)1 2020-01-01 1 123
1(0,0)1 2020-02-01 1 123
1(9,9)1 2020-03-01 1 123
2(8,8)1 2020-04-01 1 123
预期结果:
1(9,8)1 2020-04-01 1 123
这是我要应用于汇总数据的过滤器示例:
"post_filter": {
"bool": {
"should": [{
"bool": {
"must": [
{
{ "term": { "location_source_type": 1 }},{ "term" : { "org_id" : "123" }}
},{
"range": { "created": { "lte": "2020-01-01T00:00:00" } }
}
],"filter": [{
"geo_distance": {
"distance": "1000km","location": {
"lat": 9,"lon": 9
}
}
}]
}
}]
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。