ErickTamayo / laravel-scout-elastic

Elastic Driver for Laravel Scout
MIT License
916 stars 242 forks source link

Problem with Eloquent model _id field #85

Closed denudge closed 3 years ago

denudge commented 6 years ago

Making a first test with Laravel 5.5 scout (and jensseger/mongodb moloquent as base models), I found none of my documents has been indexed, no matter what I do.

I could track down the problem (covered by #37) to this root cause: The default eloquent toArray() function returns the id field with leading underscore. [ "_id" => "59f37e9b8ba21650953e7272", "name" => "some name", "description" => "some description", "updated_at" => "2017-10-27 18:44:43", "created_at" => "2017-10-27 18:44:43", ]

So, ES throws the following error: {"_index":"scout","_type":"testmodel","_id":"59f37e9b8ba21650953e7272","status":400,"error":{"type":"mapper_parsing_exception","reason":"Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters."}}}]}

Mapping the _id field to "id" within a custom toSearchableArray() method fixes the problem.

This issue is related exclusively to ES expecting special fields with underscores. So, within the chain of Eloquent, Scout, your driver and ES it should be fixed here, I think.

reinierkors commented 6 years ago

You can't use _id in your models with elasticsearch because it's a mapping field: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-fields.html

Change _id to id in your models or remap it like you did.