rubyには詳しくないので、rubyでどう実装するかは分かりませんが、
素のElasticsearchのクエリだと、max_bucketアグリゲーションが使えるのではないでしょうか。
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-max-bucket-aggregation.html
提示いただいたクエリにmax_bucketを追加した例です。
aggs以下に注目してください。
POST testtest/_search
{
"query": {
"range": {
"@timestamp": {
"gte": "2019-01-11T05:00:00.000Z",
"lte": "2019-01-11T10:00:00.000Z"
}
}
},
"size": 0,
"aggs": {
"count_in_terms": {
"date_histogram": {
"field": "@timestamp",
"interval": "60m"
}
},
"where-is-max-count": {
"max_bucket": {
"buckets_path": "count_in_terms._count"
}
}
}
}
実際に帰るレスポンスは、以下のようになり、where-is-max-countでdate_histgramの中で最大カウント数が取れていることが確認できるかと思います。
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 13806,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"count_in_terms" : {
"buckets" : [
{
"key_as_string" : "2019-01-11T05:00:00.000Z",
"key" : 1547182800000,
"doc_count" : 897
},
{
"key_as_string" : "2019-01-11T06:00:00.000Z",
"key" : 1547186400000,
"doc_count" : 6789
},
{
"key_as_string" : "2019-01-11T07:00:00.000Z",
"key" : 1547190000000,
"doc_count" : 6120
}
]
},
"where-is-max-count" : {
"value" : 6789.0,
"keys" : [
"2019-01-11T06:00:00.000Z"
]
}
}
}
蛇足ながら、Elasticsearchに関する質問ですと、本家のフォーラムがありますので、そちらの方が早く、正確な回答が得られるかもしれません。
日本語でも質問できる場が用意されています。
https://discuss.elastic.co
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/18 17:53 編集