Order results by number of facets matched

I have products with tags array. How do I rank the results of products with most number of facets matching first then the rest after that?

I have added tags.tagName as my facet.

Example of Product:

objectID : "5398"
name : "Blue Dinosaur Banana *Bread* Paleo Bar"
lowestPrice: 5.99
tags : [
{ tagTypeName: "Special Diets", tagName: "Vegetarian", description: "Vegetarian", tagId: "96", systemTag: true },
{ tagTypeName: "Special Diets", tagName: "Vegan", description: "Vegan", tagId: "87", systemTag: true },
{ tagTypeName: "Allergies", tagName: "Dairy-Free", description: "Dairy-Free", tagId: "262", systemTag: true },
{ tagTypeName: "Nutrition/Marketing Claims", tagName: "Refined Sugar-Free", description: "Refined Sugar-Free", tagId: "260", systemTag: false },
{ tagTypeName: "Intolerances", tagName: "Gluten Free", description: "Gluten Free", tagId: "86", systemTag: true }
]
typeName : "Product"
4 Likes

+1 on this. Have an array of meta-tags for every entry.

{
  objectID: 1
  classifications: ['metaTag1', "metaTag2', 'metaTag3']
},
{
  objectID: 2
  classifications: ['metaTag1', "metaTag4', 'metaTag5']
},

During api-search we pass in an array of these meta-tags and use algolia to return the items matching at least one of these.

indexSearch('', {
  facetFilters: [
     classifications: ['metaTag1', 'metaTag2', 'metaTag3'] 
  ]
})

This search should (and currently does) return both the objects 1 and 2, but I’m looking for a way to rank the Object 1 higher since the query matches all the classifications.

Looking for a way to rank the response, so that the first item is the one with the highest number of matches on the classification-tag.

UPDATE:

Got a response from Algolia support saying that they do not support this kind of ranking.
We resolved it by sorting the response ourselves.

4 Likes

Hey Simon, thanks for the response, if you wouldn’t mind sharing your approach to how you sorted it yourself? Does it work with Algolia’s pagination?

I built a custom javascript-sorting method and use this in as part of the node-api when I fetch data. This just sorts the data fetched, so it won’t work with Algolias pagination, you would have to fetch all the data, then sort it and return a slice of it for each page.

Scoring each item in my data from algolia based on how matching hits it had on the array of in my case selected classifications by the user.

In my case this approach took < 1ms to sort 100 items so not really any performance impact.
Hope it helps :slight_smile:

/*
  selectedClassifications: Array of strings, the options selected by the user
  hits: data received from algolia (array)
*/
const sortByClassificationHits = (selectedClassifications, hits) => {
  const sorted = hits.sort((prev, next) => {
    const numberOfMatches = (taggedClassifications) => {
      return taggedClassifications.filter(
        taggedClassification => selectedClassifications.includes(taggedClassification)
      ).length;
    };
    const prevMatchCount = numberOfMatches(prev.classifications);
    const nextMatchCount = numberOfMatches(next.classifications);
    return nextMatchCount - prevMatchCount;
  });
  return sorted;
};
1 Like