All Products
Search
Document Center

OpenSearch:Create sort scripts by using SortScript

Last Updated:Feb 03, 2023

Lifecycle of a sort script

The following content describes the lifecycle of a sort script in OpenSearch:

  1. Create a sort script. In this process, you must specify the name and version ID of the application and the name of the sort script. For easy identification, we recommend that you distinguish the name of the sort script from that of an expression created in the OpenSearch console.

  2. Upload script code. A sort script is written by using Cava, which is a programming language provided by OpenSearch. You can repeatedly upload script code for an unpublished sort script.

  3. Compile the sort script. After you upload script code, you can run compilation commands to verify the syntax. You can repeatedly compile an unpublished sort script.

  4. Publish the sort script. If you want to apply a sort script to an online application after the sort script is compiled, you can publish the sort script. After the sort script is published, you cannot upload script code or compile the sort script again. You can only delete the sort script.

  5. Delete the sort script. You can delete a sort script that is no longer used. Make sure that the sort script is not being used in queries before you delete the sort script. Otherwise, a query error is returned.

  6. View the sort script. You can view the status of a sort script at any time in the preceding processes.

SortScript

SortScript is a command-line tool based on Python in a version later than 2.7. This tool allows you to create, maintain, publish, and delete sort scripts with ease. You can download SortScript. Before you use SortScript, you must configure your AccessKey ID and AccessKey secret in the tools_conf.py file.

The following example describes how to create a sort script by using SortScript:

1. Create a sort script.

python ./sortscript create --appName script_test --appVersionId 12345678 --scriptName script1

2. Upload script code, including the BasicSimilarityScorer.cava and IntelligenceAlgorithm.cava files. For more information about the syntax of Cava, see Data types and variable types.

python ./sortscript updatecontent --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName BasicSimilarityScorer.cava --localFile ./BasicSimilarityScorer.cava
python ./sortscript updatecontent --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName IntelligenceAlgorithmScorer.cava --localFile ./IntelligenceAlgorithmScorer.cava

3. Compile the sort script. Specific syntax errors may be returned when you compile the sort script. Modify the sort script based on prompts.

python ./sortscript compile --appName script_test --appVersionId 12345678 --scriptName script1

4. Publish the sort script. After you confirm that no further modification is needed, publish the sort script.

python ./sortscript release --appName script_test --appVersionId 12345678 --scriptName script1

5. View the sort script. You can set the scriptName parameter to view a specific sort script. Otherwise, all the sort scripts in the current application version are listed.

python ./sortscript list --appName script_test --appVersionId 12345678 --scriptName script1
python ./sortscript list --appName script_test --appVersionId 12345678

6. View the details of code files.

python ./sortscript getcontent --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName BasicSimilarityScorer.cava
python ./sortscript getcontent --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName IntelligenceAlgorithmScorer.cava

7. Delete code files.

python ./sortscript deleteScriptFile --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName BasicSimilarityScorer.cava
python ./sortscript deleteScriptFile --appName script_test --appVersionId 12345678 --scriptName script1 --scriptFileName IntelligenceAlgorithmScorer.cava

8. Delete the sort script. Make sure that the sort script is not being used in queries before you delete the sort script.

python ./sortscript delete --appName script_test --appVersionId 12345678 --scriptName script1

Use a sort script in a query to sort results

  1. Make sure that the sort script has been published.

  2. Set the second_rank_name parameter in the query to the name of the sort script. For easy identification, we recommend that you distinguish the name of the sort script from that of an expression created in the OpenSearch console.

  3. Set the second_rank_type parameter in the query to cava_script.

  4. View query results. If the sort script does not exist, query results can be returned, but an error is also returned.