如何根据ElasticSearch中的Viewed_timestamp对存储桶结果进行排序?

时间:2016-08-23 17:10:50

标签: elasticsearch

我是Elastic Search的新手。我想找到前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
                            ]
                        }]
                    }
                }
            }]
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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