Fine sort expressions score the candidates from rough sort and return the highest-ranking results. Define custom sort expressions per application and pass them in search queries to control how results are ranked. Sort expressions support basic operations (arithmetic, relational, logical, bitwise, and conditional), mathematical functions, and the feature functions described on this page.
Basic operations—arithmetic, relational, logical, and conditional—require numbers or field values of the NUMERIC type. Most function-based operations cannot be applied to values of the STRING type.
Text relevance
Use text relevance functions to measure how closely a document's content matches the search query.
| Function | What it returns |
|---|---|
text_relevance | Text relevance score between the search query and a field value |
field_match_ratio | Ratio of matched terms in a field to the total number of terms in that field |
query_match_ratio | Ratio of query terms that hit a specific field to the total number of terms in the search query |
fieldterm_proximity | Proximity of matched terms within a field |
field_length | Number of terms in a field |
query_term_count | Number of terms in the search query after analysis |
query_term_match_count | Number of query terms that hit a specific field in a document |
field_term_match_count | Number of terms in a field that match the search query |
query_min_slide_window | Ratio of query terms that hit a specific field to the minimum window that contains those terms in the field |
Geographical location relevance
Use geo functions to score documents by proximity to a reference point. The three decay functions (gauss_decay, linear_decay, exp_decay) share the same underlying concept—they calculate a score that decreases as values move further from a specified origin—but differ in the shape of the decay curve (Gaussian bell curve, linear, or exponential). Choose the curve that best matches how steeply you want scores to drop with distance.
| Function | What it returns |
|---|---|
distance | Spherical distance between two geographic points; commonly used in location-based service (LBS) scenarios |
gauss_decay | Decay score using a Gaussian (bell curve) function based on distance from a specified origin |
linear_decay | Decay score using a linear function based on distance from a specified origin |
exp_decay | Decay score using an exponential function based on distance from a specified origin |
Timeliness
Use timeliness functions to score documents by how recently they were created or updated. Choose the unit based on the time precision your application requires.
| Function | What it returns |
|---|---|
timeliness | Timeliness score indicating document freshness, in seconds |
timeliness_ms | Timeliness score indicating document freshness, in milliseconds |
Functionality
Use these functions for specific ranking tasks: tag-based scoring, score passthrough from rough sort, query parameter extraction, score normalization, and set membership checks.
| Function | What it returns |
|---|---|
tag_match | Score based on matching query clauses against document tags, weighted by matched tag weights |
first_phase_score | Score calculated by the rough sort expression |
kvpairs_value | Value of a specified field from a kvpairs clause in the query |
normalize | Score normalized from its original value range to [0,1] |
in or notin | Whether a field value is in (or not in) a specified list |
What's next
Sort search results by relevance — get started with relevance-based ranking for forum, news, and similar applications.