快速入门
更新时间:2024-05-14
用户可以参考本文档,快速使用BES向量数据库的hnsw算法进行数据写入和向量检索。
前提条件
已创建百度智能云账号。创建账号参考百度云账号注册流程。
创建集群
创建BES集群,请参考文档:创建集群。
如需规划集群的资源规格及数量,请参考文档:资源规划。
访问集群
参考文档:访问Elasticsearch服务。
快速体验
1、创建一个hnsw的索引,详细参数含义可以参考文档:创建索引。
JSON
1PUT my_index
2{
3 "settings": {
4 "index": {
5 "knn": true
6 }
7 },
8 "mappings": {
9 "properties": {
10 "image-vector": {
11 "type": "bpack_vector",
12 "dims": 4,
13 "index_type": "hnsw",
14 "space_type": "l2",
15 "parameters": {
16 "ef_construction": 200,
17 "m": 32
18 }
19 },
20 "id": {
21 "type": "integer"
22 },
23 "title": {
24 "type": "keyword"
25 }
26 }
27 }
28}
2、写入向量数据
JSON
1POST my_index/_bulk
2{"index":{}}
3{"id":1, "title":"t1", "image-vector":[3.5,4.5,6.5,6.5]}
4{"index":{}}
5{"id":2, "title":"t2", "image-vector":[3.5,4.5,6.5,6.5]}
6{"index":{}}
7{"id":3, "title":"t1", "image-vector":[5.5,6.5,6.5,6.5]}
8{"index":{}}
9{"id":4, "title":"t2", "image-vector":[3.5,6.5,6.5,6.5]}
10{"index":{}}
11{"id":5, "title":"t1", "image-vector":[5.5,2.5,4.5,4.5]}
3、索引构建完成后,即可检索数据
Plain Text
1GET my_index/_search
2{
3 "size": 2,
4 "query": {
5 "knn": {
6 "image-vector": {
7 "vector": [3, 4, 5, 6],
8 "k": 2,
9 "ef": 100,
10 "filter": { // 基于标量数据进行过滤,不需要的话可以删掉filter
11 "term": {
12 "title": "t1"
13 }
14 }
15 }
16 }
17 }
18}