Filter Scoring
As well as refining results, you can use filtering to rank records according to how well or how poorly they match a set of filters: this is called filter scoring. Scoring applies numerical settings to filter values, making some filter values more important than others. Records with the highest filter values sit at the top of the list of results.
Consider the case of stock portfolios in which companies are scored according to their monetary significance. Filter scoring can be used to rank Google higher than Facebook. For example:
1
2
3
index.search('', {
filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
});
The result is that records with Google stocks sit at the top of the list, higher than Amazon and Facebook.
How scoring is calculated
Filter scores are integer values from 0 to 65535.
Default scoring
In the preceding example, any portfolio containing all three companies (Google, Amazon, and Facebook) would score 3
, as the total is based on the highest score. In other words, by default, there is no accumulation of the individual scores.
Accumulating scores with sumOrFiltersScores
You accumulate scores by setting the sumOrFiltersScores
parameter to true
. In the preceding example, any record with all three companies would have a total score of 6 (3+2+1).
If sumOrFiltersScores
is false
, the default, the system uses the default scoring method: taking the highest score. Using the preceding example and a query filtered on Google and Amazon with sumOrFiltersScores
= false
returns 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
returns 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
Use the OR
operator when you want to weigh terms differently.
- When filtering with
OR
, the total score is based on the individualtrue
value scores. If you have three filter values in your query and all three match, 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. With a group of ANDs, records are chosen only if all filters match: all records will have the same score.
Scoring using numeric filters
You can’t apply scores when using numeric filters (like >=
, !=
, >
). Scoring can only be done on facet values, using the attribute:value<score=X>
syntax.