Identical word in two different attributes. Mixed Search Possible?

Is there a way to achieve the following?

We have two searchable attributes, station_name and country_name.
We ordered the searchable attributes by station_name and then country_name

Let’s say someone searches “Ghana.”

Would it be possible to return the first three hits as matches on the station_name (like stations called “Ghana Waves” and “Ghana Urban Radio” would show up) and then in the same “hits dropdown” after the first three hits return matches of “Ghana” on the country_name attribute of the station regardless of whether it has the actual word “Ghana” in it.

The purpose of this would be to show the top Ghanaian stations in order of popularity after the top 3 matches on the actual station_name with the word “Ghana” in it. When people search the word “Ghana” we want to show not only the exact station_name matches but the top Ghanaian stations as well.

Is this feasible?

Hi Chaim,

Welcome to the Algolia Community!

You can show the top matches in the station_name attribute and then the matches in the country_name attribute by setting the searchable attributes on the index.


You can then rank them further by placing a Custom Ranking by a ‘popularity’ attribute.

I’m assuming your records have the following attributes or something similar:

“station_name”: “Ghana Waves”,
“country_name”: “Ghana”,
“popularity”: 1,
“objectID”: “17”

To limit the results from the station_name to 3 and then limit the results from the country_name to 3 (or some other number), one option is to use replicas with a different sort order, placing the country_name above the station_name in the Searchable Attributes.

To get the desired results, you would need to use the Algolia API (I’m using the php API client in the following example, but you could use any of the other API clients).

You would need to first create the replica:

    'replicas' => [

Then in your search code have something like this:

$client = Algolia\AlgoliaSearch\SearchClient::create(

$index = $client->initIndex('indexname');
$replica = $client->initIndex('indexname_country');

$results = $index->search($search_term);   //Ghana

$hits = array_slice($results['hits'],0,3);
foreach ($hits as $station) {
    print($station['station_name'].' '.$station['country_name']."\n");

$results = $replica->search($search_term);  //Ghana

$hits = array_slice($results['hits'],0,3);
foreach ($hits as $station) {
    print($station['station_name'].' '.$station['country_name']."\n");

Let us know how this works for you!

1 Like

This solution works well, thank you!

However, instead of just printing the hits, is there a way to combine the two index results in a single dropdown when users are searching or some other seamless way of integrating both hits results?

I’ve looked at “Search Multiple Indices” in the Algolia Docs, but was wondering:

  1. If I even need to search multiple indices when using replicas of the same index, and
  2. If there was an easier way to integrate the two results so that I can show the user the results from both searches at the same time.

Thank you for your help!

You could build your own using one of the API clients.

Again, you would need to build this with one of the API clients using the same concepts I used above, searching from each index.

Keep in mind, a replica has the same records as the master index. In my example, both indexes contain the same records, they are just sorted differently based on the searchable attributes. So, these aren’t really different indexes, they just have different sort orders so that the sorting is done at index time to make the search faster.

I hope that helps!

1 Like