文档

通过curl命令连接并使用向量引擎

更新时间:

Lindorm向量引擎支持向量数据检索功能,兼容Elasticsearch协议,同时支持标量、向量、全文混合检索功能。本文介绍如何通过curl命令连接并使用向量引擎。

前提条件

创建向量索引

创建向量索引,其中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

索引列的类型。对于向量列,固定为knn_vector

dimension

向量数据的维度。取值范围:[1,32768]。

data_type

向量数据的类型。目前仅支持float

method.name

向量数据的索引算法。取值如下:

  • flat

  • hnsw

  • ivfpq

method.space_type

向量数据的距离算法。取值如下:

  • l2(默认值

  • cosinesimil

  • innerproduct

HNSW算法参数

参数

是否必填

说明

method.parameters.m

每一层图的最大出边数量。

取值范围:[1,100]。默认值为16

method.parameters.ef_construction

索引构建时动态列表的长度。

取值范围:[1,1000]。默认值为100

IVFPQ算法参数

参数

是否必填

说明

method.parameters.m

量化中子空间的数量。

取值范围:[2,32768]。默认值为16

method.parameters.nlist

聚类中心的数量。

取值范围:[2, 1000000]。默认值为10000

method.parameters.centroids_use_hnsw

是否在聚类中心搜索时使用HNSW算法。

取值如下:

  • true(默认值

  • false

method.parameters.centroids_hnsw_m

若在聚类中心搜索时使用HNSW算法,设定HNSW算法的每一层图的最大出边数量。

取值范围:[1,100]。默认值为16

method.parameters.centroids_hnsw_ef_construct

若在聚类中心搜索时使用HNSW算法,设定HNSW算法在索引构建时动态列表的长度。

取值范围:[1,1000]。默认值为100

method.parameters.centroids_hnsw_ef_search

若在聚类中心搜索时使用HNSW算法,设定HNSW算法在查询时动态列表的长度。

取值范围:[1,1000]。默认值为100

假设向量索引名为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]。默认值为0

lvector.filter_type

融合查询使用的模式。取值如下:

  • pre_filter

  • post_filter

默认值为空。

lvector.ef_search

HNSW算法中,索引构建时动态列表的长度。只能用于HNSW算法。

取值范围:[1,1000]。默认值为100

lvector.nprobe

要查询的聚类单元(cluster units)的数量。请根据您的召回率要求,对该参数的值进行调整已达到理想效果。值越大,召回率约高,搜索性能越低。

取值范围:[1,method.parameters.nlist]。无默认值。

lvector.reorder_factor

使用原始向量创建重排序(reorder)。通常用于提升召回精度,但会增加性能开销。

取值范围:[1,200]。默认值为10

以索引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)