0

I am new to Elastic Search. I want to find the top 10 unique recent visited doc_id.

I have done first aggregation on doc_id and added sub-aggregation to sort each group and get a single result. Now I want to sort this bucket. I am not able to sort the bucket's result based on view_timestamp. How can I add order during first aggregation?

I have tried other solutions given on stack overflow, but it is not working for me. Can anyone help me to solve this problem?


Query

{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "username": "[email protected]" }
            }
        }
    },

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

                    }
                }
            }        

        }

    }    

}

Bucket result:

{
    "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 Answers 2

0

it is beacuse your view_timestap type is not date, it is timesatmp. you should change this field to date format, such as: "updateTime": "2017-01-12T21:28:49.562065"

0

If you're only trying to order by the timestamp, you could try using a max aggregation, like in this example:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.