You need to enable JavaScript to run this app.
导航
核心流程
最近更新时间:2024.06.14 11:34:51首次发布时间:2024.06.14 11:34:51

本页面提供一个向量数据里 VikingDB 通过 Nodejs SDK 创建数据集、写入数据、创建索引和检索查询的完整请求示例。

import { vikingdb } from '@volcengine/openapi'

async main() {
  declare const service: vikingdb.VikingdbService
  
  await service.collection.CreateCollection({
    CollectionName: 'test_collection',
    Description: 'a test collection',
    Fields: [
      { FieldName: 'Id', IsPrimary: true, FieldType: vikingdb.FieldType.Int64 },
      { FieldName: 'Name', FieldType: vikingdb.FieldType.String },
      { FieldName: 'Sex', FieldType: vikingdb.FieldType.Bool, DefaultValue: false },
      { FieldName: 'Vector', FieldType: vikingdb.FieldType.DenseVector, Dim: 4 }
    ]
  })
  
  interface Record {
    Id: number
    Name: string
    Sex: boolean
    Vector: [number, number, number, number]
  }
  
  const data: Record[] = [
    { Id: 1, Name: 'Tom', Vector: [1, 2, 3, 4] },
    { Id: 2, Name: 'Jerry', Sex: true, Vector: [2, 3, 4, 5] },
  ]
  
  await service.data.UpsertData({
    CollectionName: 'test_collection',
    Fields: data,
    TTL: 10 * 1000 * 60
  })
  
  await service.index.CreateIndex({
    CollectionName: 'test_collection',
    IndexName: 'test_index',
    Description: 'a test index',
    CpuQuota: 10,
    ShardConfig: {
      ShardPolicy: vikingdb.index.ShardPolicy.Auto
    },
    VectorIndex: {
      IndexType: vikingdb.index.IndexType.Hnsw,
      Distance: vikingdb.index.Distance.Ip,
      Quant: vikingdb.index.Quant.Int8,
    },
    ScalarIndexes: ['Name']
  })
  
  const response = await service.search.SearchByVector<Data>({
    CollectionName: 'test_collection',
    IndexName: 'test_index',
    Limit: 10,
    DenseVector: [[1, 2, 3, 4]]
  })
  
  console.log(response.Data) // SearchResult
}

main()