2

I have elastic query built with ruflin/Elastica, with global aggregation. Is it possible to somehow add some filters to it, separate from my main query.

It looks like so:

    $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);

I would like to add some custom filters to manufacturer_ids and category_ids aggregations. At the moment they are aggregated from all documents. Is there any way to do it via Elastica API, so that it applies some filtering to it?

galdikas
  • 1,609
  • 5
  • 19
  • 43

1 Answers1

2

I found it myself through trial and error, it goes as following:

$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);
galdikas
  • 1,609
  • 5
  • 19
  • 43