laravel-scout-tntsearch-driver
laravel-scout-tntsearch-driver copied to clipboard
One index for multiple models
I've got a project where I have news and comments. I managed to get everything up and running in no time, but I can't figure out the best way to search in multiple models at once.
Right now I have an index per model. I guess I could overwrite that, but then I would have duplicate ID's. I could then create a custom index like news-1, comment-1, but that feels awfully ugly.
What's the best practice to search in multiple models?
I'm also interested in this also!
One solution could be that if the models are related, like in the news-comments case, one can eager load the whole thing and index that.
@blackfyre did you make any progress? Have you tried to implement your idea? I think I'll try to build an index with a key using a prefix to determine the class. I think that's more generic as it doesn't require unnecessary database models, but I might fail..
@Remo Nope, it fails for an entirely other reason. See #37 for the details.
I see, thanks for the hint!
This might be a trash idea but what about a model solely for search that uses a polymorphic relation for the entry relation and a JSON column or something for the searchable payload? We lose some matching features though since columns per attribute are not well represented. But that would get you a temporary workaround.
But based on how the internal search mechanism works for Scout I am not entirely sure how this will be possible without some refactoring in the guts of Scout first.
Well, it's a long shot... but this is definitely a band-aid solution 😢
Juat an idea, but a solution could be a search query that joins all the fields from all the models that you need into a single result. Then do some polymorphic relationship stuff I you get my drift. So... a result table that has it's own id, but also model_id(the id of the model in it's table) model_type(this can be the classname).
this feature would be very very very useful.
I solved this problem by this trick:
public function toSearchableArray()
{
$array = [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
];
if (!empty(self::$tags))
$array = array_merge($array, self::$tags);
return $array;
}
when I want to create or update a model, I pass values to static variable:
Model::$tags = ['tags' => $request->tags];
@mostafaznv it does not solve the problem of multiple models and duplicate id's