teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

One index for multiple models #44

Closed Remo closed 3 months ago

Remo commented 8 years ago

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?

blackfyre commented 8 years ago

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.

Remo commented 7 years ago

@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..

blackfyre commented 7 years ago

@Remo Nope, it fails for an entirely other reason. See #37 for the details.

Remo commented 7 years ago

I see, thanks for the hint!

RyanThompson commented 7 years ago

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.

blackfyre commented 7 years ago

Well, it's a long shot... but this is definitely a band-aid solution 😢

xtrasmal commented 7 years ago

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).

fastmover commented 7 years ago

this feature would be very very very useful.

mostafaznv commented 6 years ago

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];

xtrasmal commented 6 years ago

@mostafaznv it does not solve the problem of multiple models and duplicate id's