Vector index best practices

Updated at:

Configure a vector index in an existing OpenSearch Retrieval Engine Edition instance and use an SDK to retrieve data with a vector query.

Purchase an OpenSearch Retrieval Engine Edition instance

To purchase an instance, see Purchase an OpenSearch Retrieval Engine Edition instance.

Configure the cluster

A new instance has an Instance Status of Pending Configuration. An empty cluster with the same QRS worker and data node count and specifications that you purchased is automatically deployed. Configure the cluster in the following order: Data Source ---> Configure Index ---> Rebuild Index. After configuration, you can run searches.

image

  1. Configure the data source. Supported data sources include MaxCompute data sources and API push data sources. This example uses a MaxCompute data source. Click Add Data Source. Set Data Source Type to MaxCompute. Configure the project, AccessKey ID, AccessKey secret, Table, and partition key parameters. If needed, you can also enable automatic index rebuilding.

image

After the parameters are validated, click OK to add the data source.

image

  1. After you configure the data source, click Next to configure the index schema.

image

2.1. Add an index table.

image

2.2. Configure the index table.

image

  • Index Table: Enter a custom name.

  • Data Source: Select the data source that you configured in Step 1.

  • Data Shards: Set this based on the number of data nodes that you purchased.

2.3. Configure the fields. You must define at least a primary key field and a vector field. The vector field must be a multi-value field of the FLOAT type.

image

If you want to use vectors with categories, add a category field between the primary key field and the vector field. The category field must be a single-value or multi-value field of the INTEGER type.

image

Property and field content compression

  • Property field compression is disabled by default. To enable it, select file_compressor.

  • Field content compression is disabled by default. For multi-value and STRING types, uniq is selected by default. For single-value numeric types, equal is selected by default.

Note
  • For vector search, you must create fields in the following order: primary key field, tag field (optional), and vector field. The preceding figure shows an example.

  • If you enable property compression, go to Deployment Management > Data Nodes > Online Table Configuration to edit the index loading method to reduce the impact on performance.

  • You can copy fields. The new field, also known as a DUP field, has the same content as the original field. If you push different content to the DUP field, the content of the original field overwrites it. If you want the DUP field to have different content, you must manually delete the copy configuration in the advanced configuration settings of the DUP field.

3. Configure the index. Set the index type for the primary key field to PRIMARYKEY64 and the index type for the vector field to CUSTOMIZED.

image

Index field compression settings

  • Index field compression is disabled by default. To enable it, select file_compressor.

Note
  • Primary key indexes do not support compression.

  • If you enable index compression, go to Deployment Management > Data Nodes > Online Table Configuration to edit the index loading method to reduce the impact on performance.

3.1. Add included fields for the vector field.

image

Note
  • The primary key field and vector field are required. The tag field is optional and can be left empty.

  • You can select only from these three fields. You cannot add other fields.

3.2. In the Advanced Configuration section, configure the vector index parameters. Use the following configuration as a reference. For more information, see Vector index.

image

Additional parameters:

image.png

Sample build_index_params configuration:

{
  "proxima.qc.builder.quantizer_class": "Int8QuantizerConverter",
  "proxima.qc.builder.quantize_by_centroid": true,
  "proxima.qc.builder.optimizer_class": "BruteForceBuilder",
  "proxima.qc.builder.thread_count": 10,
  "proxima.qc.builder.optimizer_params": {
    "proxima.linear.builder.column_major_order": true
  },
  "proxima.qc.builder.store_original_features": false,
  "proxima.qc.builder.train_sample_count": 3000000,
  "proxima.qc.builder.train_sample_ratio": 0.5
}

Sample search_index_params configuration:

{
  "proxima.qc.searcher.scan_ratio": 0.01
}

  1. After you complete the configuration, click Save Version. In the dialog box that appears, enter optional remarks and click Publish.

image

After the index is published, click Next to rebuild the index.

image

  1. Rebuild the index. Select the parameters for index rebuilding and click Next.

  • API push data source

image

  • MaxCompute data source

image

  1. Go to Operation Center > Change History > Data Source Changes to view the progress of the index rebuilding task. After the task is complete, you can test a query.

image

  1. Testing page queries:

image

Syntax description

For more information, see Vector search syntax.

Use vector search in an SDK

Add the dependency:

pip install alibabacloud-ha3engine

Search demo:

from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError

def search():
    Config = models.Config(
        endpoint="",
        instance_id="",
        protocol="http",
        access_user_name="",
        access_pass_word=""
    )

    # If a request takes a long time, you can use this configuration to increase the request timeout period. Unit: ms.
    # This parameter can be used in the search_with_options method.
    runtime = util_models.RuntimeOptions(
        connect_timeout=5000,
        read_timeout=10000,
        autoretry=False,
        ignore_ssl=False,
        max_idle_conns=50
    )
    # Initialize the Ha3Engine client.
    ha3EngineClient = client.Client(Config)
    optionsHeaders = {}
    try:
        # Example 3: Use an ha-sql query string to perform a search.
        # =====================================================
        sql_str = '''query=select proxima_score('index_name') as score,id from table_name where MATCHINDEX('index_name', ?) order by score asc limit 5&&kvpair=timeout:1000,iquan.plan.cache.enable:true;urlencode_data:false;iquan.plan.prepare.level:jni.post.optimize;dynamic_params:[["0.892704,0.783731&n=10"]]'''
        sqlsearchQuery = models.SearchQuery(sql=sql_str)
        sqlSearchRequestModel = models.SearchRequestModel(optionsHeaders, sqlsearchQuery)
        sqlstrSearchResponseModel = ha3EngineClient.search(sqlSearchRequestModel)
        print(sqlstrSearchResponseModel)

    except TeaException as e:
        print(f"send request with TeaException : {e}")
    except RetryError as e:
        print(f"send request with Connection Exception  : {e}")


if(__name__  == "__main__"):
    search()

Notes