【问题标题】:Elasticsearch group by and pick up documents having max time stampElasticsearch 分组并提取具有最大时间戳的文档
【发布时间】:2019-11-25 14:33:50
【问题描述】:

我使用的是 Elasticsearch 6.5。我正在寻找具有某些过滤条件的文档(工作正常),然后我只需要获取具有 MAX 时间戳的文档(文档中的字段)。

基本上,当我使用以下有效负载搜索索引时,我得到如下所示的数据(仅显示重要字段,数据有其他字段和内容)

{
  "query": {
        "bool": {
            "must": [
                {
                    "match": { "myfield.date" : "2019-07-02" }
                },
                {
                    "match": { "myfield.data" : "ABC" }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "regexp": { "myOtherFieldId": "myregex1" }
                            },
                            {
                                "regexp": { "myOtherFieldId": "myregex2" }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

基本上,我得到了以下文档。

date, book, id, timestamp
2019-07-02, ABC, PQR_20190703130000_1234, 2019-07-03 13:01:00
2019-07-02, ABC, PQR_20190703140000_234, 2019-07-03 14:01:00
2019-07-02, ABC, PQR_20190704100000_0199, 2019-07-04 10:01:00

总共有大约 1200 条消息或文档,具有 3 个以上的 Id。 其中,我只需要那些时间戳是最新的文档

我正在尝试进行一些聚合,但没有帮助。 应该怎么做才能获得唯一的最新文档?

我尝试过这样的事情:

{
  "query": {
        "bool": {
            "must": [
                {
                    "match": { "myfield.date" : "2019-07-02" }
                },
                {
                    "match": { "myfield.data" : "ABC" }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "regexp": { "myOtherFieldId": "myregex1" }
                            },
                            {
                                "regexp": { "myOtherFieldId": "myregex2" }
                            }
                        ]
                    }
                }
            ]
        }
    },
"aggs": {
    "group_by_id" : {
        "terms": { 
            "field": "field1.Id"
        },
        "aggs": {
            "timeStamp": {
                "max": { 
                    "field": "field1.Id"
                }
            }
        }
    },
    "max_timestamp": {
        "max_bucket": {
            "buckets_path": "group_by_id>timeStamp"
        }
    }
},
"size": "10000"
}

我仍然在这里获取所有文件。 请注意,映射显示 field1.Id 是关键字。

"Id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}

检索到的数据显示 MAX 值为 NULL。

"aggregations": {
        "group_by_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "PQR_20190703130000_1234",
                    "doc_count": 947,
                    "timeStamp": {
                        "value": null
                    }
                },
                {
                    "key": "PQR_20190703140000_234",
                    "doc_count": 947,
                    "timeStamp": {
                        "value": null
                    }
                },
                {
                    "key": "PQR_20190704100000_0199",
                    "doc_count": 947,
                    "timeStamp": {
                        "value": null
                    }
                }
            ]
        },
        "max_timestamp": {
            "value": null,
            "keys": []
        }
    }

这里缺少什么?

【问题讨论】:

    标签: java scala elasticsearch group-by


    【解决方案1】:

    不应该吗

    "aggs": {
        "timeStamp": {
            "max": { 
                "field": "field1.timestamp"
            }
        }
    }
    

    而不是

    "aggs": {
        "timeStamp": {
            "max": { 
                "field": "field1.Id"
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我都试过了,因为我的 id 里面也有时间戳,所以 max 也可以。我也在下面尝试过,它给出了相同的结果。 "aggs": { "timeStamp": { "max": { "field": "field1.timestamp.keyword" } } } 请注意,该字段是一个关键字,如 id。 "timeStamp":{"type":"keyword","ignore_above":256} 所以,我必须使用 .keyword,否则会抛出错误 Expected numeric type on field [fields1.timeStamp], but got [keyword] 我不确定它是否与 type: Keyword, that max is not working.
    • 确保它不起作用 - 即使在他们指出 The min and max aggregation operate on the double representation of the data 的文档中 - 对我来说,这意味着你不能将它与 keyword 一起使用,你需要提供数字映射或脚本
    • 谢谢。由于我对 Elasticsearch 相当陌生,您能否指出一些示例或链接如何在这种情况下提供映射或脚本,主要是为了找到时间戳的最大值?
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2018-07-13
    相关资源
    最近更新 更多