All Products
Search
Document Center

OpenSearch:rank clause

Last Updated:Feb 28, 2024

Overview

You can customize the scoring logic in a rank clause to control the order of the returned documents.

Syntax

{
  "rank" : {
    "ranking" : {
      "lang": "expression",
      "source" : "score_expression"
    },
    "reranking" : {
      "lang": "expression",
      "source" : "score_expression"
    }
  }
}

By default, the following sort phases are involved: rough sort and fine sort. You can customize score expressions to control the scoring logic of each phase. The ranking clause specifies the rough sort score that is calculated by using the rough sort expression. The reranking clause specifies the fine sort score that is calculated by using the fine sort expression. The returned documents are sorted based on the following rules:

  • If you specify only the ranking clause, the fine sort score equals the rough sort score. The final score of a document is the fine sort score.

  • If you specify only the reranking clause, the rough sort score is calculated by using the static_bm25() function. The fine sort score is calculated by using the fine sort expression. The final score of a document is the fine sort score.

  • If you specify both the ranking and reranking clauses, the rough sort score is calculated by using the rough sort expression, and the fine sort score is calculated by using the fine sort expression. The final score of a document is the fine sort score.

  • If you do not specify either the ranking clause or the reranking clause, the rough sort score is calculated by using the static_bm25() function. The fine sort score equals the rough sort score. The final score of a document is the fine sort score.

Parameters

  • lang: required. A type of the sort script. Set the value to expression.

  • source: required. The content of the expression. The expression is an arithmetic expression. For more information about the rules and features of the arithmetic expression, see Sort expressions.

Usage notes

  • If the number of the returned documents is greater than the number of documents that are involved in the fine sort phase, the number of documents that are returned by the Searcher worker to the Query Result Searcher (QRS) worker is greater than the number of documents that are involved in the fine sort phase. In this case, the final scores of several documents are rough sort scores. By default, a rough sort score is less than 10,000. To resolve the preceding issue, the engine adds 10,000 to the final rough sort score of a document to obtain the final fine sort score of the document. You can change 10,000 to another value in the cluster configurations.