Vector data insertion and index building

Updated at:

After defining vector properties in the graph schema, you can write vector data to vertices. HNSW indexes take effect automatically when data is written. HNSWQ, IVFPQ, and IVFBQ indexes require manual triggering after sufficient training data has been written. This topic describes the write requirements, write methods, index build procedures for different index types, and how to query index build progress.

Prerequisites

  • Lindorm Graph Engine is activated and the access whitelist is configured.

  • Lindorm Vector Engine is activated.

  • The graph schema is initialized, and the target vertex type has VECTOR_FLOAT vector properties and vectorMeta defined.

  • The connection endpoint, database name, username, and password of Graph Engine are obtained.

Insert vector data

Write requirements

Vector data is written as vertex properties. Before writing, make sure the following requirements are met:

  • Vector properties are supported only on vertices. Edges do not support vector properties.

  • The data type of vector properties is VECTOR_FLOAT. FLOAT32 vectors are used.

  • Each element in the vector array must be a numeric value.

  • The vector array length must match the dimension defined by vectorMeta.dimension in the schema.

  • The distance type is determined by distanceMethod in the schema and cannot be specified separately when writing data.

  • When using COSINE distance, do not write zero vectors because the cosine similarity of a zero vector is undefined.

The following examples assume that the document vertex type and its properties are already defined in the schema:

Property

Data type

Description

embedding

VECTOR_FLOAT

A 4-dimensional vector property.

title

STRING

The document title.

category

STRING

The document category.

status

STRING

The document status.

Write vertices using Gremlin

Use property() to write vector properties. Vector values are represented as numeric arrays:

g.addV('document')
  .property(id, 'doc_same')
  .property('embedding', [1.0d, 0.0d, 0.0d, 0.0d])
  .property('title', 'Exact match')
  .property('category', 'guide')
  .property('status', 'online')

The corresponding HTTP request is as follows:

curl -sS -u "${SUB_USER}:${SUB_PASSWORD}" -X POST \
  "http://${SERVER_HOST}:${GREMLIN_PORT}/gremlin/${DB_NAME}" \
  -H 'Content-Type: application/json' \
  --data '{
    "gremlin": "g.addV(\"document\").property(id,\"doc_same\").property(\"embedding\",[1.0d,0.0d,0.0d,0.0d]).property(\"title\",\"Exact match\").property(\"category\",\"guide\").property(\"status\",\"online\")"
  }'

The Gremlin HTTP interface returns a GraphSON 3 standard response envelope. When the write succeeds, verify that both the HTTP status code and status.code in the response are 200.

Sample data

You can continue to write the following vertices in the same way:

Vertex ID

embedding

title

category

status

doc_same

[1,0,0,0]

Exact match

guide

online

doc_scaled

[2,0,0,0]

Same direction different length

guide

online

doc_orthogonal

[0,1,0,0]

Orthogonal vector

reference

offline

doc_opposite

[-1,0,0,0]

Opposite vector

archive

online

For example, write doc_scaled:

curl -sS -u "${SUB_USER}:${SUB_PASSWORD}" -X POST \
  "http://${SERVER_HOST}:${GREMLIN_PORT}/gremlin/${DB_NAME}" \
  -H 'Content-Type: application/json' \
  --data '{
    "gremlin": "g.addV(\"document\").property(id,\"doc_scaled\").property(\"embedding\",[2.0d,0.0d,0.0d,0.0d]).property(\"title\",\"Same direction different length\").property(\"category\",\"guide\").property(\"status\",\"online\")"
  }'

Update vector properties

To update the vector property of a vertex, also use property(). The new vector must meet the dimension requirements in the schema:

g.V('doc_same')
  .property('embedding', [0.9d, 0.1d, 0.0d, 0.0d])

The corresponding HTTP request is as follows:

curl -sS -u "${SUB_USER}:${SUB_PASSWORD}" -X POST \
  "http://${SERVER_HOST}:${GREMLIN_PORT}/gremlin/${DB_NAME}" \
  -H 'Content-Type: application/json' \
  --data '{
    "gremlin": "g.V(\"doc_same\").property(\"embedding\",[0.9d,0.1d,0.0d,0.0d])"
  }'

Build vector indexes

Note

For more information about vector index build, including constraints and considerations, see the index build section in Basic features of the Vector Engine documentation.

HNSW type

HNSW indexes take effect immediately when data is written. No manual index build is required.

HNSWQ, IVFPQ, and IVFBQ types

Important
  • For HNSWQ, IVFPQ, and IVFBQ indexes, you must first write sufficient training data before manually triggering the build. Make sure that all data is written and meets the minimum data volume requirement before triggering the build.

  • After the manual index build for HNSWQ, IVFPQ, and IVFBQ is complete, you can continue to write and query data normally. No rebuild is required.

The minimum training data volumes are as follows:

Index type

Minimum data volume

HNSWQ

dimension records. For example, if the dimension is 128, write at least 128 records.

IVFPQ

max(30*NLIST, 256) records. For example, if NLIST is 1000, write at least 30000 records.

IVFBQ

max(30*NLIST, 256) records. For example, if NLIST is 1000, write at least 30000 records.

The manual build API is:

POST /schema/mgmt/index/build?db={dbName}

Example curl command:

curl -X POST \
  "http://${SERVER_HOST}:${GREMLIN_PORT}/schema/mgmt/index/build?db=${DB_NAME}" \
  -u "${SUB_USER}:${SUB_PASSWORD}" \
  -H 'Content-Type: application/json' \
  -d '{
    "label": "person",
    "propertyName": "embedding"
  }'

The request parameters are as follows.

Parameter

Required

Description

label

Yes

The vertex type name.

propertyName

Yes

The name of the vector property for which to build the index.

Example response:

{
  "status": "200 OK",
  "payload": {
    "label": "person",
    "propertyName": "embedding",
    "message": "Build index job submitted. Use GET /schema/mgmt/index/progress to poll for completion."
  }
}

A successful response indicates that the build job has been submitted, not that the index build is complete. You must continue to poll the build progress until the status changes to DONE and indexBuilt is true.

Query index build progress

Request format

The progress query API is:

GET /schema/mgmt/index/progress?db={dbName}&label={label}&propertyName={propertyName}

Example curl command:

curl -X GET \
  "http://${SERVER_HOST}:${GREMLIN_PORT}/schema/mgmt/index/progress?db=${DB_NAME}&label=person&propertyName=embedding" \
  -u "${SUB_USER}:${SUB_PASSWORD}"

The request parameters are as follows.

Parameter

Required

Description

db

Yes

The database name, also known as the subgraph name.

label

Yes

The vertex type name.

propertyName

Yes

The vector property name.

Response and status description

Example response:

{
  "status": "200 OK",
  "payload": {
    "label": "person",
    "propertyName": "embedding",
    "status": "DONE",
    "indexBuilt": true
  }
}

The response fields are described as follows.

Field

Description

status

The build job status. Valid values: RUNNING, DONE, and FAILED.

indexBuilt

true indicates that the index build is complete and the query route has taken effect.

errorInfo

Returned when error details are available. You can use this field to troub the cause of a build failure.

The status values are described as follows.

Status

Description

RUNNING

The index is being built.

DONE

The index build is complete. Also confirm that indexBuilt=true.

FAILED

The index build failed. Check errorInfo for the cause.

Schema query

Note

For more information about the schema query API, including constraints and considerations, see the query current schema section in Graph Schema Definition.

For vector properties in the schema, indexed is always false. indexBuilt indicates whether the index has been built through an explicit build job.

HNSW indexes take effect immediately when data is written. After the schema is created and before any explicit build call, both indexed and indexBuilt are false. This does not affect HNSW queries. A complete sample response is as follows:

{
  "status": "200 OK",
  "payload": {
    "schemaVersion": 1,
    "vertexLabels": {
      "document": [
        {
          "name": "embedding",
          "dataType": "VECTOR_FLOAT",
          "indexed": false,
          "indexBuilt": false,
          "vectorMeta": {
            "dimension": 128,
            "distanceMethod": "L2",
            "indexType": "HNSW",
            "indexParams": {
              "M": "16",
              "EF_CONSTRUCT": "200"
            }
          }
        }
      ]
    },
    "edgeLabels": {},
    "edgeConnections": {}
  }
}

For property types that require training, such as HNSWQ, IVFPQ, and IVFBQ, indexBuilt changes to true after a successful explicit build. The build job runs asynchronously after submission, so an immediate schema query may still show false. Use the progress API returning DONE with indexBuilt=true as the confirmation of completion.

The following is the schema data for a 128-dimensional HNSWQ vector property named embedding before the index build. Both indexed and indexBuilt are false.

{
  "status": "200 OK",
  "payload": {
    "schemaVersion": 1,
    "vertexLabels": {
      "document": [
        {
          "name": "embedding",
          "dataType": "VECTOR_FLOAT",
          "indexed": false,
          "indexBuilt": false,
          "vectorMeta": {
            "dimension": 128,
            "distanceMethod": "L2",
            "indexType": "HNSWQ",
            "indexParams": {
              "M": "16",
              "EF_CONSTRUCT": "200"
            }
          }
        }
      ]
    },
    "edgeLabels": {},
    "edgeConnections": {}
  }
}

After calling the index build API and confirming that the build is complete, the schema data may look like the following. indexed remains false, and indexBuilt is updated to true.

{
  "status": "200 OK",
  "payload": {
    "schemaVersion": 2,
    "vertexLabels": {
      "document": [
        {
          "name": "embedding",
          "dataType": "VECTOR_FLOAT",
          "indexed": false,
          "indexBuilt": true,
          "vectorMeta": {
            "dimension": 128,
            "distanceMethod": "L2",
            "indexType": "HNSWQ",
            "indexParams": {
              "M": "16",
              "EF_CONSTRUCT": "200"
            }
          }
        }
      ]
    },
    "edgeLabels": {},
    "edgeConnections": {}
  }
}

Recommended operation procedure

HNSW

  1. Define a VECTOR_FLOAT property for the vertex in the schema, and set indexType to HNSW.

  2. Write vector data that meets the dimension requirements.

  3. The index takes effect automatically after data is written. No build job is required.

HNSWQ, IVFPQ, and IVFBQ

  1. Define the vector index of the corresponding type in the schema.

  2. Write vector data that meets or exceeds the minimum training data volume.

  3. Call /schema/mgmt/index/build to submit the index build job.

  4. Poll /schema/mgmt/index/progress.

  5. Wait for status to return DONE, and confirm that indexBuilt is true.

If the build job fails, check errorInfo in the response to troubleshoot the issue. Do not treat the index as available until the build is complete.