【问题标题】:Filtered Aggregation returns no results过滤聚合不返回结果
【发布时间】:2020-09-05 02:49:04
【问题描述】:

我正在尝试在下面的示例中复制 filtered aggregation: 在这里,我试图过滤与管道名称匹配的文档,并在这些管道中找到要执行的最长持续时间。

{
    "_source" : {"excludes": ["stderr"]},
    "aggs" : {
        "max_duration_filtered" : {
            "filter" : {
                "term": {
                        "pipeline": "{name_of_pipeline}"
                }
            },
            "aggs" : {
                "max_duration" : {
                        "max" : {
                                "field" : "duration"
                        }
                }
            }
        }
    }
}

调用它会返回以下输出以及 1 个命中(我也传入 size=1)

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 63643,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "{name-of-index}",
        "_type" : "raw_data",
        "_id" : "{an-id}",
        "_score" : 1.0,
        "_source" : {
          "duration" : 42.8,
          "pipeline" : "{a-different-pipeline}",
          "buildNumber" : {build-number-integer}
        }
      }
    ]
  },
  "aggregations" : {
    "max_duration_filtered" : {
      "doc_count" : 0,
      "max_duration" : {
        "value" : null
      }
    }
  }
}

我真的很想了解为什么最大持续时间值为空。看来我非常仔细地反映了文档中的内容。有什么我可以尝试解决的问题吗? 谢谢!

【问题讨论】:

  • doc_count 为 0,表示您过滤的名称不在查询结果中
  • 您的过滤器不适用于命中,而仅适用于聚合。你能解释一下你对结果的期望吗?
  • @jaspreetchahal -> 我确保通过查询 => `` { "_source" : {"excludes": ["stderr"]}, "query": { "match_all" : {} }, "size": 40 } ``` 并在数据中找到了管道的名称
  • @Val 我有数据,其中包含管道列表以及它们执行所需的相应时间。我正在尝试按管道类型过滤数据,然后找到任何这些管道执行所需的最长时间。基本上按管道名称过滤,然后在过滤集中找到最大持续时间。

标签: python elasticsearch elasticsearch-aggregation


【解决方案1】:

以下查询将为您提供每个管道的最长持续时间。无需按特定管道过滤。

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "pipeline.keyword": "some-pipeline"
          }
        }
      ]
    }
  },
  "aggs": {
    "pipelines": {
      "terms": {
        "field": "pipeline.keyword",
        "size": 100
      },
      "aggs": {
        "max_duration": {
          "max": {
            "field": "duration"
          }
        }
      }
    }
  }
}

【讨论】:

  • 好建议!我确实遇到了一个错误,尽管``` { "type" : "illegal_argument_exception", "reason" : "Fielddata 在默认情况下在文本字段上被禁用。在 [pipeline] 上设置 fielddata=true 以便在内存中加载 fielddata通过反转倒排索引。请注意,这可能会占用大量内存。或者使用关键字字段。 } ```
  • 此外,我希望坚持使用 filter + agg 方法,因为我希望最终按多个字段进行过滤,并在过滤后的集合上聚合最大持续时间。
  • 修正了我的答案,请再次查看
  • 如果你想坚持使用过滤器,你应该把它移到查询部分而不是聚合部分。看我的回答
  • 天哪!有效! :D 非常感谢瓦尔!我已经接受了你的回答。再次感谢!既然我在查询部分进行过滤,我可以去掉 aggs 中的术语部分吗?
猜你喜欢
  • 1970-01-01
  • 2021-06-11
  • 2021-12-06
  • 1970-01-01
  • 2021-09-20
  • 2011-08-19
  • 2015-10-06
  • 1970-01-01
  • 2016-12-21
相关资源
最近更新 更多