pdphilip / laravel-elasticsearch

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

[Implemented] _index available in results #2

Closed andrei376 closed 2 years ago

andrei376 commented 2 years ago

https://github.com/pdphilip/laravel-elasticsearch/blob/c810b1ebef688af30c730f7a738460564b61e6ed/src/DSL/Bridge.php#L620

please also keep '_index' field in the results

i've added $datum['_index'] = $hit['_index'];

this is useful when working with dynamic indices.

i use in search $index = '.ds-filebeat-*', but to delete a record i need to know the exact index (".ds-filebeat-8.2.0-2022.05.21-000001")

pdphilip commented 2 years ago

This is an excellent use case. The problem with returning the '_index' field as data is that it will be added to the collection and Laravel will save it if you update the active collection; _index is a protected field name in ES so it will throw an error when you do.

Anyway, I've updated the plugin with a better solution to work with dynamic indices, on model hydration the index gets updated on lookup, so you can update and delete the record directly (without having to look up which index it lives in).

If you need to see the record's index anyway, you can use: $myRecord->getRecordIndex()

For creating a new record, you'll need to specify the index with $theModel->setIndex('models_'.$date);