All Products
Search
Document Center

OpenSearch:Develop sort plugins using Cava

Last Updated:Apr 01, 2026

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:

DimensionCava sort plug-inExpression
Syntax featuresfor loops, function definitions, class definitionsLimited operators and built-in functions
CustomizationFull business logic in codeConstrained to expression grammar
MaintainabilityReadable, structured codeCompact but harder to read at scale
Learning curveFamiliar to Java developersRequires learning expression syntax
Important

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:

  1. Create a sort policy with type Cava Script and scope Fine Sort.

  2. Add and edit script files in the console, or upload them in JSON format.

  3. Compile all script files and publish the policy.

  4. 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

  1. Go to Policy Management and click Create.

  2. On the Create Policy page, set Scope to Fine Sort and Type to Cava Script.

image

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.

image

Step 3: Compile and publish

  1. Click Compile to compile all script files. The compilation status appears below the Compile button.

  2. After compilation succeeds, click Publish.

image
Important

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:

ParameterValue
second_rank_nameName of the sort policy
second_rank_typecava_script
image

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);
Note

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

LimitValue
Max script file size10 KB per file
Max script files per sort policy5
Max Cava sort policies per application instance50
Script modification after publishingNot supported — replicate the policy to make changes
SDK scriptsMust be published before use
Unpublished scriptsAvailable in search tests only

What's next

  • Learn the Cava language syntax: see the Cava-based sort plug-in topics in the Developer Guide.