基于磁盘的向量搜索
2.17 版本引入
对于低内存环境,OpenSearch 提供了基于磁盘的向量搜索,这显著降低了向量工作负载的运营成本。基于磁盘的向量搜索使用二进制量化,通过压缩向量来减少内存需求。这种内存优化以略微增加搜索延迟为代价,提供了大量的内存节省,同时仍保持了较高的召回率。
要使用基于磁盘的向量搜索,请将向量字段类型的 mode
参数设置为 on_disk
。此参数会将您的索引配置为使用辅助存储。有关基于磁盘的搜索参数的更多信息,请参阅内存优化向量。
为基于磁盘的向量搜索创建索引
要为基于磁盘的向量搜索创建索引,请发送以下请求
PUT my-vector-index
{
"settings" : {
"index": {
"knn": true
}
},
"mappings": {
"properties": {
"my_vector_field": {
"type": "knn_vector",
"dimension": 8,
"space_type": "innerproduct",
"data_type": "float",
"mode": "on_disk"
}
}
}
}
默认情况下,on_disk
模式将索引配置为使用 faiss
引擎和 hnsw
方法。默认的 compression_level
为 32x
,可将向量所需的内存量减少 32 倍。为保留搜索召回率,默认启用重评分。对磁盘优化索引的搜索分两个阶段运行:首先搜索压缩索引,然后使用从磁盘加载的全精度向量对结果进行重评分。
要降低压缩级别,请在创建索引映射时提供 compression_level
参数
PUT my-vector-index
{
"settings" : {
"index": {
"knn": true
}
},
"mappings": {
"properties": {
"my_vector_field": {
"type": "knn_vector",
"dimension": 8,
"space_type": "innerproduct",
"data_type": "float",
"mode": "on_disk",
"compression_level": "16x"
}
}
}
}
有关 compression_level
参数的更多信息,请参阅压缩级别。请注意,对于 4x
压缩,将使用 lucene
引擎。
如果需要更精细的调优,可以在方法定义中覆盖其他 k-NN 参数。例如,要提高召回率,请增加 ef_construction
参数的值
PUT my-vector-index
{
"settings" : {
"index": {
"knn": true
}
},
"mappings": {
"properties": {
"my_vector_field": {
"type": "knn_vector",
"dimension": 8,
"space_type": "innerproduct",
"data_type": "float",
"mode": "on_disk",
"method": {
"params": {
"ef_construction": 512
}
}
}
}
}
}
on_disk
模式仅适用于 float
数据类型。
摄入
您可以像对常规向量索引一样,对磁盘优化向量索引执行文档摄入。要批量索引多个文档,请发送以下请求
POST _bulk
{ "index": { "_index": "my-vector-index", "_id": "1" } }
{ "my_vector_field": [1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], "price": 12.2 }
{ "index": { "_index": "my-vector-index", "_id": "2" } }
{ "my_vector_field": [2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5], "price": 7.1 }
{ "index": { "_index": "my-vector-index", "_id": "3" } }
{ "my_vector_field": [3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5], "price": 12.9 }
{ "index": { "_index": "my-vector-index", "_id": "4" } }
{ "my_vector_field": [4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5], "price": 1.2 }
{ "index": { "_index": "my-vector-index", "_id": "5" } }
{ "my_vector_field": [5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5], "price": 3.7 }
{ "index": { "_index": "my-vector-index", "_id": "6" } }
{ "my_vector_field": [6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5], "price": 10.3 }
{ "index": { "_index": "my-vector-index", "_id": "7" } }
{ "my_vector_field": [7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5], "price": 5.5 }
{ "index": { "_index": "my-vector-index", "_id": "8" } }
{ "my_vector_field": [8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5], "price": 4.4 }
{ "index": { "_index": "my-vector-index", "_id": "9" } }
{ "my_vector_field": [9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5], "price": 8.9 }
搜索
搜索也以与其他索引配置相同的方式执行。关键区别在于,默认情况下,重评分参数的 oversample_factor
设置为 3.0
(除非您覆盖 compression_level
)。有关更多信息,请参阅将量化结果重新评分至全精度。要在磁盘优化索引上执行向量搜索,请提供搜索向量
GET my-vector-index/_search
{
"query": {
"knn": {
"my_vector_field": {
"vector": [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5],
"k": 5
}
}
}
}
与其他索引配置类似,您可以在搜索请求中覆盖 k-NN 参数
GET my-vector-index/_search
{
"query": {
"knn": {
"my_vector_field": {
"vector": [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5],
"k": 5,
"method_parameters": {
"ef_search": 512
},
"rescore": {
"oversample_factor": 10.0
}
}
}
}
}
径向搜索不支持基于磁盘的向量搜索。
基于模型的索引
对于基于模型的索引,您可以在训练请求中指定 on_disk
参数,方式与在索引创建期间指定它相同。默认情况下,on_disk
模式将使用Faiss IVF 方法和 32x
的压缩级别。要运行训练 API,请发送以下请求
POST /_plugins/_knn/models/test-model/_train
{
"training_index": "train-index-name",
"training_field": "train-field-name",
"dimension": 8,
"max_training_vector_count": 1200,
"search_size": 100,
"description": "My model",
"space_type": "innerproduct",
"mode": "on_disk"
}
此命令假定训练数据已摄入到 train-index-name
索引中。有关更多信息,请参阅从模型构建向量索引。
您可以像对常规向量索引一样,对磁盘优化索引覆盖 compression_level
。