Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

archy_bold's avatar

Laravel Scout + Algolia - Global Search on Multiple Entities

Hi there.

As the title suggests, I'm attempting to create a global search function that will search across multiple entities. I'll be using Algolia as the search engine and I figured Scout would be perfect for interaction with that. At least on the indexing.

Now the tricky part is how do I query on multiple entities. I guess I have a few options, but I'm struggling to see one clear strategy that will do it well.

I could just do the query on multiple entities and merge the results, but I'd want to sort by relevance, and as far as I can see there's no way to get the relevance through Scout, you just get the entity. Is that correct?

So maybe I need to avoid using Scout for the retrieval of records. But then should I use a single index for all entities? Can I specify multiple indexes to Scout so I index on an entity-specific index and a global index? Should I perform multiple searches through Algolia and attempt to merge and hydrate the results manually so as to keep access to the relevance/ranking information?

It feels like a bit of a rabbit hole and one that could get messy (sorry for the mixed metaphors!). I'm sure this someone must have done before and done in a more elegant and complete way than I've currently got in my head!

Thanks!

0 likes
10 replies
donkfather's avatar

sorry for making you access the link.. :) i am interested in this too .. so if you find something please let me know

archy_bold's avatar

Yeah, I saw that but the problem with that approach is that the results are separated based on which index they come from. So if you're using Scout, that's by individual entity.

My question was how can we get a single set of results with all entities, which is then sorted considering the relevance of all results.

So rather than getting results as follows:

  • Post, Post, Post, Post
  • User, User, User, User
  • Contact, Contact, Contact

We get them mixed together, sorted by relevance to the query:

  • Post, User, User, Contact, Post

Obviously you can just merge the results of the indexes according to your own strategy but I doubt any strategy I can come up with will match that of Algolia's.

1 like
cjwoodru2's avatar

I just jumped into something similar - for me searching projects and posts and I am using Laravel + Scout with Vue. It seems like a similar move could be done in the controller, as well. Like you said though, it is going to stack one index's results on top of another.

https://community.algolia.com/vue-instantsearch/advanced/multi-index-search.html#correlated-multi-index-searches

I just started with scout/algolia this past weekend so pardon the lack of knowledge. Is it possible to create one large index if Algolia pulls in relations? For instance, have you defined relations between you models? I read somewhere in the docs about doing so and I was curious if, once things were related Algolia would pull it all into a single index - effectively moving the need to search multiple index's.

Let me know if you figure this out as this is something I am working on, too!

archy_bold's avatar

I'm afraid I've since left the project and not used Algolia since. When I was working on it, nothing I read suggested that was possible.

You could always maintain a separate 'all models' index and update both the entity index and the 'all models' index.

But the problem with Scout is it separates models into different indexes. Unless that has also changed. You would then need to build a service or something similar to retrieve results from that index and hydrate those results to models.

lara15053's avatar

What I've been doing is setting multiple models to use the same global index. They all implement the searchableAs() method returning the same index and then I use the toSearchableArray() to create a common format.

Now I'm trying to figure out how to search this index and ideally have Eloquent hydrate each result using the correct model (maybe I can store the model name in Algolia?). I'd love to hear if anyone has figured this out.

ymssrikanth's avatar

Facing the same Problem. I want to merge two collections and paginate the results. Figuring a way to do it. I'm trying to import the two model data into the elastic search engine and query the results from the elastic search. How do I paginate the results? Go to know more about this.....

onyashed's avatar

Can this problem be solved from the DB level using a view Joining the several tables View_user_post_contact then calling links on the search view blade page. I am just thinking wildly...

Please or to participate in this conversation.