Cava is a programming language developed by the OpenSearch engine team on top of the low-level virtual machine (LLVM) project. It uses Java-like syntax and delivers C++-level performance, with object-oriented design, just-in-time (JIT) compilation, and built-in security checks.
Use Cava to write custom sort plug-ins for fine sorts in OpenSearch. Compared with OpenSearch expressions, Cava sort plug-ins offer:
| Dimension | Cava sort plug-in | Expression |
|---|---|---|
| Syntax features | for loops, function definitions, class definitions | Limited operators and built-in functions |
| Customization | Full business logic in code | Constrained to expression grammar |
| Maintainability | Readable, structured code | Compact but harder to read at scale |
| Learning curve | Familiar to Java developers | Requires learning expression syntax |
Cava-based sort plug-ins work only with exclusive applications and apply only to fine sorts. For Cava syntax details, see the Cava-based sort plug-in topics in the Developer Guide.
How it works
A Cava sort policy contains one or more Cava script files that implement your ranking logic. The workflow is:
Create a sort policy with type Cava Script and scope Fine Sort.
Add and edit script files in the console, or upload them in JSON format.
Compile all script files and publish the policy.
Reference the policy name and type in your SDK or search test.
Once published, the Cava script runs during fine sort to score and rank documents according to your logic. Cava supports syntax features such as for loops, function definitions, and class definitions, which give you full control over ranking logic beyond what OpenSearch expressions allow.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch exclusive application
Access to the Policy Management page in the OpenSearch console
Create and deploy a Cava sort plug-in
Step 1: Create a sort policy
Go to Policy Management and click Create.
On the Create Policy page, set Scope to Fine Sort and Type to Cava Script.

Step 2: Add script files
Add script files using either method:
Edit the Cava script directly in the console editor.
Upload a local script file in JSON format.

Step 3: Compile and publish
Click Compile to compile all script files. The compilation status appears below the Compile button.
After compilation succeeds, click Publish.

Published scripts cannot be modified. To update a published policy, replicate it on the Policy Management page and edit the copy.
Step 4: Test the sort policy
On the Search Test page of the policy, set the following parameters:
| Parameter | Value |
|---|---|
second_rank_name | Name of the sort policy |
second_rank_type | cava_script |

The following Java example shows how to set these parameters in the SDK:
// Create a SearchParams object
SearchParams searchParams = new SearchParams(config);
// Configure the Cava sort policy
Rank rank = new Rank();
rank.setSecondRankName("Cava script name"); // Exact name of the published sort policy
rank.setSecondRankType(RankType.CAVA_SCRIPT); // Invokes Cava script during fine sort
// Attach the rank config to the search request
searchParams.setRank(rank);Scripts referenced in the SDK must be published. Unpublished scripts can be used only in search tests.
Get common parameters
Vector index score
Use ProximaScore to retrieve the similarity score from a vector index. Call ProximaScore.create() to bind to a named index, then call evaluate() to get the score for each document.
import com.aliyun.opensearch.cava.features.similarity.ProximaScore;
// Bind to the target vector index
ProximaScore _proximaScoreVector;
_proximaScoreVector = ProximaScore.create(params, "{{indexes.vector_index}}"); // Replace with your vector index name
// Get the similarity score for the current document
float proximaScoreVector = _proximaScoreVector.evaluate(params);Limits
| Limit | Value |
|---|---|
| Max script file size | 10 KB per file |
| Max script files per sort policy | 5 |
| Max Cava sort policies per application instance | 50 |
| Script modification after publishing | Not supported — replicate the policy to make changes |
| SDK scripts | Must be published before use |
| Unpublished scripts | Available in search tests only |
What's next
Learn the Cava language syntax: see the Cava-based sort plug-in topics in the Developer Guide.