Elasticsearch Top Hits Aggregation

Hyunjun Jang·2021년 7월 22일
2

Top Hits Aggregation

"data.http_request" 필드로 그룹화 하고, 각 그룹별로 가장 오래 걸린 response_msecs에 대한 doc 1건만 가져오는 쿼리

  • 집계된 그룹에 top_hits의 _source 필드를 지정하여 hits를 반환할수 있다.
{
    "aggs": {
        "agg": {
            "terms": {
                "field": "data.http_request"
            },
            "aggs": {
                "agg": {
                    "top_hits": {
                        "_source": [
                            "data.http_method",
                            "data.http_path",
                            "data.response_msecs",
                            "data.http_statcode",
                            "data.@timestamp"
                        ],
                        "size": 1,
                        "sort": [
                            {
                                "data.response_msecs": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
  • 결과
    "aggregations": {
        "agg": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 86,
            "buckets": [
                {
                    "key": "GET /index.php HTTP/1.1",
                    "doc_count": 3617,
                    "agg": {
                        "hits": {
                            "total": 3617,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "test_2021.07.22",
                                    "_type": "data",
                                    "_id": "veuZzHoBIg2C3BP3w2AA",
                                    "_score": null,
                                    "_source": {
                                        "data": {
                                            "http_method": "GET",
                                            "@timestamp": 1626930266000,
                                            "http_path": "/index.php",
                                            "http_statcode": "200",
                                            "response_msecs": 14045
                                        }
                                    },
                                    "sort": [
                                        14045
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "GET /exercise_guide.txt HTTP/1.1",
                    "doc_count": 32,
                    "agg": {
                        "hits": {
                            "total": 32,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "test_2021.07.22",
                                    "_type": "data",
                                    "_id": "c-uZzHoBIg2C3BP3vFiq",
                                    "_score": null,
                                    "_source": {
                                        "data": {
                                            "http_method": "GET",
                                            "@timestamp": 1626930265000,
                                            "http_path": "/exercise_guide.txt",
                                            "http_statcode": "200",
                                            "response_msecs": 2242
                                        }
                                    },
                                    "sort": [
                                        2242
                                    ]
                                }
                            ]
                        }
                    }
                },

Reference

search-aggregations-metrics-top-hits-aggregation

profile
Let's grow together😊

0개의 댓글