简体   繁体   English

Elasticsearch聚合基于过滤器计数

[英]Elasticsearch aggregate counts based on a filter

I've worked on several facet and filter based searches in the past with Solr but I'm struggling to achieve parity with Elasticsearch. 我过去曾与Solr一起研究过几个基于facet和过滤器的搜索,但我正在努力实现与Elasticsearch的平等。

I understand that aggregations are calculated against the results of a query or globally if no query is specified. 我知道聚合是根据查询结果计算的,如果没有指定查询则全局计算。 This is fine, however I would like the counts of those aggregations to be based on the results of a filter. 这很好,但我希望这些聚合的计数基于过滤器的结果。

In Solr this is straightforward - just specify a query and filter - but with Elasticsearch a filter has no effect on aggregates and the documentation is very confusing. 在Solr中,这很简单 - 只需指定查询和过滤器 - 但使用Elasticsearch,过滤器对聚合没有影响,文档非常混乱。

My desired output for the following query is for the suggestions bucket to be scoped to the query but the resulting counts therein to be scoped to the specified filter : 我对以下查询的所需输出是将suggestions存储区限定为查询,但其中的结果计数限定为指定的filter

{
    "size": 0,
    "query": {
        "range": {
            "published": {
                "gte": "now-1y",
                "lt": "now"
            }
        }
    },
    "filter": {
        {
            "term": {
                "tag.id": "123"
            }
        },
        {
            "term": {
                "tag.id": "456"
            }
        },
    },
    "aggs": {
        "tags": {
            "nested": {
                "path": "tag"
            },
            "aggs": {
                "suggestions": {
                    "terms": {
                        "field": "name",
                        "size": 10,
                        "min_doc_count": 1
                    },
                    "aggs": {
                        "id": {
                            "terms": {
                                "field": "id",
                                "size": 1
                            }
                        }
                    }
                }
            }
        }
    }
}

And given the example mapping: 并给出示例映射:

{
    "mappings":{
        "content":{
            "properties":{
                "id":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "title":{
                    "type":"string"
                },
                "byline":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "body":{
                    "type":"string"
                },
                "publishedDate":{
                    "type":"date",
                    "format":"dateOptionalTime"
                },
                "tag":{
                    "type":"nested",
                    "include_in_parent":true,
                    "properties":{
                        "id":{
                            "type":"integer"
                        },
                        "name":{
                            "type":"string"
                        }
                    }
                }
            }
        }
    }
}

Any help is appreciated. 任何帮助表示赞赏。

You can get the expected results by : 您可以通过以下方式获得预期结果:

  1. keeping the query where it is, 保持查询在哪里,
  2. but moving the filter part (what you have is equivalent to a post_filter actually and is only applied on the results after aggregations have run) into a filter aggregation 但是将filter部分(实际上相当于post_filter ,并且仅在聚合运行后的结果上应用)移动到filter聚合中

Basically this should work: 基本上这应该工作:

{
  "size": 0,
  "query": {
    "range": {
      "published": {
        "gte": "now-1y",
        "lt": "now"
      }
    }
  },
  "aggs": {
    "tags": {
      "nested": {
        "path": "tag"
      },
      "aggs": {
        "suggestions": {
          "terms": {
            "field": "tag.name",
            "size": 10,
            "min_doc_count": 1
          },
          "aggs": {
            "filtered": {
              "filter": {
                "terms": {
                  "tag.id": [
                    "123",
                    "456"
                  ]
                }
              },
              "aggs": {
                "id": {
                  "terms": {
                    "field": "tag.id",
                    "size": 1
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM