Deleting a model record with "shouldIndex = false" shows errors
ramsaylee opened this issue · comments
Hi,
Thanks for this great module, nice job!
I have a model with "use Iverberk\Larasearch\Traits\SearchableTrait;" and "shouldIndex always returns false". When I want to delete a model record, the record is deleted, but it shows some errors related to Elasticsearch like this:
{"error":{"type":"Elasticsearch\Common\Exceptions\Missing404Exception","message":"{"found":false,"_index":"model_20150318093910","_type":"model","_id":"15","_version":1}","file":"C:\xampp\htdocs\my_project\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\GuzzleConnection.php","line":260}}
I guess it is related to "index" thing, I reindex this model with a crontab command every 2 minutes, if the record is indexed, the deleting works great while if the record is not indexed, the deleting works just as I descriped above.
The reason why I don't use the "auto-reindex" and make "shouldIndex always returns false" is that I cannot make sure the elasticsearch is always connected, when I make it "auto-index" and the elasticsearch is not connected, my app will be not working anymore. Everything related to the model will not be working(e.g. I have a User model with "use Iverberk\Larasearch\Traits\SearchableTrait;", when the elasticsearch service is down, I cannot even login to my app, so I have to add "shouldIndex always returns false")
Is there any advices for this situation? Thanks a lot!
If you don't understand the description, leave a comment to let me know.
Again, thanks a lot for this great module and your hard work.
Hi,
I just figured out that the deleting event fired the following method in Larasearch/Observer.php
public function deleted(Model $model)
{
// Delete corresponding $model document from Elasticsearch
Queue::push('Iverberk\Larasearch\Jobs\DeleteJob', [get_class($model) . ':' . $model->getKey()]);
// Update all related model documents to reflect that $model has been removed
Queue::push('Iverberk\Larasearch\Jobs\ReindexJob', $this->findAffectedModels($model, true));
}
I have a question here, why does this method not use shouldIndex() as the mothed below:
public function saved(Model $model)
{
if ($model::$__es_enable && $model->shouldIndex())
{
Queue::push('Iverberk\Larasearch\Jobs\ReindexJob', $this->findAffectedModels($model));
}
}
Thanks.
@ramsaylee seems to me that if a record was deleted it should be removed from the index no matter what ->shouldIndex()
results in. Also that related models should be re-indexed so they do no reference a non-existent record regardless.
@pfeiferchristopher Thanks for your kind comment. Got your point.
I will close this issue.