surrealdb-vector

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SurrealDB Vector Search

SurrealDB 向量搜索

HNSW Index

HNSW 索引

Create a basic HNSW index:
surql
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4;
With specific distance function and type:
surql
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4 DIST EUCLIDEAN TYPE F64;
Available types:
F64
,
F32
,
I64
,
I32
,
I16
.
创建基础HNSW索引:
surql
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4;
指定距离函数和类型:
surql
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4 DIST EUCLIDEAN TYPE F64;
支持的类型:
F64
,
F32
,
I64
,
I32
,
I16
.

Full Table Example

完整表示例

surql
DEFINE TABLE OVERWRITE document SCHEMALESS;
DEFINE FIELD OVERWRITE embedding ON document TYPE array<float>;
DEFINE INDEX OVERWRITE hnsw_idx_document ON document
    FIELDS embedding
    HNSW DIMENSION 384
    DIST COSINE
    TYPE F32
    EFC 150 M 12 M0 24;
surql
DEFINE TABLE OVERWRITE document SCHEMALESS;
DEFINE FIELD OVERWRITE embedding ON document TYPE array<float>;
DEFINE INDEX OVERWRITE hnsw_idx_document ON document
    FIELDS embedding
    HNSW DIMENSION 384
    DIST COSINE
    TYPE F32
    EFC 150 M 12 M0 24;

HNSW Parameters

HNSW 参数

ParameterDescription
DIMENSIONVector dimensionality (must match your embeddings)
DISTDistance function:
COSINE
,
EUCLIDEAN
, etc.
TYPENumeric type:
F64
,
F32
,
I64
,
I32
,
I16
EFCConstruction search effort (higher = better index)
MMax connections per node
M0Max connections at layer 0
参数描述
DIMENSION向量维度(必须与你的embeddings匹配)
DIST距离函数:
COSINE
,
EUCLIDEAN
TYPE数值类型:
F64
,
F32
,
I64
,
I32
,
I16
EFC构建索引时的搜索力度(值越高,索引质量越好)
M每个节点的最大连接数
M0第0层的最大连接数

Querying Vectors

向量查询

The
<|K, EF|>
operator performs KNN search.
K
is the number of results,
EF
is the search effort (higher = more accurate, slower).
Recommended effort values:
  • 40
    — default, good accuracy
  • 17
    — fast but may miss some results
<|K, EF|>
运算符用于执行KNN搜索。
K
是返回结果的数量,
EF
是搜索力度(值越高,结果越准确,但速度越慢)。
推荐的力度值:
  • 40
    — 默认值,准确性良好
  • 17
    — 速度快,但可能遗漏部分结果

Basic KNN Query

基础KNN查询

surql
SELECT
    *,
    vector::distance::knn() AS dist
FROM document
WHERE embedding <|10, 40|> $vector;
vector::distance::knn()
uses the distance function defined by the index.
surql
SELECT
    *,
    vector::distance::knn() AS dist
FROM document
WHERE embedding <|10, 40|> $vector;
vector::distance::knn()
会使用索引定义的距离函数。

Scored Results with Threshold

带阈值的评分结果

surql
SELECT *, score
FROM (
    SELECT *, (1 - vector::distance::knn()) AS score
    FROM document
    WHERE embedding <|20, 40|> $vector
)
WHERE score >= $threshold
ORDER BY score DESC;
surql
SELECT *, score
FROM (
    SELECT *, (1 - vector::distance::knn()) AS score
    FROM document
    WHERE embedding <|20, 40|> $vector
)
WHERE score >= $threshold
ORDER BY score DESC;