忽略“匹配"聚合查询中的子句 [英] Ignore "match" clause from query in aggregation

查看:40
本文介绍了忽略“匹配"聚合查询中的子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有聚合的查询.聚合之一是在 starsCount 字段上.有一个查询子句与其他match子句(为清楚起见而被隐藏)一起过滤 starsCount 字段.

I have a query with aggregations. One of the aggregation is on the field starsCount. There is a query clause that filters on the starsCount field along with other match clauses (hidden for clarity).

我希望 starsCount 聚合在其结果中忽略 starsCount 过滤(聚合的结果应该就像我在不使用match子句的情况下运行相同的查询一样 starsCount 字段),而其他聚合保持其当前行为

I wish for the starsCount aggregation to ignore the starsCount filtering in its results (the aggregation's result should be as if I had run the same query without the match clause on the starsCount field) while the other aggregation keeps its current behavior

这可以在单个查询中完成还是应该使用多个查询?

Can this be done in a single query or should I use multiple ?

以下是(简化的)查询:

Here is the (simplified) query:

{
    [...]
    "aggs": {
        "group_by_service": {
            "comment": "keep current behaviour",
            "terms": {
                "field": "services",
                "size": 46
            }
        },
        "group_by_stars": {
            "comment": "ignore the filter on the starsCount field",
            "terms": {
                "field": "starsCount",
                "size": 100
            }
        }
    },
    "query": {
        "bool": {
            "must": [
                [...] filters on other properties, non-relevant 
                {
                    "match": {
                        "starsCount": {
                            "query": "2"
                        }
                    }
                }
            ]
        }
    }
}

推荐答案

是的,您可以通过使用

Yes you can achieve this in single query by making use of post filter and filter aggregation. You need to follow the below steps to create the query:

  1. 从主查询中删除 starsCount 匹配查询,因为它不会影响 group_by_stars 聚合.
  2. 由于 starsCount 匹配查询应过滤文档,因此将其移至 post_filter .计算汇总后,post_filter内部的任何查询都将过滤文档.
  3. 现在,由于 starsCount 不再是主查询的一部分,因此所有聚合都不会受到它的影响.但是所需的是,此过滤器应影响除 group_by_stars 聚合之外的所有其他聚合.为此,我们将使用过滤器聚合并将其应用于除 group_by_stars 聚合之外的所有聚合.
  1. Remove the starsCount match query from the main query as it should not affect the group_by_stars aggregation.
  2. Since starsCount match query should filter the documents, move it to post_filter. Any query inside post_filter will filter the documents after calculating aggregations.
  3. Now since starsCount is no more part of main query all the aggregations will not be affected by it. But what is required is that this filter should effect all other aggregations except group_by_stars aggregation. To achieve this we'll make use of filter aggregation and apply it to all the aggregations except group_by_stars aggregation.

结果查询如下.(请注意,我不是使用 match 查询,而是使用了 term 查询.您仍然可以使用match,但在这种情况下,term是一个更好的选择.):

The resultant query will be as below. (Note that instead of match query I have used term query. You can still use match but in this case term is a better choice.):

{
  "aggs": {
    "some_other_agg":{
      "filter": {
        "term": {
          "starsCount": "2"
        }
      },
      "aggs": {
        "some_other_agg_filtered": {
          "terms": {
            "field": "some_other_field"
          }
        }
      }
    },
    "group_by_service": {
      "filter": {
        "term": {
          "starsCount": "2"
        }
      },
      "aggs": {
        "group_by_service_filtered": {
          "terms": {
            "field": "services",
            "size": 46
          }
        }
      }
    },
    "group_by_stars": {
      "terms": {
        "field": "starsCount",
        "size": 100
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {...} //filter on other properties
      ]
    }
  },
  "post_filter": {
    "term": {
      "starsCount": "2"
    }
  }
}

这篇关于忽略“匹配"聚合查询中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆