Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Composite Aggregation #21

Open
igoooor opened this issue Nov 3, 2022 · 3 comments
Open

Feature request: Composite Aggregation #21

igoooor opened this issue Nov 3, 2022 · 3 comments

Comments

@igoooor
Copy link
Contributor

igoooor commented Nov 3, 2022

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';
    }
}
@erichard
Copy link
Owner

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.

@igoooor
Copy link
Contributor Author

igoooor commented Nov 15, 2022

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

@erichard
Copy link
Owner

Yes ! You are totally welcome to make a PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants