pdphilip / laravel-elasticsearch

Laravel Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
https://elasticsearch.pdphilip.com/
MIT License
95 stars 17 forks source link

rawAggregation returns only the last aggregation instead of all aggregations #34

Closed cristian-araujo closed 3 months ago

cristian-araujo commented 3 months ago

Package Version

4.0.3

Describe the Bug

The rawAggregation method is only returning the last aggregation result, instead of returning all specified aggregations.

To Reproduce

Here is the code that demonstrates the issue:

$body = [
    'query' => 'match_all',
    'size' => 0,
    'aggs' => [
        'brands' => [
            'terms' => [
                'field' => 'brand',
                'size'  => 100,
            ],
        ],
        'price_ranges' => [
            'range' => [
                'field' => 'price',
                'ranges' => [
                    ['to' => 10000],
                    ['from' => 10001, 'to' => 20000],
                    ['from' => 20001, 'to' => 50000],
                    ['from' => 50001],
                ],
            ],
        ],
        'fuel_types' => [
            'terms' => [
                'field' => 'fuel',
                'size'  => 100,
            ],
        ],
        'transmission_types' => [
            'terms' => [
                'field' => 'transmission',
                'size'  => 100,
            ],
        ],
    ],
];

$results = VehicleElastic::rawAggregation($body);
dd($results);

Issue Explanation

The query above defines multiple aggregations (brands, price_ranges, fuel_types, and transmission_types). The rawAggregation method should return the results for all specified aggregations, but currently, it only returns the result for the last aggregation (transmission_types in this case).

Expected Behavior

The expected behavior is that the rawAggregation method should return the results for all specified aggregations as part of the response.

Component Versions

Please find the relevant versions from the composer.json file:

"require": {
    "php": "^8.2",
    "laravel/framework": "^11.9",
    "laravel/tinker": "^2.9",
    "league/flysystem-aws-s3-v3": "^3.28",
    "pdphilip/elasticsearch": "^4.0",
    "spatie/laravel-medialibrary": "^11.8",
    "tucker-eric/eloquentfilter": "^3.4"
}
pdphilip commented 3 months ago

Thanks for flagging @cristian-araujo - this will be fixed in the next release.

pdphilip commented 3 months ago

Hi @cristian-araujo - this has been fixed in the latest release: https://github.com/pdphilip/laravel-elasticsearch/releases/tag/v4.0.4