【问题标题】:Elastic - Filter after selecting top 5 hits弹性 - 选择前 5 个匹配项后过滤
【发布时间】:2020-08-19 19:10:27
【问题描述】:

我在 Kibana 中使用警报功能,我想检查一个字段的最后 5 个连续值是否超过阈值 x,但如果我在弹性查询中使用过滤器,它会在前 N 个聚合之前应用。

有没有一种方法可以在使用其他选择器或方法之后应用过滤器或检查最后一个连续值是否超过阈值?我不想在触发条件中轻松检查这一点,因为这将返回 ctx 中的所有文档,而不仅仅是那些超过我想在警报消息中显示的阈值的文档。

我已经被这个问题困扰了一段时间,我只看到博客文章说在前 N 上子聚合是不可能的,所以任何帮助或解决方法将不胜感激。

这是我的查询:

{ 
    "size": 500, 
    "query": { 
        "bool": { 
            "filter": [ 
                { 
                    "match_all": {
                        "boost": 1 
                    } 
                }, 
                {
                    "match_phrase": { 
                        "client.id": { 
                            "query": "42", 
                            "slop": 0, 
                            "zero_terms_query": "NONE", 
                            "boost": 1 
                        } 
                    } 
                }, 
                { 
                    "range": { 
                        "@timestamp": { 
                            "from": "{{period_end}}||-10m", 
                            "to": "{{period_end}}", 
                            "include_lower": true, 
                            "include_upper": true, 
                            "format": "epoch_millis", 
                            "boost": 1 
                        } 
                    }
                } 
            ], 
            "adjust_pure_negative": true, 
            "boost": 1 
        } 

    }, 
    "aggs": {
        "2": {
          "terms": {
            "field": "component.name",
            "order": {
              "_key": "desc"
            },
            "size": 50
          },
          "aggs": {
            "3": {
              "terms": {
                "field": "client.name.keyword",
                "order": {
                  "_key": "desc"
                },
                "size": 5
              },
              "aggs": {
                "1": {
                  "top_hits": {
                    "docvalue_fields": [
                      {
                        "field": "gc.oldgen.used",
                        "format": "use_field_mapping"
                      }
                    ],
                    "_source": "gc.oldgen.used",
                    "size": 5,
                    "sort": [
                      {
                        "@timestamp": {
                          "order": "desc"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }       
}

【问题讨论】:

    标签: elasticsearch kibana elasticsearch-opendistro


    【解决方案1】:

    您是否尝试使用子过滤器聚合: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html

    或者您可以使用管道聚合来操作聚合结果 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html

    顺便说一句,对客户端 ID 的术语查询看起来更合适。

    【讨论】:

    • 我想检查上面查询中获取的 component.stats.gc.oldgen.used.pct 的所有 5 个值是否都大于 40。我不知道如何使用子聚合。
    • 嗯,经过一些测试,问题是我们不能对热门点击进行子聚合stackoverflow.com/questions/38195420/… 所以你可能无法测试最后 5 个结果的值直接查询,需要在外部编写脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多