Concepts / Managing results / Filter scoring
Sep. 26, 2019

Filter Scoring

Filters Scoring

You can use filtering not only to refine results but also to rank records according to how much or how little they match a set of filters. This is done with filter scoring. Algolia provides the ability to score filter values, making some filter values more important than others. This impacts the results because records that match the highest filter values will be put at the top of the results.

Consider the case of stock portfolios in which companies are scored according to their monetary significance within the portfolios. If we make Google shares more valuable than those of Facebook, we’ll need to score Google higher than Facebook. By leveraging filter scoring, it’s possible for Algolia to rank items with Google higher than items with Facebook.

Here’s what this query might look like:

1
2
3
index.search({
  filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
});

The end result is that records with google stocks are put at the top of the list, higher than Amazon and Facebook.

How scoring is calculated

Default Scoring

Note that, in the above example, any portfolio that contained all three of these companies (Google, Amazon, and Facebook) would still have a score of only 3, as the total score is based only on the highest score. In other words, by default, there is no accumulation of the individual scores.

Accumulating scores with sumOrFiltersScores

You can change this default by using the sumOrFiltersScores setting. If this setting is set to true, the total score is an accumulation of the filters. In the above example, any record with all three companies would have a total score of 6 (3+2+1).

If not specified, this parameter is false. When the parameter is false, the system uses the default scoring method, that of taking the highest score.

Another example is a query filtered on “google amazon” with sumOrFiltersScores = false. This will return a score of 3 (google(3) > amazon(2)).

1
2
3
4
index.search("", {
  filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
  sumOrFiltersScores: false
});

The same query with sumOrFiltersScores = true will return a score of 5 (google (3) + amazon(2)).

1
2
3
4
index.search("", {
  filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
  sumOrFiltersScores: true
});

Scoring ANDs and ORs

Scoring only makes sense when you want to weigh terms differently, which is achieved when using the OR operator. With the OR, each filter is potentially true or false, and the total score will be based on the individual scores of the true values. So if you have 3 filter values in your query and all three values matches, it will have a higher score than another record that matches only one filter value.

Filtering only with ANDs will remove the effect of scoring. Essentially, with a group of ANDs, records are chosen only if all filters match, and so all records will end up having the same score.

Scoring using numeric filters

It is not currently possible to apply scoring when using numeric filters (like >=, !=, >, etc). Scoring can only be done on facet values, using the attribute:value<score=X> syntax.

Did you find this page helpful?