fadion / Bouncy

Map Elasticsearch results to Eloquent models
MIT License
71 stars 26 forks source link

should not replace this original data id with ES _id #17

Closed argb closed 9 years ago

argb commented 9 years ago

I don't think this is a good way: $attributes = $hit['_source']; $attributes['id'] = $hit['_id']; and if you search like this: Question::search(['query'=>['match'=>['_all'=>'beijing']],'_source'=>['location','title','body']]); I originally don't want to get "id",but now I was forced got it ,in this case,it should be as a bug.

fadion commented 9 years ago

The _id field should be present even if you haven't created it in ES, so no errors should arise. Are you getting any error? What do you mean that you didn't want to get id, but now you're forced to get it?

argb commented 9 years ago

@fadion I mean,that query's result was supposed not to have id,but i got it,cause id is is forced added from $hit. In addition,_id from ES must be same as id from database? If i need use id in business logic would may get error or wrong data.

fadion commented 9 years ago

Bouncy creates a 1-to-1 mapping between ES and the database. In that way, it provides the convenience of Eloquent models, but without hitting the database on searches. When data is indexed via the index() method, the _id field is set the same as the row's id. For consistency, that _id is put back to the model when data is retrieved from ES.

Now that you mentioned it, I believe that using the _id in such a way can be problematic to those who index data manually and don't follow Bouncy's way of indexing. Will remove it.

argb commented 9 years ago

@fadion Thank you .

igor-kamil commented 9 years ago

Hello! Well. I am unsure whether it was good idea - to remove

$attributes['id'] = $hit['_id'];

I index also manually. Using Bouncy for searching - and I like that I receive back eloquent model. But my methods within model rely on having set $attributes['id'] And I think in most cases it is more useful,to keep setting $attributes['id'] automatically.

Maybe it can be part of the config - to make it possible to disable auto setting $attributes['id']

What do you think?