【问题标题】:Elasticsearch query to count number of hits for each APIElasticsearch 查询来计算每个 API 的点击次数
【发布时间】:2021-10-13 06:29:48
【问题描述】:

我必须获取每个 API/url 的不同 Https 响应的计数,并将前 5 个最热门的 API 显示为 Kibana 警报。

{
"query": {
    "bool": {
        "must": [
            {
                "range": {
                    "@timestamp": {
                        "from": "now-15m",
                        "to": "now",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                    }
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1
    }
},
"aggregations": {
    "Status": {
        "terms": {
            "field": "data.response.status",
            "size": 10,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
                {
                    "_count": "desc"
                },
                {
                    "_key": "asc"
                }
            ]
        }
    }
}
}

从这个查询中,我可以获取过去 15 分钟的 http 状态计数。

    "aggregations": {
    "Status": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 47,
        "buckets": [
            {
                "doc_count": 252095,
                "key": 200
            },
            {
                "doc_count": 3845,
                "key": 400
            },
            {
                "doc_count": 1102,
                "key": 404
            },
            {
                "doc_count": 853,
                "key": 401
            },
            {
                "doc_count": 694,
                "key": 206
            },
            {
                "doc_count": 305,
                "key": 500
            },
            {
                "doc_count": 166,
                "key": 204
            },
            {
                "doc_count": 61,
                "key": 429
            },
            {
                "doc_count": 56,
                "key": 403
            },
            {
                "doc_count": 40,
                "key": 422
            }
        ]
    }
}

由于我是 elasticsearch 新手,我无法使用“data.url”字段编写多个聚合来获取每个 API/url 的 http 状态计数。

我期待这样的事情

API               
/search/results  200 : 30 201: 10 500:1
/eligibility     200 : 20 500 : 3

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: elasticsearch kibana elasticsearch-aggregation


    【解决方案1】:

    很好的开始,你就快到了!

    您只需要在状态之一之上添加另一个 terms 聚合,如下所示:

    {
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "@timestamp": {
                  "from": "now-15m",
                  "to": "now",
                  "include_lower": true,
                  "include_upper": true,
                  "boost": 1
                }
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "aggregations": {
        "Url": {
          "terms": {
            "field": "url_field_name",                      <----- add this 
            "size": 10
          },
          "aggs": {
            "Status": {
              "terms": {
                "field": "data.response.status",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                  {
                    "_count": "desc"
                  },
                  {
                    "_key": "asc"
                  }
                ]
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢!!按预期完美运行!
    • 太棒了,很高兴它有帮助!
    • 我能够获得每个状态计数的顶级 API。但是是否有可能获得每个状态的百分比。我看到很少有博客和帖子说百分比在弹性搜索中是不可能的,而且在少数地方我可以看到使用 bucket_script 可以实现百分比。如果可以计算百分比,对此有什么想法吗?
    • @augustinevijay 我建议你为这个新需求创建一个新线程(也许引用这个)
    • 创建了一个新线程 stackoverflow.com/questions/68736366/… 。帮我解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多