This topic describes Cava, which is a programming language that is developed by the OpenSearch engine team based on the low-level virtual machine (LLVM) project. Cava uses the syntax similar to that of Java and can achieve the performance as efficient as C++. Cava is an object-oriented programming language. It supports just-in-time (JIT) compilation and various security checks to ensure more robust programs. You can use Cava and the Cava libraries that are provided by OpenSearch to design a dedicated sort plug-in in OpenSearch. A Cava-based sort plug-in has the following benefits compared with the expressions that are supported by OpenSearch:
More customization features: Cava supports more syntax features than expressions, such as for loops, function definition, and class definition. You can use these features to customize a sort plug-in based on your business requirements.
Easier to maintain: Cava code is easier to understand than expressions. Therefore, a sort plug-in that is developed by using Cava is easier to maintain.
Easier to learn: Cava uses a syntax similar to that of Java, which reduces learning costs. You can use Cava to develop a sort plug-in with ease if you are familiar with Java.
OpenSearch supports Cava-based plug-ins only for exclusive applications.
Cava-based plug-ins take effect only for fine sorts.
For more information about the syntax of Cava, see the topics about Cava-based sort plug-ins in Developer Guide.
Procedure
Create a policy: On the Policy Management page, click Create. On the Create Policy page, select Fine Sort from the Scope drop-down list and select Cava Script from the Type drop-down list.

Add a script file: You can choose to add a script file and edit the Cava script in the console. Alternatively, you can upload a local script file in the JSON format.

Compile and publish a script: After script files are edited, click Compile to compile all the script files. You can view the compilation status below the Compile button. After the compilation is complete, click Publish to publish the script files. Published scripted files cannot be modified.

Test the sorting performance: On the Search Test page of the created policy, set the
second_rank_nameparameter to the name of the policy and thesecond_rank_typeparameter to cava_script.

The following code block shows the parameter settings (Java is used in this example):
...
// Create a SearchParams object.
SearchParams searchParams = new SearchParams(config);
...
// Create a Rank object.
Rank rank=new Rank();
// Configure the called Cava script.
rank.setSecondRankName("Cava script name");
// Set the sort type to Cava scripts.
rank.setSecondRankType(RankType.CAVA_SCRIPT);
// Add the sort policy to the parameter object.
searchParams.setRank(rank);Obtain common parameters
Obtain the score by using a vector index:
import com.aliyun.opensearch.cava.features.similarity.ProximaScore;
ProximaScore _proximaScoreVector;
_proximaScoreVector=ProximaScore.create (params, "{{indexes.vector_index}}"); // Name of the required vector index
float proximaScoreVector = _proximaScoreVector.evaluate(params);Usage notes
OpenSearch supports Cava-based plug-ins only for exclusive applications.
A single Cava script file cannot exceed 10 KB. A maximum of five script files can be created for a single Cava sort policy, and a maximum of 50 Cava sort policies can be created for a single application instance.
Published Cava scripts cannot be modified. If you want to modify a Cava-based sort policy, you can replicate the sort policy on the Policy Management page and then modify the Cava script.
Cava scripts configured in the SDK must be published. Unpublished Cava scripts can be used only in search tests.