基于多过滤器的Elasticsearch度量聚合

cgfeq70w  于 2023-05-16  发布在  ElasticSearch
关注(0)|答案(1)|浏览(129)

有没有一种方法可以在多个过滤器上运行度量聚合(例如值计数)。例如,我目前能够通过以下聚合获得所有用户获得的成功和错误计数:

aggs = {
    "f": {
        "filters": {
            "filters": {
                "start_count": {"term": {"type": "Success"}},
                "error_count": {"term": {"type": "Error"}}
            }
        },
        "aggs": {
            "counts": {"value_count": {"field": "type"}}
        }
    }
}

现在,假设我想获得特定用户玛丽、Joe和Tina的成功和错误计数。如何使用聚合和过滤器来实现这一点?
谢谢!

mxg2im7a

mxg2im7a1#

您可以这样做,在您想要的三个用户上添加一个过滤器,然后在user字段上添加一个terms聚合。成功/错误筛选器聚合将是用户的子聚合:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "user": [
              "Mary",
              "Joe",
              "Tina"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "users": {
      "terms": {
        "field": "user",
        "size": 10
      },
      "aggs": {
        "f": {
          "filters": {
            "filters": {
              "start_count": {
                "term": {
                  "type": "Success"
                }
              },
              "error_count": {
                "term": {
                  "type": "Error"
                }
              }
            }
          },
          "aggs": {
            "counts": {
              "value_count": {
                "field": "type"
              }
            }
          }
        }
      }
    }
  }
}

相关问题