basemkhirat / elasticsearch

The missing elasticsearch ORM for Laravel, Lumen and Native php applications
MIT License
401 stars 130 forks source link

Retain aggregation data retrieved from Elasticsearch #113

Open ametad opened 4 years ago

ametad commented 4 years ago

Hello Basem Khirat,

Your package helped me out a great deal, thank you! I like to use some extra data retrieved from Elasticsearch, some already included (but not all):

\Basemkhirat\Elasticsearch\Query

            $new->total = $result["hits"]["total"];
            $new->max_score = $result["hits"]["max_score"];
            $new->took = $result["took"];
            $new->timed_out = $result["timed_out"];  // typical Elasticsearch data
            $new->scroll_id = isset($result["_scroll_id"]) ? $result["_scroll_id"] : NULL;  // typical Elasticsearch data
            $new->shards = (object)$result["_shards"];  // typical Elasticsearch data

I would like to add this data so I can use this in my application:

            $new->aggregations = isset($result['aggregations']) ? (object) $result['aggregations'] : NULL;

Nothing further has to be changed in you package, but with this change aggregations can be retrieved as follows:


$rules = [
    'query' => [
        // ...
    ],
    'aggs' => [
        // ...
    ],
];

$result = MyModel::body($rules)->paginate();
$result->getCollection()->aggregations;
ametad commented 4 years ago

@basemkhirat The tests fail also without my additions to the code base. I hope the tests failing is not a problem for you accepting my PR?