elasticsearch-query-builder icon indicating copy to clipboard operation
elasticsearch-query-builder copied to clipboard

Feature request: Composite Aggregation

Open igoooor opened this issue 2 years ago • 3 comments

Hello there,

I believe that "composite aggregations" are currently missing in v3 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html

This is currently the class I built to handle that, it is very basic at the moment so all cases might not be covered. And I'm not sure if it follows the logic you have in your other aggregations but at least it might give an idea

<?php

declare(strict_types=1);

namespace Core\Search\Model\Search\DQL;

use Erichard\ElasticQueryBuilder\Aggregation\AbstractAggregation;

class CompositeAggregation extends AbstractAggregation
{
    public function __construct(
        string $name,
        private array $aggregations,
        private int $size = 10,
    ) {
        parent::__construct($name, []);
    }

    protected function buildAggregation(): array
    {
        $build = [];
        foreach ($this->aggregations as $aggregation) {
            $build[] = [
                $aggregation => [
                    'terms' => [
                        'field' => $aggregation,
                    ],
                ],
            ];
        }

        return [
            'sources' => $build,
            'size' => $this->size,
        ];
    }

    protected function getType(): string
    {
        return 'composite';
    }
}

igoooor avatar Nov 03 '22 22:11 igoooor

Hi !

The logic in v3 is:

  • Query and Aggregation MUST HAVE a constructor providing all required properties to build a valid object
  • getter/setter can be added for others properties

For your Composite aggregation the only required property is source which seems similar but yet different from a real aggregation. Size should be handle with getter/setter.

Although similar, the terms value source doesn’t support the same set of parameters as the terms aggregation.

erichard avatar Nov 14 '22 20:11 erichard

Would you like me to adapt it based on your comments and open a PR?

igoooor avatar Nov 15 '22 08:11 igoooor

Yes ! You are totally welcome to make a PR 👍

erichard avatar Nov 15 '22 18:11 erichard