Cava 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. Compared with the expressions that are supported by OpenSearch, a Cava-based sort plug-in has the following benefits:
More diversified custom designs: Cava allows you to customize a sort plug-in by using more diversified syntax. For example, you can use for loops and define functions and classes 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 the syntax similar to that of Java. If you are familiar with Java, you can use Cava to develop a sort plug-in with ease. This reduces learning costs.
Cava-based plug-ins can be used only in 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: Log on to the OpenSearch console. In the left-side navigation pane, choose Search Algorithm Center > Sort Configuration. On the Policy Management page, click Create. In the Basic Information step of the Create Policy wizard, select Fine Sort from the Scope drop-down list and select Cava Script from the Type drop-down list.
Add a script file: In the Sort Configuration step, you can 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 effect: On the Search Test page of the created policy, set the
second_rank_name
parameter to the name of the policy and thesecond_rank_type
parameter to cava_script.
The following sample code provides an example on how to use a Cava-based sort policy if you use the SDK for Java:
...
// Create a SearchParams object.
SearchParams searchParams = new SearchParams(config);
...
// Create a Rank object.
Rank rank=new Rank();
// Specify the Cava script that you want to call.
rank.setSecondRankName("Cava script name");
// Set the sort type to Cava scripts.
rank.setSecondRankType(RankType.CAVA_SCRIPT);
// Add the sort policy to the SearchParams 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}}"); // The name of the vector index.
float proximaScoreVector = _proximaScoreVector.evaluate(params);
Usage notes
Cava-based plug-ins can be used only in exclusive applications.
A single Cava script file cannot exceed 10 KB in size. A maximum of five script files can be added to a single Cava-based sort policy, and a maximum of 50 Cava-based sort policies can be created for a single application.
Published Cava scripts cannot be modified. If you want to modify a published Cava script, you can replicate the policy to which the Cava script is added on the Policy Management page and create another policy.
Cava scripts configured in an SDK must be published. Unpublished Cava scripts can be used only in search tests.