【Elasticsearch】Filters聚合

发布于:2025-02-10 ⋅ 阅读:(82) ⋅ 点赞:(0)

 Filters聚合的基本语法

 

Filters聚合是一种多桶聚合,用于将文档分组到多个桶中,每个桶对应一个过滤条件。以下是Filters聚合的基本语法和结构:

 

 

1.基本结构

 

```json

{

  "aggs": {

    "<aggregation_name>": {

      "filters": {

        "filters": {

          "<bucket_key1>": { <query1> },

          "<bucket_key2>": { <query2> },

          ...

        }

      }

    }

  }

}

```

 

 

 

2.关键部分说明

 

• `<aggregation_name>`:聚合的名称,用户自定义。

 

• `filters`:聚合类型,固定为`filters`。

 

• `filters`字段:定义多个过滤条件,每个条件对应一个桶。

 

• `<bucket_key>`:桶的名称,用户自定义。

 

• `<query>`:Elasticsearch查询语句,用于定义桶的过滤条件。

 

 

3.返回结果

返回结果中,每个桶会包含以下信息:

 

• `doc_count`:匹配该过滤条件的文档数量。

 

 

4.示例

以下是一个具体的Filters聚合示例,用于将日志消息分为“错误”和“警告”两类:

 

 

```json

POST /logs/_search?size=0

{

  "aggs": {

    "message_categories": {

      "filters": {

        "filters": {

          "errors": { "match": { "body": "error" } },

          "warnings": { "match": { "body": "warning" } }

        }

      }

    }

  }

}

```

 

 

 

• `message_categories`:聚合的名称。

 

• `errors`和`warnings`:桶的名称。

 

• `match`查询:分别匹配包含“error”和“warning”的日志消息。

 

 

5.返回结果示例

 

```json

{

  "aggregations": {

    "message_categories": {

      "buckets": {

        "errors": { "doc_count": 1 },

        "warnings": { "doc_count": 2 }

      }

    }

  }

}

```

 

 

 

• `errors`桶:匹配“error”的文档数量为1。

 

• `warnings`桶:匹配“warning”的文档数量为2。

 

 

6.使用数组形式的过滤器

Filters聚合的过滤条件也可以用数组形式定义:

 

 

```json

POST /logs/_search?size=0

{

  "aggs": {

    "message_categories": {

      "filters": {

        "filters": [

          { "match": { "body": "error" } },

          { "match": { "body": "warning" } }

        ]

      }

    }

  }

}

```

 

 

返回结果会以数组形式列出每个桶的信息:

 

 

```json

{

  "aggregations": {

    "message_categories": {

      "buckets": [

        { "doc_count": 1 },

        { "doc_count": 2 }

      ]

    }

  }

}

```

 

 

 

7.添加“其他”桶

可以通过`other_bucket`参数添加一个“其他”桶,捕获不匹配任何过滤条件的文档。`other_bucket_key`参数可以自定义“其他”桶的名称:

 

 

```json

POST /logs/_search?size=0

{

  "aggs": {

    "message_categories": {

      "filters": {

        "other_bucket_key": "other_messages",

        "filters": {

          "errors": { "match": { "body": "error" } },

          "warnings": { "match": { "body": "warning" } }

        }

      }

    }

  }

}

```

 

 

返回结果示例:

 

 

```json

{

  "aggregations": {

    "message_categories": {

      "buckets": {

        "errors": { "doc_count": 1 },

        "warnings": { "doc_count": 2 },

        "other_messages": { "doc_count": 1 }

      }

    }

  }

}

```

 

 

 

8.`keyed`参数

`keyed`参数用于控制返回结果的格式:

 

• `true`(默认):以对象形式返回桶。

 

• `false`:以数组形式返回桶。

 

 

```json

POST /sales/_search?size=0

{

  "aggs": {

    "product_categories": {

      "filters": {

        "keyed": false,

        "filters": {

          "hats": { "term": { "type": "hat" } },

          "t_shirts": { "term": { "type": "t-shirt" } }

        }

      }

    }

  }

}

```

 

 

返回结果示例:

 

 

```json

{

  "aggregations": {

    "product_categories": {

      "buckets": [

        { "key": "hats", "doc_count": 5 },

        { "key": "t_shirts", "doc_count": 10 }

      ]

    }

  }

}

```

 

 

 

9.完整示例

以下是一个完整的Filters聚合示例,结合了多个参数:

 

 

```json

POST /logs/_search?size=0

{

  "aggs": {

    "message_categories": {

      "filters": {

        "keyed": false,

        "other_bucket_key": "other_messages",

        "filters": [

          { "match": { "body": "error" } },

          { "match": { "body": "warning" } }

        ]

      }

    }

  }

}

```

 

 

返回结果示例:

 

 

```json

{

  "aggregations": {

    "message_categories": {

      "buckets": [

        { "key": "error", "doc_count": 1 },

        { "key": "warning", "doc_count": 2 },

        { "key": "other_messages", "doc_count": 1 }

      ]

    }

  }

}

```

 

 

 

总结

Filters聚合的基本语法包括定义多个过滤条件,每个条件对应一个桶。通过`other_bucket`和`keyed`参数,可以灵活地控制返回结果的格式和内容。这种聚合方式适用于对文档进行分类统计的场景。


网站公告

今日签到

点亮在社区的每一天
去签到