在弹性搜索v6中过滤后的空聚合

时间:2019-03-04 12:37:55

标签: elasticsearch aggregation elasticsearch-aggregation

我有一个问题,一旦我在要聚合的一个字段上添加了一个过滤器,存储桶就为空。第一个查询使我的聚合工作已满,而下面的第二个查询(向下滚动)则没有。我不明白为什么?怎么了?

获取装满的水桶

{
    "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' => []
        ]
    ]
]

1 个答案:

答案 0 :(得分:0)

根据您的汇总,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": {
     ...