Lindorm向量引擎支持向量数据检索功能,兼容Elasticsearch协议,同时支持标量、向量、全文混合检索功能。本文介绍如何通过curl命令连接并使用向量引擎。
前提条件
已开通Lindorm向量引擎。如何开通,请参见开通向量引擎。
已开通Lindorm搜索引擎。具体操作,请参见开通指南。
已连接Lindorm搜索引擎。如何连接,请参见通过curl命令连接并使用搜索引擎。
已将客户端的IP地址加入到Lindorm实例的白名单中。具体操作,请参见设置白名单。
创建向量索引
创建向量索引,其中vector1
为向量列、field1
为普通列。向量列及其相关参数必须在创建索引时通过mappings结构显式指定。
curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "<URL>/<索引名称>" -d '
{
"settings" : {
"index": {
"number_of_shards": 4,
"knn": true
}
},
"mappings": {
"properties": {
"vector1": {
"type": "knn_vector",
"dimension": 3,
"data_type": "float",
"method": {
"engine": "lvector",
"name": "hnsw",
"space_type": "l2",
"parameters": {
"m": 24,
"ef_construction": 128
}
}
},
"field1": {
"type": "long"
}
}
}
}'
参数说明
连接参数说明
参数 | 是否必填 | 示例值 | 说明 |
URL | 是 | ld-bp106782jm96****-proxy-search-vpc.lindorm.aliyuncs.com:30070 | 搜索引擎的Elasticsearch兼容连接地址。如何获取,请参见Elasticsearch兼容地址。 |
username | 是 | xltest | 访问向量引擎的用户名和密码。 默认用户名和密码的获取方式:在控制台的左侧导航栏,选择数据库连接,单击搜索引擎页签,在搜索引擎页签可获取。 |
password | 是 | test |
向量列参数说明
通用参数
参数 | 是否必填 | 说明 |
type | 是 | 索引列的类型。对于向量列,固定为 |
dimension | 是 | 向量数据的维度。取值范围:[1,32768]。 |
data_type | 否 | 向量数据的类型。目前仅支持 |
method.name | 是 | 向量数据的索引算法。取值如下:
|
method.space_type | 否 | 向量数据的距离算法。取值如下:
|
HNSW算法参数
参数 | 是否必填 | 说明 |
method.parameters.m | 否 | 每一层图的最大出边数量。 取值范围:[1,100]。默认值为 |
method.parameters.ef_construction | 否 | 索引构建时动态列表的长度。 取值范围:[1,1000]。默认值为 |
IVFPQ算法参数
参数 | 是否必填 | 说明 |
method.parameters.m | 否 | 量化中子空间的数量。 取值范围:[2,32768]。默认值为 |
method.parameters.nlist | 否 | 聚类中心的数量。 取值范围:[2, 1000000]。默认值为 |
method.parameters.centroids_use_hnsw | 否 | 是否在聚类中心搜索时使用HNSW算法。 取值如下:
|
method.parameters.centroids_hnsw_m | 否 | 若在聚类中心搜索时使用HNSW算法,设定HNSW算法的每一层图的最大出边数量。 取值范围:[1,100]。默认值为 |
method.parameters.centroids_hnsw_ef_construct | 否 | 若在聚类中心搜索时使用HNSW算法,设定HNSW算法在索引构建时动态列表的长度。 取值范围:[1,1000]。默认值为 |
method.parameters.centroids_hnsw_ef_search | 否 | 若在聚类中心搜索时使用HNSW算法,设定HNSW算法在查询时动态列表的长度。 取值范围:[1,1000]。默认值为 |
假设向量索引名为vector_test
,返回结果如下:
{"acknowledged":true,"shards_acknowledged":true,"index":"vector_test"}
数据写入
包含向量列的索引与普通索引的数据写入方式一致。
单条写入
curl -u <username>:<password> -H 'Content-Type: application/json' -XPOST "<URL>/<索引名称>/_doc/1?pretty" -d '
{"field1": 1, "vector1": [1.2, 1.3, 1.4]}'
以索引vector_test
为例,返回结果如下:
{"_index":"vector_test","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
批量写入
curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "<URL>/_bulk?pretty" -d '
{ "index" : { "_index" : "vector_test", "_id" : "2" } }
{ "field1" : 1, "vector1": [2.2, 2.3, 2.4]}
{ "index" : { "_index" : "vector_test", "_id" : "3" } }
{ "field1" : 2, "vector1": [1.2, 1.3, 4.4]}
{ "delete" : { "_index" : "vector_test", "_id" : "2" } }
{ "update" : {"_id" : "1", "_index" : "vector_test"} }
{ "doc" : {"field1" : 3, "vector1": [2.2, 3.3, 4.4]} }
'
以索引vector_test
为例,返回结果如下:
{"took":513,"errors":false,"items":[{"index":{"_index":"vector_test","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1,"status":201}},{"index":{"_index":"vector_test","_type":"_doc","_id":"3","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"delete":{"_index":"vector_test","_type":"_doc","_id":"2","_version":2,"result":"deleted","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1,"status":200}},{"update":{"_index":"vector_test","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":3,"_primary_term":1,"status":200}}]}
数据查询
纯向量数据查询
纯向量数据的查询可以通过knn结构实现。
curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "<URL>/<索引名称>/_search?pretty" -d '
{
"size": 10,
"query": {
"knn": {
"vector1": {
"vector": [2.3, 3.3, 4.4],
"k": 10
}
}
},
"ext": {"lvector": {"min_score": "0.8"}}
}'
参数说明
参数结构 | 参数 | 是否必填 | 说明 |
knn | vector | 是 | 查询时使用的向量。 |
k | 是 | 返回最相似的K个数据。 | |
ext | lvector.min_score | 否 | 相似度阈值,要求返回的向量得分大于该值。返回的向量得分范围为[0,1]。 取值范围:[0,+inf]。默认值为 |
lvector.filter_type | 否 | 融合查询使用的模式。取值如下:
默认值为空。 | |
lvector.ef_search | 否 | HNSW算法中,索引构建时动态列表的长度。只能用于HNSW算法。 取值范围:[1,1000]。默认值为 | |
lvector.nprobe | 否 | 要查询的聚类单元(cluster units)的数量。请根据您的召回率要求,对该参数的值进行调整已达到理想效果。值越大,召回率约高,搜索性能越低。 取值范围:[1,method.parameters.nlist]。无默认值。 | |
lvector.reorder_factor | 否 | 使用原始向量创建重排序(reorder)。通常用于提升召回精度,但会增加性能开销。 取值范围:[1,200]。默认值为 |
以索引vector_test
为例,返回结果如下:
{
"took" : 112,
"timed_out" : false,
"terminated_early" : false,
"num_reduce_phases" : 0,
"_shards" : {
"total" : 4,
"successful" : 4,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.990099,
"hits" : [
{
"_index" : "vector_test",
"_id" : "1",
"_score" : 0.990099,
"_source" : {
"vector1" : [
2.2,
3.3,
4.4
]
}
},
{
"_index" : "vector_test",
"_id" : "3",
"_score" : 0.16103059,
"_source" : {
"vector1" : [
1.2,
1.3,
4.4
]
}
}
]
}
}
融合查询
向量列的查询可与普通列的查询条件结合,并返回综合的查询结果。
Pre-Filter近似查询
通过在knn查询结构内部添加过滤器filter,并指定filter_type参数的值为pre_filter
,可实现先过滤结构化数据,再查询向量数据。
curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "<URL>/<索引名称>/_search?pretty" -d '
{
"size": 10,
"query": {
"knn": {
"vector1": {
"vector": [2.3, 3.3, 4.4],
"filter": {
"range": {
"field1": {
"gte": 0
}
}
},
"k": 10
}
}
},
"ext": {"lvector": {"filter_type": "pre_filter"}}
}'
以查询索引vector_test
为例,返回结果如下:
{
"took" : 115,
"timed_out" : false,
"terminated_early" : false,
"num_reduce_phases" : 0,
"_shards" : {
"total" : 4,
"successful" : 4,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.990099,
"hits" : [
{
"_index" : "vector_test",
"_id" : "1",
"_score" : 0.990099,
"_source" : {
"vector1" : [
2.2,
3.3,
4.4
]
}
},
{
"_index" : "vector_test",
"_id" : "3",
"_score" : 0.16103059,
"_source" : {
"vector1" : [
1.2,
1.3,
4.4
]
}
}
]
}
}
Post-Filter近似查询
通过在knn查询结构内部添加过滤器filter,并指定filter_type参数的值为post_filter
,可实现先查询向量数据,再过滤结构化数据。
curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "<URL>/<索引名称>/_search?pretty" -d '
{
"size": 10,
"query": {
"knn": {
"vector1": {
"vector": [2.3, 3.3, 4.4],
"filter": {
"range": {
"field1": {
"gte": 0
}
}
},
"k": 10
}
}
},
"ext": {"lvector": {"filter_type": "post_filter"}}
}'
以查询索引vector_test
为例,返回结果如下:
{
"took" : 437,
"timed_out" : false,
"_shards" : {
"total" : 4,
"successful" : 4,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.990099,
"hits" : [
{
"_index" : "vector_test",
"_id" : "1",
"_score" : 0.990099,
"_source" : {
"field1" : 3,
"vector1" : [
2.2,
3.3,
4.4
]
}
},
{
"_index" : "vector_test",
"_id" : "3",
"_score" : 0.16103059,
"_source" : {
"field1" : 2,
"vector1" : [
1.2,
1.3,
4.4
]
}
}
]
}
}
您也可以通过post_filter添加过滤条件,实现Post-Filter近似查询。
curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "<URL>/<索引名称>/_search?pretty" -d '
{
"size": 10,
"query": {
"knn": {
"vector1": {
"vector": [2.3, 3.3, 4.4],
"k": 10
}
}
},
"post_filter": {
"range": {
"field1": {
"gte": 0
}
}
}
}'
以查询索引vector_test
为例,返回结果如下:
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 4,
"successful" : 4,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.990099,
"hits" : [
{
"_index" : "vector_test",
"_id" : "1",
"_score" : 0.990099,
"_source" : {
"field1" : 3,
"vector1" : [
2.2,
3.3,
4.4
]
}
},
{
"_index" : "vector_test",
"_id" : "3",
"_score" : 0.16103059,
"_source" : {
"field1" : 2,
"vector1" : [
1.2,
1.3,
4.4
]
}
}
]
}
}
- 本页导读 (1)