--- title: "Ranking formula" slug: "cloudsearch-ranking" tags: ["Cloud Search"] updated: 2026-09-17T09:18:13Z published: 2026-09-17T09:18:13Z canonical: "guide.ncloud-docs.com/cloudsearch-ranking" stale: true --- > ## Documentation Index > Fetch the complete documentation index at: https://guide.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # Ranking formula Available in Classic and VPC You can utilize ranking formulas to sort your search results the way you want. This describes how to create ranking formulas and apply them when searching. Note If you do not set a sorting method, it will sort the search results in order of highest document weight (qds). ## Ranking formula A ranking formula is an expression to calculate a ranking score that is the criterion to sort search results. Ranking scores are usually expressed as relevance (_relevance) and calculated as the sum of similarity (_similarity) and quality (_quality). Ranking formula of the basic ranking (clous) provided upon creation of domain ``` _ratio=0.5; _quality=1.0; _similarity=qds; _relevance = _similarity * _ratio + _quality * (1 - _ratio); ``` You can write your own ranking formula to sort your search results in an order appropriate for each search service. ## Writing a ranking formula The way to write a ranking formula is as follows. - Use only assignment statements in the form of a single function. ``` relevance = qds; relevance() = qds; // exactly the same as above. relevance( x ) = x*qds; ``` - Enter all lines as a string separated by semicolons (;). - Write using various variables, attribute values, expressions, and built-in functions. Example of writing a ranking formula ``` relevance = min(qry_mtc("q1"), qry_mtc("q2")); normalized_qds = qds/qtermc; normalized_qds_of_q1 = qry_qds("q1") / qry_qtermc("q1"); mtratio = mtc/qtermc; ``` ### Ranking variables and attribute values When creating ranking formulas, you can use various attribute values along with ranking variables. Note - All variables internally have a type (bool, int, double, string). - Variables starting with an underscore (_) are reserved for internal use. #### Ranking variables Available ranking variables when creating a ranking formula are as follows. - A function with document attribute values (`dp_섹션이름`): a ranking variable set when creating a section Ranking formula with document attribute values ``` _ratio=0.5; _quality= dp_like_cnt * 0.09 + dp_dislike_cnt * 0.01; _similarity=qds; _relevance = _similarity * _ratio + _quality * (1 - _ratio); ``` - Define the desired variables #### Query property Available property values of queries when creating a ranking formula are as follows. - `qtermc`: the number of search words of a query, the number of added search words (Query-Term-Count) to qds - `qry_qtermc(query_name)`: the number of search words of a specific query (query_name), a compound query is used to obtain the number of search words of individual queries - `qry_exist(query_name)`: return whether a specific query (query_name) exists - `epoch_time`: the time at the start of executing a query #### Document score Available property values of documents when creating a ranking formula are as follows. - `qds`: document weight, similarity of query document (Query-Document-Score) - `qry_qds(query_name)`: document weight for a specific query (query_name) - `mtc`: the number of query terms included in the search target documents (Matched-Term-Count) - `qry_mtc(query_name)`: the number of query terms included in the search target document for a specific query (query_name) (mtc) #### Other Other available property values when creating a ranking formula are as follows. - `total_document_count`: total number of search result documents (estimated) - The total number of documents is subject to change when requests for uploading document are in progress. ### Expressions and built-in functions You can use a variety of expressions and built-in functions when writing ranking formulas. #### Expressions Available expressions when creating a ranking formula are as follows. - Arithmetic operators: `+`, `-`, `*`, `/` - Comparison operators: `>`, `<`, `>=`, `<=`, `==`, `!=` - Logical operators: `|`, `&`, `!` - Constant expressions: integer, real, `true`, `false` - Condition expressions: `if - then - else` Ranking formula written in an expression ``` mtr = mtc/qtermc; quality = 1.0; relevance = if mtr >= 0.6 then qds + quality else quality; ``` #### Built-in functions Available built-in functions when creating a ranking formula are as follows. - `min(a, b)`: minimum value - `max(a, b)`: maximum value - `exp(x)`: index - `log(x)`: natural logarithm - `log10(x)`: log - `sqrt(x)`: square root - `pow(x, y)`: y-square of x ## Using ranking formula You can use the created ranking formula by writing a search query during search as follows. - If you save the ranking formula in the ranking module - Specifies the ranking module to use in the `ranking` field - Specifies the ranking variable calculated in the ranking formula as a parameter of the `sort` field - If you set the ranking module to the default, you are not required to set up the `ranking` field. - If you enter a ranking formula directly into your search query - Enter the ranking formula directly in the `ranking` field - Specifies the ranking variable calculated in the ranking formula as a parameter of the `sort` field Note - For how to create a ranking module and set the default ranking, see [Ranking formula](/docs/en/cloudsearch-searchsettings#%EB%9E%AD%ED%82%B9%EC%88%98%EC%8B%9D). - For how to write the `ranking` and `sort` field, see [ranking](/docs/en/cloudsearch-searchquery#ranking1) and [sort.](/docs/en/cloudsearch-searchquery#sort1) Search query that specifies the ranking variable (relevance) of the basic ranking as a `sort` field parameter ``` { "search": { "index_name": { "main": [ { "name": "q1", "query": "term" }, { "name": "q2", "query": "test" } ] } }, "sort" : { "relevance": "desc" } } ```