Faceting with multiple OR filters matching, but not ranking up correctly

I have two items, which have

Item 1:

{
type: [‘lunch’, ‘dinner’]
}

Item 2:

{
type: [ ‘dinner’]
}

If I search using the following query:

const results = await index.search(query, {
  facetFilters: ['type:lunch', 'type:dinner']
});

the query works and I get the two items, but item 1, which is a better match, doesn’t show up first… Is this the normal behavior? How can I rank up the items that match more OR facet values?

Hi there!

What you’re describing is indeed the normal behavior. The engine doesn’t check which record matches a filter on the most points, but just if it matches or not. After that, your custom ranking kicks in to determine the order of the records. This is most likely what causes record 2 to show up before record 1.

That being said, it is possible to achieve the behavior you want. You can do this by leveraging optionalFilters. optionalFilters boost records that match the filters in it, without removing records that don’t match it. This, combined with the facetFilters you already have, allows you to boost results that match both filters. This would look something like this:

const results = await index.search(query, {
  facetFilters: ['type:lunch', 'type:dinner'],
  optionalFilters: [
    ['type:lunch', 'type:dinner']
  ],
});

The code above will filter all your results (as you currently have), and the optionalFilters will promote all the items that have type:lunch and type:dinner in their attributes. Doing this will cause result 1 to show up before result 2.

I hope this helps, let me know if you have any other questions!

That’s great. Your explanation matches the behavior I’m experiencing, I’m going to give the optionalFilters a try. From what I understand I basically need to pass the exact same facetFilters there.

Works great, thank you once again!

As a side note, your exact snippet didn’t work:

  optionalFilters: [
['type:lunch', 'type:dinner']
  ],

With this they rank equally and the custom ranking boosted the wrong item, I had to flatten it out so the one that had both tags was boosted over the custom ranking:

  optionalFilters: ['type:lunch', 'type:dinner']
1 Like