Skip to main content
VeloDB Cloud 26.x is based on Apache Doris 4.x. See Version Mapping.

Relevance Scoring

Relevance scoring ranks full-text search results so that the most relevant records appear first. For queries using predicates such as MATCH_ANY, MATCH_ALL, and MATCH_PHRASE, VeloDB Cloud assigns each matching row a numeric score based on the query conditions. A higher score indicates stronger relevance.

VeloDB Cloud currently uses the BM25 (Best Matching 25) algorithm as the default implementation for relevance scoring.

Typical use cases:

  • Search engine applications: return the most relevant document list based on keywords
  • Log analysis: sort by relevance to quickly locate the logs that best match the keywords
  • Content recommendation: filter priority content based on text similarity
  • Knowledge base retrieval: return candidate answers ranked by question relevance

Quick start​

The following query assumes a populated search_demo table with a tokenized inverted index on content. It projects score() and orders the matching rows by that score:

SELECT *,
score() AS relevance
FROM search_demo
WHERE content MATCH_ANY 'text search test'
ORDER BY relevance DESC
LIMIT 10;

The following upstream sample output illustrates the result shape. Scores and row order depend on the indexed data and query:

+------+-----------------------------------+---------+--------------+-----------+
| id | content | author | publish_date | relevance |
+------+-----------------------------------+---------+--------------+-----------+
| 1 | Full text search engine test demo | Alice | 2024-01-01 | 2.915228 |
| 7 | Text processing techniques | Grace | 2024-01-07 | 1.341931 |
| 5 | Performance test framework | Eve | 2024-01-05 | 1.341931 |
| 3 | Advanced search algorithms | Charlie | 2024-01-03 | 1.341931 |
+------+-----------------------------------+---------+--------------+-----------+

Activation conditions​

For scoring and minimum-score filtering, use a single-table Top-N query that meets all three conditions:

  1. The SELECT clause explicitly calls score() and assigns it an alias.
  2. The same SELECT contains at least one MATCH_* or SEARCH() predicate in WHERE.
  3. ORDER BY has a single key referencing the score alias, and the query has LIMIT.

Filter by relevance score​

First prepare a separate example table with a tokenized inverted index. Run the following statements with permission to create the database and table, load data, and query it:

CREATE DATABASE IF NOT EXISTS score_filter_example;
USE score_filter_example;

CREATE TABLE score_filter_demo (
id BIGINT,
content TEXT,
INDEX idx_content (content) USING INVERTED
PROPERTIES("parser" = "english", "support_phrase" = "true")
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1;

INSERT INTO score_filter_demo VALUES
(1, 'Full text search engine test demo'),
(2, 'Database performance analysis'),
(3, 'Search engine examples');

Starting from VeloDB Core 26.0.2, you can filter the minimum relevance score directly in WHERE. The documented VeloDB Core 26.1 series also supports this query form:

SELECT *, score() AS relevance
FROM score_filter_demo
WHERE content MATCH_ANY 'text search test'
AND score() > 5
ORDER BY relevance DESC
LIMIT 10;

This returns up to 10 matching rows with a score greater than 5, ordered by score from highest to lowest. If no matching row passes the threshold, the result is empty. Scores depend on the query and corpus, so 5 is only an example threshold.

Use score() itself in WHERE and the select-list alias in ORDER BY. Direct filtering supports one minimum-score predicate, score() > <threshold> or score() >= <threshold>, where the threshold is a numeric literal. Combine it with the search predicate using a top-level AND. Upper bounds (<, <=), equality (=), multiple score predicates, and score predicates nested inside OR are not supported.

Compatibility with VeloDB Core 26.0.1​

VeloDB Core 26.0.1 does not allow score() in WHERE. On this version, first select a bounded set of scored candidates in a subquery. Keep the MATCH_* predicate, ORDER BY on the score alias, and LIMIT inside that subquery. Then filter the alias in the outer query:

SELECT a.*
FROM (
SELECT *, score() AS relevance
FROM score_filter_demo
WHERE content MATCH_ANY 'text search test'
ORDER BY relevance DESC
LIMIT 1000
) AS a
WHERE a.relevance > 5
ORDER BY a.relevance DESC
LIMIT 10;

This returns up to 10 rows with a score greater than 5 from the top 1000 candidates. It returns no rows if none of those candidates passes the threshold. Choose the candidate limit for your application. This pattern filters that candidate set rather than returning every matching row above the threshold.

In VeloDB Core 26.0.1, keep the inner ORDER BY and LIMIT to provide the required Top-N operation. An outer ORDER BY and LIMIT alone do not supply the required inner Top-N operation.

Supported index types​

Index typeScoring supportedDescription
Tokenized inverted indexYesCan compute BM25 relevance scores
Non-tokenized inverted indexNoSupports only exact matching and does not compute scores

Supported query types​

  • MATCH_ANY
  • MATCH_ALL
  • MATCH_PHRASE
  • MATCH_PHRASE_PREFIX
  • SEARCH()

BM25 algorithm​

BM25 is a text relevance algorithm based on a probabilistic model. It performs a weighted calculation on matching results by considering three factors together: term frequency (TF), inverse document frequency (IDF), and record length. Compared with the traditional TF-IDF model, BM25 offers better robustness and tunability, and effectively balances the score difference between long and short text.

Core formula​

score = IDF × (tf × (k1 + 1)) / (tf + k1 × (1 - b + b × |d| / avgdl))

The variables in the formula are defined as follows:

VariableMeaning
tfNumber of occurrences of the query term in the current row
IDFInverse document frequency, which measures how rare the term is
|d|Length of the current row (number of tokens after tokenization)
avgdlAverage length of all rows in the table
k1, bAlgorithm tuning parameters

Parameters​

ParameterDefaultDescription
k11.2Controls how much term frequency influences the score
b0.75Controls the strength of record-length normalization
boost1.0Query-level weight factor

Auxiliary statistics​

IDF = log(1 + (N - n + 0.5) / (n + 0.5))
avgdl = total_terms / total_rows

Where:

  • N: total number of rows in the table
  • n: number of rows that contain the query term

When a query contains multiple terms, the final score is the sum of the scores of each term.

Record length and the norms property​

VeloDB Core 26.1.4 adds the norms index property. The related inverted_index_skip_norms_for_variant setting is managed by the service.

|d| comes from norms, which cost one byte per row per indexed field, including rows with no value for that field. Tokenized indexes write norms by default ("norms" = "true"); non-tokenized indexes do not. For indexes used only for filtering, set "norms" = "false" to save space:

INDEX idx_content(content) USING INVERTED PROPERTIES("parser" = "english", "norms" = "false")

VARIANT subpaths inherit the index's norms property, costing rows × paths bytes. The managed configuration inverted_index_skip_norms_for_variant initializes to false in the Core 26.1.4 source. When enabled, it disables norms for VARIANT path indexes and overrides the index property. Contact VeloDB Cloud support to evaluate this setting. It is not a user-managed compute-node configuration.

Disabling norms does not affect MATCH_* filtering, but score() returns an error if it reads any segment without norms. These settings affect newly written segments; existing segments remain unchanged until compaction rewrites them. The service manages compute-node upgrades. Confirm the upgrade and segment-rewrite plan with VeloDB Cloud support before changing norms behavior.

Interpret the results​

Understanding the scoring results helps you use relevance ranking more accurately:

CharacteristicDescription
Score rangePositive number with no fixed upper bound. Usually only relative magnitude matters, not the absolute value.
Multi-term queryWhen multiple terms are involved, the total score equals the sum of the per-term scores
Length effectGiven the same matching terms, a shorter record receives a slightly higher score
No matching termIf the query term does not appear in the table, the corresponding score is 0

FAQ​

Q1: Why does my query not return scores?

Check the following in order:

  1. Verify that the same SELECT projects score() with an alias.
  2. Verify that its WHERE contains a MATCH_* or SEARCH() predicate.
  3. Verify that ORDER BY uses only the score alias and the query has LIMIT.
  4. Verify that the index is a tokenized inverted index.

Q2: What is the difference between BM25 and TF-IDF?

BM25 builds on TF-IDF by introducing two improvements: term frequency saturation and length normalization. These make the scores of long and short text more balanced and provide better robustness.

Q3: Can scores be compared across queries?

This is not recommended. BM25 scores depend on the specific query terms and the data distribution in the table. Absolute values are not comparable between different queries. Compare relative magnitudes only within the result set of the same query.