All Products
Search
Document Center

OpenSearch:Write sort scripts by using Cava

Last Updated:Oct 19, 2026

Overview of Cava sort scripts

OpenSearch supports two-phase sorting: rough sort and fine sort. Currently, sort scripts implemented in Cava take effect only in the fine sort phase. This topic focuses only on how to write sort scripts by using Cava. For information about how to create and use scripts, see the API reference for sort scripts. You can also use the command-line tool that we provide to create sort scripts. Compared with sort expressions, sort scripts written in Cava provide greater flexibility and customizability. In a script, you can implement your own business logic by using the syntax supported by Cava and the feature library provided by OpenSearch.

Cava sort script interface

To use a custom script for scoring and sorting, you must implement the scoring interface class provided by OpenSearch. The following code shows the interface class:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;

class BasicSimilarityScorer {
    //You can define member variables here
    boolean init(OpsScorerInitParams params) {
        //Implement your code here to initialize request-level variables, such as member variables of the class
        return true;
    }

    double score(OpsScoreParams params) {
        double score = 0;
        //Implement your code here and assign the scoring result to score
        return score;
    }
};

The interface class BasicSimilarityScorer is in the users.scorer package. You cannot change the class name or the package name. Otherwise, a compilation error occurs. BasicSimilarityScorer provides two methods, init and score, in which you can implement your own business logic.

In OpenSearch, for each request, OpenSearch first calls the init method of BasicSimilarityScorer to initialize request-level variables, such as member variables of the class. This method is executed only once for each request. If the method returns a failure, the request is terminated. Then, for each document that is hit and participates in fine sort, OpenSearch calls the score method in sequence to score the document. Finally, the documents are sorted based on the scoring results.

The input parameter of the init interface is OpsScorerInitParams. You can use this parameter to obtain request-level resources. We recommend that you initialize the member variables of BasicSimilarityScorer in the init phase.

The input parameter of the score interface is OpsScoreParams. You can use this parameter to obtain request-level and document-level resources. The score interface is called once for each document that participates in scoring. Therefore, avoid obtaining request-level resources, such as parameters in kvpairs or the creation of feature objects, in the score interface. You can obtain them in the init interface instead. Document-level resources can be obtained in the score interface.

You cannot modify the function definitions of init and score, for example, by changing the return value type or the input parameters. Otherwise, a compilation error occurs.

Cava sort script example

package users.scorer;
import cava.lang.CString;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsKvPairs;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.similarity.TextRelevance; //Import the features that you want to use
class BasicSimilarityScorer {
    TextRelevance _textRelevance; //Define the scoring feature as a member variable

    boolean init(OpsScorerInitParams params) {
        if (!params.getDoc().requireAttribute("shop_margin")) { //Attribute fields used in scoring must be declared in the init interface
            return false;
        }
        _textRelevance = TextRelevance.create(params, "default", "name"); //Scoring features are declared in the init interface
        return true;
    }

    double score(OpsScoreParams params) {
        float shopMargin = params.getDoc().docFieldFloat("shop_margin"); //Obtain the value of a field in the document
        float textScore = _textRelevance.evaluate(params); //Calculate the feature score
        double score = textScore * 30.0 + shopMargin;
        return score;
    }
}

Precautions for developing sort scripts

  • We recommend that you define the feature classes provided by OpenSearch as member variables of BasicSimilarityScorer, initialize them in the init function, and perform scoring in the score function. Initializing them in the score phase causes a significant waste of performance.

  • We recommend that you define custom parameters in a request as member variables of BasicSimilarityScorer and obtain their values in the init function.

  • To obtain a field in a document, you must first define the field as an attribute field in the application schema, declare it in the init function, and then obtain its specific value in the score function.

  • Currently, you can upload only a single file. Therefore, you can define classes only in the users.scorer package.

  • You can use the import keyword to reference the system libraries provided by OpenSearch. However, the import com.aliyun.opensearch.cava.framework.*; syntax is currently not supported.

  • For a single query request, the upper limit of memory used by a sort script at runtime is 40 MB. If memory usage exceeds the limit, the query request reports an error and returns. Therefore, do not perform large memory operations in the script. In particular, do not frequently perform new operations or use a large number of strings in the score function. For a request that exceeds the memory limit, OpenSearch reports an error and also returns results (it tries its best to ensure that results are returned). However, only some of the documents are scored by the sort script, and the others are not.

  • You can use for loops or make function calls in sort scripts. To prevent infinite loops and unlimited function calls, OpenSearch limits the number of for loop iterations and function calls in a sort script to no more than 100,000 for a single query request. If the limit is exceeded, the query request reports an error and returns early.

  • The number of documents sorted by a sort script is limited by the rerank_size parameter. You can use this parameter to adjust the number of documents that participate in sorting to avoid triggering the memory and loop count limits.