私はElastic Searchを初めて利用しています。最近訪問したトップ10のユニークなdoc_idを探したい。ElasticSearchでviewed_timestampに基づいてバケットの結果を並べ替える方法は?
私は最初にdoc_idで集計を行い、サブ集計を追加して各グループをソートして単一の結果を得ました。今私はこのバケツを並べ替えたい。 view_timestampに基づいてバケットの結果を並べ替えることができません。最初の集計中にオーダーを追加するにはどうすればよいですか?
私はスタックオーバーフローで与えられた他のソリューションを試しましたが、それは私のために働いていません。誰も私がこの問題を解決するのを助けることができますか?
クエリ
{
"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
}
}
}
}
}
}
バケツ結果:
{
"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
]
}]
}
}
}]
}
}
}