Skip to content
Snippets Groups Projects
AggsFacetTerms.php 960 B
Newer Older
Pavel's avatar
Pavel committed
<?php

namespace IQDEV\ElasticSearch\Search\Aggs;

use IQDEV\ElasticSearch\Search\Nested;

Pavel's avatar
Pavel committed
final class AggsFacetTerms
Pavel's avatar
Pavel committed
{
    public static function create(string $code, string $facet, string $path = 'search_data'): Aggs
    {
        $aggKeywordFacet = new Aggs($code);
        $nested = new Nested();
        $nested->setPath($path . '.' . $facet);
        $aggKeywordFacet->setNested($nested);

        $aggKeywordFacetCode = new Aggs("agg_{$facet}_code");
        $aggKeywordFacetCode->setTerms(
            (new Terms("{$path}.{$facet}.facet_code"))
Pavel's avatar
Pavel committed
                ->setSize(250)
        );
        $aggKeywordFacetValue = new Aggs("agg_{$facet}_value");
        $aggKeywordFacetValue->setTerms(
            (new Terms("{$path}.{$facet}.facet_value"))
                ->setSize(250)
        );

        $aggKeywordFacetCode->addAggs($aggKeywordFacetValue);
        $aggKeywordFacet->addAggs($aggKeywordFacetCode);

        return $aggKeywordFacet;
    }
}