pdphilip / laravel-elasticsearch

Laravel Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
MIT License
86 stars 16 forks source link

Please consider these two changes #25

Closed kenken-vpl closed 4 months ago

kenken-vpl commented 5 months ago
  1. Elasticsearch local deployment might also access via APIKEY.
  2. date_histogram is a very powerful aggregation function in Elasticsearch and rawSearch() seems the current only method to perform such a search without violating the norm of Eloquent ORM. So I suggest allowing rawSearch() to perform raw construct aggregations and get the values back.
pdphilip commented 5 months ago

Thanks @kenken-vpl - the PR's look good, but I would like to add this rawSearch() variant to my tests, could you provide me with some query examples (Ideally 3 if you can)? Then I'll take them from there.

Confirming that you're on Laravel 11, yes?

Happy to accept the APIKEY commit once the tests clear with rawSearch()

kenken-vpl commented 4 months ago

Dear David Philip,

Thanks for considering these two enhancements.

About rawSearch below DSL is an example on my data. It aggregates by a 5-minutes interval, for records matching a particular serial number (sn) and category, with time range filtering.

I’ve also included the PHP code fragment that extracts the labels for time axis and the aggregate data.

Most of our projects were still using Laravel 10.x but new ones including this starts moving to Laravel 11.x.

Best Regards,

Ken

    {
        "aggs": {
            "result": {
                "date_histogram": {
                    "field": "ts",
                    "fixed_interval": "5m",
                    "time_zone": "+08:00"
                },
                "aggs": {
                    "avg": { "avg": { "field": "AH" } }
                }
            }
        },
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    { "match": { "sn": "aabbccddeeff" } },
                    { "match": { “category": "r" } }
                ],
                "filter": [
                    {
                        "range": {
                            "ts": {
                                "gte": "2024-04-18T00:00:00Z",
                                "lt": "2024-04-19T00:00:00Z"
                            }
                        }
                    }
                ]
            }
        }   
    }

    $agg = self::rawSearch($q)
    $axis = [];
    $data = [];
    foreach ($agg['result']['buckets'] as $row) {
        $axis[] = $row['key_as_string'];
        $data[0][] = rand(0, 100); // $row['avg']['value'];
        if (is_array($field)) {
            for ($i = 1; $i < count($field); $i++) {
                $name = "avg_$i";
                $data[$i][] = rand(0, 100); // $row[$name]['value'];
            }
        }
    }

    return [
        'axis' => $axis,
        'data' => $data,
    ];

On 9 Apr 2024, at 19:17, David Philip @.***> wrote:

Thanks @kenken-vpl https://github.com/kenken-vpl - the PR's look good, but I would like to add this rawSearch() variant to my tests, could you provide me with some query examples (Ideally 3 if you can)? Then I'll take them from there.

Confirming that you're on Laravel 11, yes?

Happy to accept the APIKEY commit once the tests clear with rawSearch()

— Reply to this email directly, view it on GitHub https://github.com/pdphilip/laravel-elasticsearch/pull/25#issuecomment-2044781590, or unsubscribe https://github.com/notifications/unsubscribe-auth/BHKW7NSATMDAQC7BUKICB5LY4PE5PAVCNFSM6AAAAABF6FAAV2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBUG44DCNJZGA. You are receiving this because you were mentioned.

pdphilip commented 4 months ago

@kenken-vpl , having looked into these, my approach is to keep aggregations separate from search queries. Mainly because search comes back to hydrate a model where as aggs usually just bring back a result.

To that, I'll keep rawSearch() for searching and have created a new method for raw aggs rawAggregation() which will format the results - on dev branch now for you to try: https://github.com/pdphilip/laravel-elasticsearch/tree/dev

I updated this branch with your APIKEY suggestions as well.

Please test this and let me know

pdphilip commented 4 months ago

Hi @kenken-vpl - the new release has rawAggregation()

See: https://elasticsearch.pdphilip.com/es-specific#raw-aggregation-queries