Elasticsearch v6(将PHP与Elastica结合使用)过滤聚合

时间:2019-03-12 09:38:17

标签: elasticsearch aggregation elasticsearch-aggregation

所以我认为我正在按照手册在filtering an aggregation中告诉我的内容。

    use Elastica\Aggregation\Filter as FilterAggregation;
    use Elastica\Aggregation\Terms as TermsAggregation;

    $filterAggregation = new FilterAggregation('filtered_state_ids');

    $bool = new BoolQuery();
    $bool->addShould(new Term(['company.country_state.country_id' => $countryId]));
    $bool->addShould(new Term(['company.company_branches.country_state.country_id' => $countryId]));

    $filterAggregation->setFilter($bool);

    $termsAgg = new TermsAggregation('state_ids');
    $termsAgg->setField('company.country_state.id');
    $termsAgg->setSize(1000);

    $termsAgg2 = new TermsAggregation('branch_state_ids');
    $termsAgg2->setField('company.company_branches.country_state.id');
    $termsAgg2->setSize(1000);

    $filterAggregation->addAggregation($termsAgg);
    $filterAggregation->addAggregation($termsAgg2);

    $query->_rootQuery->addAggregation($filterAggregation);

这是聚合的输出。这是不对的,对于传递的国家/地区ID,它应该仅显示1890和3440。我们基本上只是想获取给定国家/地区ID的州。

[
    'doc_count' => (int) 1,
    'branch_state_ids' => [
        'doc_count_error_upper_bound' => (int) 0,
        'sum_other_doc_count' => (int) 0,
        'buckets' => [
            (int) 0 => [
                'key' => (int) 9,
                'doc_count' => (int) 1
            ],
            (int) 1 => [
                'key' => (int) 10,
                'doc_count' => (int) 1
            ],
            (int) 2 => [
                'key' => (int) 115,
                'doc_count' => (int) 1
            ],
            (int) 3 => [
                'key' => (int) 118,
                'doc_count' => (int) 1
            ],
            (int) 4 => [
                'key' => (int) 138,
                'doc_count' => (int) 1
            ],
            (int) 5 => [
                'key' => (int) 517,
                'doc_count' => (int) 1
            ],
            (int) 6 => [
                'key' => (int) 1890,
                'doc_count' => (int) 1
            ],
            (int) 7 => [
                'key' => (int) 3440,
                'doc_count' => (int) 1
            ]
        ]
    ],
    'state_ids' => [
        'doc_count_error_upper_bound' => (int) 0,
        'sum_other_doc_count' => (int) 0,
        'buckets' => [
            (int) 0 => [
                'key' => (int) 136,
                'doc_count' => (int) 1
            ]
        ]
    ]
]

这是整个查询:

[
    'query' => [
        'bool' => [
            'filter' => [
                (int) 0 => [
                    'bool' => [
                        'must' => [
                            (int) 0 => [
                                'bool' => [
                                    'should' => [
                                        (int) 0 => [
                                            'term' => [
                                                'domains.id' => (int) 36
                                            ]
                                        ],
                                        (int) 1 => [
                                            'bool' => [
                                                'must_not' => [
                                                    (int) 0 => [
                                                        (int) 0 => [
                                                            'exists' => [
                                                                'field' => 'domains.id'
                                                            ]
                                                        ]
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            (int) 1 => [
                                'bool' => [
                                    'should' => [
                                        (int) 0 => [
                                            'term' => [
                                                'company.country_id' => (int) 72
                                            ]
                                        ],
                                        (int) 1 => [
                                            'term' => [
                                                'company.company_branches.country_id' => (int) 72
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            (int) 2 => [
                                'term' => [
                                    'active' => true
                                ]
                            ],
                            (int) 3 => [
                                'term' => [
                                    'completed' => true
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ],
    '_source' => [
        (int) 0 => 'id',
        (int) 1 => 'slug',
        (int) 2 => 'profile_title',
        (int) 3 => 'name',
        (int) 4 => 'year',
        (int) 5 => 'modified',
        (int) 6 => 'last_modified_by_user',
        (int) 7 => 'created',
        (int) 8 => 'established',
        (int) 9 => 'company.name',
        (int) 10 => 'company.company',
        (int) 11 => 'company.city',
        (int) 12 => 'company.postal_code',
        (int) 13 => 'company.translations',
        (int) 14 => 'company.country.id',
        (int) 15 => 'company.country.country',
        (int) 16 => 'company.country.translations',
        (int) 17 => 'company.country_state.id',
        (int) 18 => 'company.country_state.name',
        (int) 19 => 'company.country_state.translations',
        (int) 20 => 'description',
        (int) 21 => 'profile_images',
        (int) 22 => 'profile_category_id',
        (int) 23 => 'employee_number',
        (int) 24 => 'primary_profile_category.name',
        (int) 25 => 'primary_profile_category.translations',
        (int) 26 => 'project_count',
        (int) 27 => 'award_count',
        (int) 28 => 'exhibition_count',
        (int) 29 => 'employee_count',
        (int) 30 => 'publication_count',
        (int) 31 => 'competition_count',
        (int) 32 => 'keywords',
        (int) 33 => 'meta_description',
        (int) 34 => 'domains.id',
        (int) 35 => 'profile_categories',
        (int) 36 => 'company.company_branches.company',
        (int) 37 => 'company.company_branches.city',
        (int) 38 => 'company.company_branches.id',
        (int) 39 => 'company.company_branches.country_state.id',
        (int) 40 => 'company.company_branches.country_state.name',
        (int) 41 => 'company.company_branches.country.id',
        (int) 42 => 'company.company_branches.country.country',
        (int) 43 => 'company.branch_count',
        (int) 44 => 'profile_specializations.name',
        (int) 45 => 'profile_specializations.translations',
        (int) 46 => 'publications.*',
        (int) 47 => 'awards.*',
        (int) 48 => 'competitions.*',
        (int) 49 => 'exhibitions.*',
        (int) 50 => 'translations',
        (int) 51 => 'projects.project_photos'
    ],
    'aggs' => [
        'country_ids' => [
            'terms' => [
                'field' => 'company.country_id',
                'size' => (int) 1000
            ]
        ],
        'branch_country_ids' => [
            'terms' => [
                'field' => 'company.company_branches.country_id',
                'size' => (int) 1000
            ]
        ],
        'filtered_state_ids' => [
            'filter' => [
                'bool' => [
                    'should' => [
                        (int) 0 => [
                            'term' => [
                                'company.country_state.country_id' => (int) 72
                            ]
                        ],
                        (int) 1 => [
                            'term' => [
                                'company.company_branches.country_state.country_id' => (int) 72
                            ]
                        ]
                    ]
                ]
            ],
            'aggs' => [
                'state_ids' => [
                    'terms' => [
                        'field' => 'company.country_state.id',
                        'size' => (int) 1000
                    ]
                ],
                'branch_state_ids' => [
                    'terms' => [
                        'field' => 'company.company_branches.country_state.id',
                        'size' => (int) 1000
                    ]
                ]
            ]
        ],
        'profile_category_id' => [
            'terms' => [
                'field' => 'profile_category_id'
            ]
        ],
        'profile_categories_id' => [
            'terms' => [
                'field' => 'profile_categories.id'
            ]
        ]
    ],
    'sort' => [
        (int) 0 => [
            'last_modified_by_user' => [
                'order' => 'desc'
            ]
        ]
    ],
    'from' => (int) 0
]

0 个答案:

没有答案