Searching with two attributes and different priority, while one as an array

Hi all!
So I’m dealing with a dataset that looks like that:

{ id: 1, “title”: “Dog”, “alternativeTitle”: [“Haskey”, “Poodle”, “Dodo”] },
{ id: 2, “title”: “Animal”, “alternativeTitle”: [“Dog”, “Cat”] },

And what i’m trying to achieve, is that searching will have first priority at the “title” field, and the second priority will be one of the results in the “alternativeTitle” array.
So basically each of the fields (title and alternativeTitle) can return is a legit result (including instance inside of the array), but it will also be the only result that is returned.

Meaning that If I’ll type “Do”, ill have the next output:

{ id: 1, output: “Dog” },
{ id: 2, output: “Dog” }


  1. Id 1, has dog in the title, so it has more priority then id 2, which only has “dog” on the alternative titles.
  2. Id 1 only gives back only “dog” from its title, without “dodo” from its alternative title, because it only shows the top scored title from that object (like in faceting).
  3. Id 2 only gives back “dog”, because it has that in the “alternative title” field, and there only, so its title doesn’t need to be returned.

Is that possible to achieve?

Hi @it15,

Algolia returns records, not attributes, so the records will be found based on the query and your search criteria.

In your example if you search by ‘Do’, it will find all records with ‘Do’ (or a record with a typo, depending on your configuration) in any searchable attribute. It will then rank those records based on your ranking criteria.

So, in your example, both records will be found, but if you have placed the ‘title’ attribute higher in your searchable attributes list, the search engine will return the record with ‘id’ 1 first in the results. Record 2 would be shown afterwards because ‘Do’ is also found in the ‘alternativeTitle’ attribute.

You can choose which attributes to show from each of those attributes when you display the results.

1 Like