Help Center/ Cloud Search Service/ Best Practices/ Elasticsearch Vector Search/ Using the GRAPH Algorithm to Implement Vector Search
Updated on 2025-09-05 GMT+08:00

Using the GRAPH Algorithm to Implement Vector Search

The GRAPH algorithm (a deeply optimized implementation of HNSW) enables vector search featuring high recall and low latency based on a memory-optimized cluster. It also supports hybrid queries that combine both vector similarity search and scalar field filters.

Scenario

Vector databases that use the GRAPH indexing algorithm support vector search applications that demand high query performance and recall, such as image search, recommendation systems, and semantic search.

Solution Procedure

  1. Creating vector indexes: The GRAPH algorithm is used to create vector indexes. Optimizations like edge cutting, connectivity enhancement, and SIMD acceleration are supported.
  2. Associating with scalar fields: Hybrid queries that combine both vector similarity search and scalar field filters (such as labels and classes) are supported.
  3. Query engine: Enables efficient vector similarity search.

Highlights

  • Enhanced performance: Compared with open-source algorithms, optimizations like edge cutting, connectivity enhancement, and SIMD acceleration enhance query performance and recall.
  • Flexible filtering: Hybrid queries that combine both vector and scalar fields enhance search accuracy.
  • Higher accuracy: The HNSW-optimized implementation enables more accurate vector similarity matching.

Constraints

Indexes created using the GRAPH algorithm require resident memory. The optimal query performance can be achieved only when there is sufficient memory. For details about how to estimate the required memory capacity, see Memory Planning.

Prerequisites

A CSS Elasticsearch vector database has been created. The cluster version is 7.10.2, and the cluster node flavor is memory-optimized.

Procedure

  1. Log in to Kibana and go to the command execution page. Elasticsearch clusters support multiple access methods. This topic uses Kibana integrated by CSS as an example to describe the operation procedures.

    1. Log in to the CSS management consoleCSS management console.
    2. In the navigation pane on the left, choose Clusters > Elasticsearch.
    3. In the cluster list, find the target cluster, and click Kibana in the Operation column to log in to the Kibana console.
    4. In the left navigation pane, choose Dev Tools.

  2. Create a GRAPH vector index.

    Create an index named my_index that contains a vector field my_vector and a label field my_label.

    PUT my_index 
    {
      "settings": {
        "index": {
          "vector": true
        }
      },
      "mappings": {
        "properties": {
          "my_vector": {
            "type": "vector",
            "dimension": 2,
            "indexing": true,
            "algorithm": "GRAPH",
            "metric": "euclidean"
          },
          "my_label": {
            "type": "keyword"
          }
        }
      }
    }

  3. Ingest vector data.

    Run the following command to write the full vector data to the new GRAPH index.

    • Write a single record:
      POST my_index/_doc
      {
        "my_vector": [1.0, 2.0],
        "my_label": "red"
      }
    • Write multiple records at the same time:
      POST my_index/_bulk
      {"index": {}}
      {"my_vector": [1.0, 2.0], "my_label": "red"}
      {"index": {}}
      {"my_vector": [2.0, 2.0], "my_label": "green"}
      {"index": {}}
      {"my_vector": [2.0, 3.0], "my_label": "red"}

  4. Query vector data.

    Run the following command to perform a vector search:

    • Pure vector similarity search:
      POST my_index/_search
      {
        "size":3,
        "_source": {"excludes": ["my_vector"]}, 
        "query": {
          "vector": {
            "my_vector": {
              "vector": [1, 1],
              "topk":3
            }
          }
        }
      }
    • Vector+scalar hybrid search (pre-filtering query):
      POST my_index/_search
      {
        "size":3,
        "_source": {"excludes": ["my_vector"]}, 
        "query": {
          "vector": {
            "my_vector": {
              "vector": [1, 1],
              "topk":3,
              "filter": {
                "term": {
                  "my_label": "red"
                }
              }
            }
          }
        }
      }

    If the result is returned, the query is successful.

Related Documents