• Python ES 单个字段分组和多个字段分组聚合查询


    1.以下操作es5.+和7.+都能使用

    1).根据字段http_host 和 字段http_path分组

        filters = {
            "size": 5,
            "from": 0,
            "sort": [
                {
                    "@timestamp": {
                        "order": "desc"
                    }
                }
            ],
            "query": {
                "bool": {
                    "filter": [
                        {
                            "query_string": {
                                "query": "http_path:/video/BV1cq4y1R7y1 AND http_host:www.bilibili.com",
                                # "query":"*" ,
                            }
                        },
                        {
                            "range": {
                                "@timestamp": {
                                    "gte": begin_time * 1000,
                                    "lte": end_time * 1000,
                                    "format": "epoch_millis"
                                }
                            }
                        }
                    ],
                    "must_not": []
                }
            },
            # "aggs": {
            #     "aggs_1": {
            #         "terms": {
            #             # "field": "http_host",
            #             "script": "doc['http_host'].value +'#split#'+ doc['http_path'].value",
            #             "size": 50000,
            #             # "order": {
            #             #     "_count": "desc"
            #             # },
            #         }
            #     }
            # }
        }

    2)根据字段http_host字段分组

        filters = {
            "size": 5,
            "from": 0,
            "sort": [
                {
                    "@timestamp": {
                        "order": "desc"
                    }
                }
            ],
            "query": {
                "bool": {
                    "filter": [
                        {
                            "query_string": {
                                "query": "http_path:/video/BV1cq4y1R7y1 AND http_host:www.bilibili.com",
                                # "query":"*" ,
                            }
                        },
                        {
                            "range": {
                                "@timestamp": {
                                    "gte": begin_time * 1000,
                                    "lte": end_time * 1000,
                                    "format": "epoch_millis"
                                }
                            }
                        }
                    ],
                    "must_not": []
                }
            },
            # "aggs": {
            #     "aggs_1": {
            #         "terms": {
            #             # "field": "http_host",
            #             "field": "http_host",
            #             "size": 50000,
            #             # "order": {
            #             #     "_count": "desc"
            #             # },
            #         }
            #     }
            # }
        }

    注意无论怎么聚合最终聚合结果都是嵌套在原始数据中,关键字aggregations下

  • 相关阅读:
    完成登录与注册页面的前端
    JavaScript 基础,登录验证
    CSS实例:图片导航块
    导航,头部,CSS基础
    web基础,用html元素制作web页面
    web基础
    timestamp与timedelta,管理信息系统概念与基础
    datetime处理日期和时间
    加载静态文件,父模板的继承和扩展
    开始Flask项目
  • 原文地址:https://www.cnblogs.com/kylin5201314/p/16406313.html
Copyright © 2020-2023  润新知