gpt4 book ai didi

elasticsearch - 如何在Elasticsearch中组合多个aggs?

转载 作者:行者123 更新时间:2023-12-03 01:37:30 25 4
gpt4 key购买 nike

我想计算一天中每个产品的每个IP访问计数。

一个索引(nginx-access-log)中包含三个参数:

  • 时间戳
  • clientip
  • product_id

  • 我知道date_histogram可以引用 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

    计数可以引用 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_precision_control

    但是我不知道如何结合aggs来构建脚本。

    更新:

    我使用以下脚本进行搜索
    GET log-nginx_access*/_search 
    {
    "aggs": {
    "by_day": {
    "date_histogram": {
    "field": "timestamp",
    "interval": "1d",
    "time_zone": "Asia/Shanghai",
    "min_doc_count": 1
    },
    "aggs": {
    "by_product": {
    "terms": {
    "field": "uri_args.product_id",
    "size": 100
    }
    },
    "aggs": {
    "by_ip": {
    "terms": {
    "field": "clientip"
    }
    }
    }
    }
    }
    }
    }

    出现错误:
    {
    "error": {
    "root_cause": [
    {
    "type": "unknown_named_object_exception",
    "reason": "Unknown BaseAggregationBuilder [by_ip]",
    "line": 18,
    "col": 20
    }
    ],
    "type": "unknown_named_object_exception",
    "reason": "Unknown BaseAggregationBuilder [by_ip]",
    "line": 18,
    "col": 20
    },
    "status": 400
    }

    最佳答案

    也许我们可以使用termsdate_histogram聚合

    引用:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

    GET /{index_name}
    {
    "aggs": {
    "by_day": {
    "date_histogram": {
    "field" : "timestamp",
    "interval" : "day"
    },
    "aggs": {
    "by_product": {
    "terms" : {
    "field" : "product",
    "size": 100 // 100 unique products will be aggregated
    },
    "aggs": {
    "by_ip": {
    "terms" : {
    "field" : "ip"
    }
    }
    }
    }
    }
    }
    }
    }
    terms聚合的响应具有 doc_count字段,该字段可能满足您的要求。我们必须考虑的一件事是 size参数,以定义聚合的唯一性。

    关于elasticsearch - 如何在Elasticsearch中组合多个aggs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256153/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com