【问题标题】:How to sort bucket result based on viewed_timestamp in ElasticSearch?如何根据 ElasticSearch 中的viewed_timestamp 对存储桶结果进行排序?
【发布时间】:2016-08-23 17:10:50
【问题描述】:

我是弹性搜索的新手。我想找到最近访问过的前 10 个唯一的 doc_id。

我已经对 doc_id 进行了第一次聚合,并添加了子聚合来对每个组进行排序并获得一个结果。现在我想对这个桶进行排序。 我无法根据 view_timestamp 对存储桶的结果进行排序。如何在第一次聚合期间添加订单?

我已经尝试过针对堆栈溢出给出的其他解决方案,但它对我不起作用。谁能帮我解决这个问题?


查询

{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "username": "nil@gmail.com" }
            }
        }
    },

    "size":0,
    "aggs":{
        "title": {
            "terms": {
                "field": "doc_id",
                "size":0
            }
            ,
            "aggs": {
                "top": {
                    "top_hits": {
                       "sort": [
                        {
                            "viewed_timestamp": {
                                "order": "desc"
                            }
                        }
                    ],
                        "size": 1

                    }
                }
            }        

        }

    }    

}

桶结果:

{
    "aggregations": {
        "title": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{
                "key": "b003",
                "doc_count": 3,
                "top_tag_hits": {
                    "hits": {
                        "total": 3,
                        "max_score": null,
                        "hits": [{
                            "_index": "visitedData",
                            "_type": "userdoc",
                            "_id": "AVak51Sp",
                            "_score": null,
                            "_source": {
                                "viewed_timestamp": "20160819T152359",
                                "content_type": "bp",
                                "title": "Data print",
                                "doc_id": "BP003"
                            },
                            "sort": [
                                1471620239000
                            ]
                        }]
                    }
                }
            }, {
                "key": "bp004",
                "doc_count": 3,
                "top_tag_hits": {
                    "hits": {
                        "total": 3,
                        "max_score": null,
                        "hits": [{
                            "_index": "visitedData",
                            "_type": "userdoc",
                            "_id": "AVak513Y8G",
                            "_score": null,
                            "_source": {
                                "viewed_timestamp": "20160819T152401",
                                "content_type": "bp",
                                "title": "Application Print",
                                "doc_id": "BP004"
                            },
                            "sort": [
                                1471620241000
                            ]
                        }]
                    }
                }
            }]
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这是因为您的 view_timestap 类型不是日期,而是 timeatmp。您应该将此字段更改为日期格式,例如: "updateTime": "2017-01-12T21:28:49.562065"

    【讨论】:

      【解决方案2】:

      如果您只是尝试按时间戳排序,您可以尝试使用 max 聚合,如下例所示:

      https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example

      【讨论】:

        猜你喜欢
        • 2016-03-15
        • 2016-09-08
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        相关资源
        最近更新 更多