Creating a Vector Index

Prerequisites

Creating a Vector Index

  1. Log in to the CSS management console.
  2. Choose Clusters in the navigation pane. On the Clusters page, locate the target cluster and click Access Kibana in the Operation column.
  3. Click Dev Tools in the navigation tree on the left and run the following command to create a vector index.

    Create an index named my_index that contains a vector field my_vector and a text field my_label. The vector field creates the graph index and uses Euclidean distance to measure similarity.

    PUT my_index 
    {
      "settings": {
        "index": {
          "vector": true
        }
      },
      "mappings": {
        "properties": {
          "my_vector": {
            "type": "vector",
            "dimension": 2,
            "indexing": true,
            "algorithm": "GRAPH",
            "metric": "euclidean"
          },
          "my_label": {
            "type": "text"
          }
        }
      }
    }
    Table 1 Parameters for creating an index

    Type

    Parameter

    Description

    Index settings parameters

    vector

    To use a vector index, set this parameter to true.

    Field mappings parameters

    type

    Field type, for example, vector.

    dimension

    Vector dimension.

    The default value is 768 and cannot be changed.

    Value range: [1, 4096]

    indexing

    Whether to enable vector index acceleration.

    The value can be:
    • false: disables vector index acceleration. If this parameter is set to false, vector data is written only to docvalues, and only ScriptScore and Rescore can be used for vector query.
    • true: enables vector index acceleration. If this parameter is set to true, an extra vector index is created. The index algorithm is specified by the algorithm field and VectorQuery can be used for data query.

    Default value: false

    algorithm

    Index algorithm. This parameter is valid only when indexing is set to true.

    The value can be:
    • FLAT: brute-force algorithm that calculates the distance between the target vector and all vectors in sequence. The algorithm relies on sheer computing power and its recall rate reaches 100%. You can use this algorithm if you require high recall accuracy.
    • GRAPH: Hierarchical Navigable Small Worlds (HNSW) algorithm for graph indexes. This algorithm is mainly used in scenarios where high performance and precision are required and the data records of a single shard is fewer than 10 million.
    • GRAPH_PQ: combination of the HNSW algorithm and the PQ algorithm. The PQ algorithm reduces the storage overhead of original vectors, so that HNSW can easily search for data among hundreds of millions of records.
    • IVF_GRAPH: combination of IVF and HNSW. The entire space is divided into multiple cluster centroids, which makes search much faster but slightly inaccurate. You can use this algorithm if you require high performance when searching for data among hundreds of millions of records.
    • IVF_GRAPH_PQ: combination of the PQ algorithm with the IVF or HNSW algorithm to further improve the system capacity and reduce the system overhead. This algorithm is applicable to scenarios where there are more than 1 billion files in shards and high retrieval performance is required.
    Default value: GRAPH
    NOTE:

    If IVF_GRAPH or IVF_GRAPH_PQ is specified, you need to pre-build and register a central point index. For details, see (Optional) Pre-Building and Registering a Center Point Vector.

    Table 2

    If Indexing is set to true, CSS provides optional parameters for vector search to achieve higher query performance or precision.

    metric

    Method of calculating the distance between vectors.

    The value can be:

    • euclidean: Euclidean distance
    • inner_product: inner product distance
    • cosine: cosine distance
    • hamming: Hamming distance, which can be used only when dim_type is set to binary.

    Default value: euclidean

    dim_type

    Type of the vector dimension value.

    The value can be binary and float (default).

    Table 2 Optional parameters

    Type

    Parameter

    Description

    Graph index configuration parameters

    neighbors

    Number of neighbors of each vector in a graph index. The default value is 64. A larger value indicates higher query precision. A larger index results in a slower build and query speed.

    Value range: [10, 255]

    shrink

    Cropping coefficient during HNSW build. The default value is 1.0f.

    Value range: (0.1, 10)

    scaling

    Scaling ratio of the upper-layer graph nodes during HNSW build. The default value is 50.

    Value range: (0, 128]

    efc

    Queue size of the neighboring node during HNSW build. The default value is 200. A larger value indicates a higher precision and slower build speed.

    Value range: (0, 100000]

    max_scan_num

    Maximum number of nodes that can be scanned. The default value is 10000. A larger value indicates a higher precision and slower indexing speed.

    Value range: (0, 1000000]

    PQ index configuration parameters

    centroid_num

    Number of cluster centroids of each fragment. The default value is 255.

    Value range: (0, 65535]

    fragment_num

    Number of fragments. The default value is 0. The plug-in automatically sets the number of fragments based on the vector length.

    Value range: [0, 4096]

Importing Vector Data

Run the following command to import vector data. When writing vector data to the my_index index, you need to specify the vector field name and vector data.

Advanced Cluster Configurations