过滤Elastica中的全局聚合

时间:2017-04-06 11:12:40

标签: elasticsearch elastica

我使用ruflin / Elastica构建了弹性查询,具有全局聚合功能。是否有可能以某种方式添加一些过滤器,与我的主查询分开。

看起来像这样:

    $query = new Query($boolQuery);

    $categoryAggregation = new Terms('category_ids');
    $categoryAggregation->setField('category_ids');
    $categoryAggregation->setSize(0);

    $manufacturerAggregation = new Terms('manufacturer_ids');
    $manufacturerAggregation->setField('manufacturer_id');
    $manufacturerAggregation->setSize(0);

    $globalAggregation = new GlobalAggregation('global');
    $globalAggregation->addAggregation($categoryAggregation);
    $globalAggregation->addAggregation($manufacturerAggregation);

    $query->addAggregation($globalAggregation);

我想为manufacturer_idscategory_ids聚合添加一些自定义过滤器。目前,它们是从所有文件中汇总而来的。有没有办法通过Elastica API来实现它,以便它对它应用一些过滤?

1 个答案:

答案 0 :(得分:2)

我通过反复试验找到了它,它如下:

$categoryAggregation = new Terms('category_ids');
$categoryAggregation->setField('category_ids');
$categoryAggregation->setSize(0);

$filter = new Filter('category_ids', $merchantIdQuery);
$filter->addAggregation($categoryAggregation);

$globalAggregation = new GlobalAggregation('global');
$globalAggregation->addAggregation($filter);