Create sort scripts by using SortScript

更新时间:
复制 MD 格式

SortScript is a Python-based CLI tool for managing sort scripts in OpenSearch. Use it to create, upload, compile, publish, and delete Cava-based sort scripts that control how search results are ranked.

Note

Name sort scripts differently from expressions created in the OpenSearch console. Both can be referenced in queries by name, so distinct names prevent confusion.

Prerequisites

Before you begin, make sure you have:

  • Python later than version 2.7

  • SortScript CLI — download the tool

  • Your AccessKey ID and AccessKey secret configured in tools_conf.py

Sort script lifecycle

A sort script moves through the following stages:

StageDescriptionRepeatable before publish?
CreateRegister a script name tied to an application versionNo
Upload codeUpload .cava source filesYes
CompileVerify syntaxYes
PublishPromote to production; locks the scriptNo
DeleteRemove a script that is no longer neededNo
ViewInspect status or code at any pointN/A
Important

After publishing, the script is locked. You cannot upload new code or recompile it. Deletion is the only available operation.

Manage sort scripts

All commands use the following common parameters:

ParameterDescriptionExample
--appNameApplication namescript_test
--appVersionIdApplication version ID12345678
--scriptNameSort script namescript1
--scriptFileNameName of a Cava source file within the scriptBasicSimilarityScorer.cava
--localFileLocal path to the file being uploaded./BasicSimilarityScorer.cava

Create a sort script

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

Upload script code

Upload each Cava source file individually. For Cava syntax, see the Cava guide.

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

You can re-upload files as many times as needed before publishing the script.

Compile the sort script

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

If syntax errors are returned, fix them and recompile. You can compile repeatedly before publishing.

Publish the sort script

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

Publishing locks the script. You cannot upload new code or recompile after this point.

View sort scripts

To view a specific sort script:

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

To list all sort scripts in an application version:

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

View code file details

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

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

Delete the sort script

Warning

Before deleting a sort script, make sure it is not referenced in any active queries. Deleting a script that is in use causes those queries to return errors.

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

Use a sort script in a query

To apply a sort script to search results:

  1. Confirm the sort script is published.

  2. In the query, set second_rank_name to the name of the sort script.

  3. Set second_rank_type to cava_script.

Example query parameters:

ParameterValueDescription
second_rank_namescript1Name of the published sort script
second_rank_typecava_scriptSpecifies that the ranking source is a Cava script
Note

If the sort script specified in second_rank_name does not exist, the query still returns results but also returns an error.