【问题标题】:Empty aggregations after filtering in elastic search v6在弹性搜索 v6 中过滤后的空聚合
【发布时间】:2019-07-25 18:40:08
【问题描述】:

我有一个问题,只要我在我聚合的一个字段上添加过滤器,存储桶就是空的。第一个查询为我的聚合填充了桶,下面的第二个(向下滚动)没有。我不明白为什么?怎么了?

装满水桶

{
    "query":{
        "bool":{
            "filter":[
                {
                    "bool":{
                        "must":[
                            {
                                "term":{
                                    "active":true
                                }
                            },
                            {
                                "term":{
                                    "completed":true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "_source":[
        "id",
        "slug",
        "profile_title",
        "name",
        "year",
        "modified",
        "last_modified_by_user",
        "created",
        "established",
        "company.name",
        "company.company",
        "company.city",
        "company.postal_code",
        "company.translations",
        "company.country.id",
        "company.country.country",
        "company.country.translations",
        "company.country_state.id",
        "company.country_state.name",
        "company.country_state.translations",
        "description",
        "profile_images",
        "profile_category_id",
        "employee_number",
        "primary_profile_category.name",
        "primary_profile_category.translations",
        "project_count",
        "award_count",
        "exhibition_count",
        "employee_count",
        "publication_count",
        "competition_count",
        "keywords",
        "meta_description",
        "domains.id",
        "profile_categories",
        "company.company_branches.company",
        "company.company_branches.city",
        "company.company_branches.id",
        "company.company_branches.country_state.id",
        "company.company_branches.country_state.name",
        "company.company_branches.country.id",
        "company.company_branches.country.country",
        "company.branch_count",
        "profile_specializations.name",
        "profile_specializations.translations",
        "publications.*",
        "awards.*",
        "competitions.*",
        "exhibitions.*",
        "translations",
        "projects.project_photos"
    ],
    "sort":[
        {
            "last_modified_by_user":{
                "order":"desc"
            }
        }
    ],
    "aggs":{
        "countries":{
            "nested":{
                "path":"company"
            },
            "aggs":{
                "country_ids":{
                    "terms":{
                        "field":"company.country_id"
                    }
                },
                "branch_country_ids":{
                    "terms":{
                        "field":"company.company_branches.country_id"
                    }
                }
            }
        },
        "country_states":{
            "nested":{
                "path":"company"
            },
            "aggs":{
                "state_ids":{
                    "terms":{
                        "field":"company.state_id"
                    }
                },
                "branch_state_ids":{
                    "terms":{
                        "field":"company.company_branches.state_id"
                    }
                }
            }
        }
    }
}

桶的php数组结果:

[
    'countries' => [
        'doc_count' => (int) 3165,
        'branch_country_ids' => [
            'doc_count_error_upper_bound' => (int) 3,
            'sum_other_doc_count' => (int) 113,
            'buckets' => [
                (int) 0 => [
                    'key' => (int) 8,
                    'doc_count' => (int) 143
                ],
                (int) 1 => [
                    'key' => (int) 9,
                    'doc_count' => (int) 112
                ],
                (int) 2 => [
                    'key' => (int) 11,
                    'doc_count' => (int) 54
                ],
                (int) 3 => [
                    'key' => (int) 51,
                    'doc_count' => (int) 42
                ],
                (int) 4 => [
                    'key' => (int) 13,
                    'doc_count' => (int) 22
                ],
                (int) 5 => [
                    'key' => (int) 28,
                    'doc_count' => (int) 16
                ],
                (int) 6 => [
                    'key' => (int) 42,
                    'doc_count' => (int) 13
                ],
                (int) 7 => [
                    'key' => (int) 27,
                    'doc_count' => (int) 12
                ],
                (int) 8 => [
                    'key' => (int) 22,
                    'doc_count' => (int) 10
                ],
                (int) 9 => [
                    'key' => (int) 18,
                    'doc_count' => (int) 9
                ]
            ]
        ],
        'country_ids' => [
            'doc_count_error_upper_bound' => (int) 10,
            'sum_other_doc_count' => (int) 481,
            'buckets' => [
                (int) 0 => [
                    'key' => (int) 8,
                    'doc_count' => (int) 940
                ],
                (int) 1 => [
                    'key' => (int) 9,
                    'doc_count' => (int) 731
                ],
                (int) 2 => [
                    'key' => (int) 11,
                    'doc_count' => (int) 248
                ],
                (int) 3 => [
                    'key' => (int) 13,
                    'doc_count' => (int) 165
                ],
                (int) 4 => [
                    'key' => (int) 42,
                    'doc_count' => (int) 138
                ],
                (int) 5 => [
                    'key' => (int) 18,
                    'doc_count' => (int) 130
                ],
                (int) 6 => [
                    'key' => (int) 10,
                    'doc_count' => (int) 91
                ],
                (int) 7 => [
                    'key' => (int) 28,
                    'doc_count' => (int) 91
                ],
                (int) 8 => [
                    'key' => (int) 51,
                    'doc_count' => (int) 84
                ],
                (int) 9 => [
                    'key' => (int) 23,
                    'doc_count' => (int) 66
                ]
            ]
        ]
    ],
    'country_states' => [
        'doc_count' => (int) 3165,
        'branch_state_ids' => [
            'doc_count_error_upper_bound' => (int) 6,
            'sum_other_doc_count' => (int) 300,
            'buckets' => [
                (int) 0 => [
                    'key' => (int) 9,
                    'doc_count' => (int) 50
                ],
                (int) 1 => [
                    'key' => (int) 115,
                    'doc_count' => (int) 46
                ],
                (int) 2 => [
                    'key' => (int) 118,
                    'doc_count' => (int) 26
                ],
                (int) 3 => [
                    'key' => (int) 511,
                    'doc_count' => (int) 26
                ],
                (int) 4 => [
                    'key' => (int) 478,
                    'doc_count' => (int) 20
                ],
                (int) 5 => [
                    'key' => (int) 123,
                    'doc_count' => (int) 17
                ],
                (int) 6 => [
                    'key' => (int) 870,
                    'doc_count' => (int) 17
                ],
                (int) 7 => [
                    'key' => (int) 8,
                    'doc_count' => (int) 13
                ],
                (int) 8 => [
                    'key' => (int) 10,
                    'doc_count' => (int) 13
                ],
                (int) 9 => [
                    'key' => (int) 57,
                    'doc_count' => (int) 12
                ]
            ]
        ],
        'state_ids' => [
            'doc_count_error_upper_bound' => (int) 22,
            'sum_other_doc_count' => (int) 1380,
            'buckets' => [
                (int) 0 => [
                    'key' => (int) 9,
                    'doc_count' => (int) 437
                ],
                (int) 1 => [
                    'key' => (int) 478,
                    'doc_count' => (int) 157
                ],
                (int) 2 => [
                    'key' => (int) 511,
                    'doc_count' => (int) 141
                ],
                (int) 3 => [
                    'key' => (int) 118,
                    'doc_count' => (int) 128
                ],
                (int) 4 => [
                    'key' => (int) 115,
                    'doc_count' => (int) 127
                ],
                (int) 5 => [
                    'key' => (int) 123,
                    'doc_count' => (int) 125
                ],
                (int) 6 => [
                    'key' => (int) 902,
                    'doc_count' => (int) 98
                ],
                (int) 7 => [
                    'key' => (int) 52,
                    'doc_count' => (int) 78
                ],
                (int) 8 => [
                    'key' => (int) 138,
                    'doc_count' => (int) 77
                ],
                (int) 9 => [
                    'key' => (int) 8,
                    'doc_count' => (int) 74
                ]
            ]
        ]
    ]
]

按国家/地区 id 过滤后的空桶

{
    "query":{
        "bool":{
            "filter":[
                {
                    "bool":{
                        "must":[
                            {
                                "bool":{
                                    "must":[
                                        {
                                            "term":{
                                                "company.country_id":9
                                            }
                                        },
                                        {
                                            "term":{
                                                "company.company_branches.country_id":9
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "term":{
                                    "active":true
                                }
                            },
                            {
                                "term":{
                                    "completed":true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "_source":[
        "id",
        "slug",
        "profile_title",
        "name",
        "year",
        "modified",
        "last_modified_by_user",
        "created",
        "established",
        "company.name",
        "company.company",
        "company.city",
        "company.postal_code",
        "company.translations",
        "company.country.id",
        "company.country.country",
        "company.country.translations",
        "company.country_state.id",
        "company.country_state.name",
        "company.country_state.translations",
        "description",
        "profile_images",
        "profile_category_id",
        "employee_number",
        "primary_profile_category.name",
        "primary_profile_category.translations",
        "project_count",
        "award_count",
        "exhibition_count",
        "employee_count",
        "publication_count",
        "competition_count",
        "keywords",
        "meta_description",
        "domains.id",
        "profile_categories",
        "company.company_branches.company",
        "company.company_branches.city",
        "company.company_branches.id",
        "company.company_branches.country_state.id",
        "company.company_branches.country_state.name",
        "company.company_branches.country.id",
        "company.company_branches.country.country",
        "company.branch_count",
        "profile_specializations.name",
        "profile_specializations.translations",
        "publications.*",
        "awards.*",
        "competitions.*",
        "exhibitions.*",
        "translations",
        "projects.project_photos"
    ],
    "sort":[
        {
            "last_modified_by_user":{
                "order":"desc"
            }
        }
    ],
    "aggs":{
        "countries":{
            "nested":{
                "path":"company"
            },
            "aggs":{
                "country_ids":{
                    "terms":{
                        "field":"company.country_id"
                    }
                },
                "branch_country_ids":{
                    "terms":{
                        "field":"company.company_branches.country_id"
                    }
                }
            }
        },
        "country_states":{
            "nested":{
                "path":"company"
            },
            "aggs":{
                "state_ids":{
                    "terms":{
                        "field":"company.state_id"
                    }
                },
                "branch_state_ids":{
                    "terms":{
                        "field":"company.company_branches.state_id"
                    }
                }
            }
        }
    }
}

聚合为 php 数组结果:

[
    'countries' => [
        'doc_count' => (int) 0,
        'branch_country_ids' => [
            'doc_count_error_upper_bound' => (int) 0,
            'sum_other_doc_count' => (int) 0,
            'buckets' => []
        ],
        'country_ids' => [
            'doc_count_error_upper_bound' => (int) 0,
            'sum_other_doc_count' => (int) 0,
            'buckets' => []
        ]
    ],
    'country_states' => [
        'doc_count' => (int) 0,
        'branch_state_ids' => [
            'doc_count_error_upper_bound' => (int) 0,
            'sum_other_doc_count' => (int) 0,
            'buckets' => []
        ],
        'state_ids' => [
            'doc_count_error_upper_bound' => (int) 0,
            'sum_other_doc_count' => (int) 0,
            'buckets' => []
        ]
    ]
]

【问题讨论】:

    标签: elasticsearch aggregation elasticsearch-aggregation


    【解决方案1】:

    根据您的聚合,company 是一个嵌套字段,因此您的过滤条件还必须利用 nested 查询:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "nested": {                     <-- use nested here
                      "path": "company",
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "term": {
                                "company.country_id": 9
                              }
                            },
                            {
                              "term": {
                                "company.company_branches.country_id": 9
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "term": {
                      "active": true
                    }
                  },
                  {
                    "term": {
                      "completed": true
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "aggs": {
         ...
    

    【讨论】:

    • 谢谢,这解决了问题,但我想了解为什么? :) 我使用嵌套聚合的唯一原因是我需要两个 id 列表。原因是,我找不到从两个字段返回单个存储桶/值列表的方法。 :(
    • 之所以使用嵌套聚合是因为公司字段是嵌套在映射中的类型,因此您还需要在查询部分使用嵌套查询。
    • 是的,但查询之前也有效。我不确定 ES 在我定义它之前自动给了 company 字段的类型。嵌套聚合在此之后出现,搜索之前工作得很好,不需要嵌套。我看到你来自瑞士,你可以咨询吗?我的公司来自苏黎世,我认为我们可能需要一些帮助来解决与 ES 相关的不同问题。
    • 查看您的 LinkedIn 邀请 ;-)
    猜你喜欢
    • 1970-01-01
    • 2021-07-03
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2014-07-11
    相关资源
    最近更新 更多