写入与查询自动Embedding

自动Embedding技术通过内置预训练模型,将文本自动转化为向量,消除了传统方案中手动定义向量字段的繁琐流程。本文介绍了如何在Lindorm系统中实现自动Embedding数据的写入与查询方法。

前提条件

  • 已开通向量引擎。如何开通,请参见开通向量引擎

  • 已开通搜索引擎,且搜索引擎为3.9.10及以上版本。如何开通,请参见开通指南。如何查看或升级当前版本,请参见搜索引擎版本说明升级小版本

    重要

    如果您的搜索引擎为3.9.10以下版本,但控制台显示已是最新版本,请联系Lindorm技术支持(钉钉号:s0s3eg3)。

  • 已开通AI引擎。如何开通,请参见开通指南

    说明

    由于AI引擎的功能实现依赖于宽表引擎,因此在开通AI引擎时必须同时开通宽表引擎。

  • 已将客户端IP地址添加至Lindorm白名单,具体操作请参见设置白名单

操作步骤概览

操作步骤

涉及引擎

说明

AI引擎部署Embedding模型

AI引擎

通过curl命令调用AI引擎RESTful API,部署Embedding模型BGE-M3,用于将文本数据转换为向量。

创建写入Pipeline

搜索引擎

在搜索引擎中创建写入Pipeline,用于在写入数据时,自动将文本数据转换为向量数据(Embedding)。

创建查询Pipeline

搜索引擎

在搜索引擎中创建查询Pipeline,用于在查询数据时,自动将文本数据转化为向量数据。

创建索引并指定Pipeline

向量引擎,

搜索引擎

在创建或修改向量索引时,需指定写入和查询Pipeline,用于将写入与查询数据自动转换为向量数据。

数据写入

向量引擎,搜索引擎

使用指定的写入Pipeline,将写入的文本数据自动转化为向量数据。

数据查询

向量引擎,搜索引擎

使用指定的查询Pipeline,将查询的文本数据自动转化为向量数据。

AI引擎部署Embedding模型

AI引擎部署模型的具体操作请参见模型管理通过curl命令使用AI引擎RESTful API示例

部署BGE-M3模型示例如下,参数详情请参见模型管理

重要

curl请求地址URL使用AI引擎的专用网络连接地址。

curl -i -k --location --header 'x-ld-ak:<username>' --header 'x-ld-sk:<password>' -X POST http://<URL>/v1/ai/models/create  -H "Content-Type: application/json" -d '{
          "model_name": "bge_m3_model",
          "model_path": "huggingface://BAAI/bge-m3",
          "task": "FEATURE_EXTRACTION",
          "algorithm": "BGE_M3",
          "settings": {"instance_count": "2"}
     }'

搜索引擎创建Pipeline

在搜索引擎中创建两种Pipeline,分别用于实现数据写入和查询的自动向量化处理。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

创建写入Pipeline

创建一个名为write_embedding_pipeline的写入Pipeline。

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_ingest/pipeline/write_embedding_pipeline" -d '{
  "description": "demo_chunking pipeline",
  "processors": [
    {
      "text-embedding": {
        "inputFields": ["text_field"], 
        "outputFields": ["text_field_embedding"],
        "userName": "root",
        "password": "test****",
        "url": "http://ld-2zeyhrpnv9n7a****-proxy-ai-vpc.lindorm.aliyuncs.com:9002",
        "modeName": "bge_m3_model"
      }
    }
  ]
}'

参数说明

参数

说明

processors

对写入进行Pipeline操作。

text-embedding

固定Key,必须填写。

inputFields

需要进行向量化的文本字段。

outputFields

向量化后的向量字段。

userName

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

AI引擎的连接地址,务必使用专有网络连接地址。

modeName

模型名称,本文对应bge_m3_model

说明

写入和查询Pipeline中指定的inputFieldsoutputFields,必须与创建向量索引时填写的text_fieldtext_field_embedding保持一致。

创建查询Pipeline

创建一个名为knnsearch_pipeline的查询Pipeline。

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_search/pipeline/knnsearch_pipeline" -d '{
  "request_processors": [
    {
      "text-embedding" : {
        "tag" : "auto-query-embedding",
        "description" : "Auto query embedding",
        "model_config" : {
          "inputFields": ["text_field"],
          "outputFields": ["text_field_embedding"],
          "userName": "root",
          "password": "test****",
          "url": "http://ld-2zeyhrpnv9n7a****-proxy-ai-vpc.lindorm.aliyuncs.com:9002",
          "modeName": "bge_m3_model"
        }
      }
    }
  ]
}'

参数说明

参数

说明

request_processors

表示对搜索请求进行Pipeline操作。

text-embedding

固定Key,必须填写。

inputFields

需要进行向量化的文本字段,起到占位作用。

outputFields

向量化以后的向量字段。

userName

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

AI引擎的连接地址,务必使用专有网络连接地址。

modeName

模型名称,本文对应bge_m3_model

说明

写入和查询Pipeline中指定的inputFieldsoutputFields,必须与创建向量索引时填写的text_fieldtext_field_embedding保持一致。

创建索引并指定Pipeline

在创建向量索引或修改现有向量索引设置时,请指定所需的Pipeline。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

创建向量索引

本示例的索引名称为search_vector_test

curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "http://<URL>/search_vector_test?pretty"  -d '
{
 "settings" : {
    "index": {
      "number_of_shards": 2,
      "knn": true,
      "default_pipeline": "write_embedding_pipeline",
      "search.default_pipeline": "knnsearch_pipeline"
    }
  },
  "mappings": {
    "_source": {
      "excludes": ["text_field_embedding"]
    },
    "properties": {
      "text_field": {
        "type": "text",
        "analyzer": "ik_max_word" 
      },
      "text_field_embedding": {
        "type": "knn_vector",
        "dimension": 1024,
        "data_type": "float",
        "method": {
          "engine": "lvector",
          "name": "hnsw", 
          "space_type": "cosinesimil",
          "parameters": {
            "m": 24,
            "ef_construction": 500
         }
       }
      },
      "tag": {
          "type": "keyword"
      },
      "brand": {
          "type": "keyword"
      },
      "merit" : {
        "type": "text",
        "analyzer": "ik_max_word"  
      }
    }
  }
}'

参数说明

  • default_pipeline:设置写入Pipeline。

  • search.default_pipeline:设置查询时自动EmbeddingPipeline。

  • type:为Keyword对应等值,是text分词。需要指定分词器analyzer,建议设置为ik_max_word

  • BGE-M3模型对应的space_typecosinesimildimension1024

  • 其余参数说明请参见参数说明

说明

写入和查询Pipeline中指定的inputFieldsoutputFields,必须与创建向量索引时填写的text_fieldtext_field_embedding保持一致。

修改现有向量索引设置

如果您已经创建了向量索引,可以通过以下方式修改其配置,指定写入和查询时使用的 Pipeline,以满足特定的业务需求。

curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "http://<URL>/search_vector_test/_settings"  -d '
{
  "index": {
       "default_pipeline": "write_embedding_pipeline",
        "search.default_pipeline": "knnsearch_pipeline"
    }
}
'

数据写入

由于指定了写入的Pipeline,因此,在写入过程中,除了将文本字段text_field写入外,还会根据该Pipelinetext_field编码成向量形式,并将其作为text_field_embedding一并写入。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

curl -u <username>:<password> -H 'Content-Type: application/json' -XPOST "http://<URL>/_bulk?pretty"  -d '
{ "index" : { "_index" : "search_vector_test", "_id" : "3982" } }
{ "text_field" : "品牌A 时尚节能无线鼠标(草绿)(眩光.悦动.时尚炫舞鼠标 12个月免换电池 高精度光学寻迹引擎 超细微接收器10米传输距离)", "tag": ["鼠标", "电子产品"], "brand":"品牌A", "merit":"好用、外观漂亮"}
{ "index" : { "_index" : "search_vector_test", "_id" : "323519" } }
{ "text_field" : "品牌B 光学鼠标(经典黑)(智能自动对码/1000DPI高精度光学引擎)", "tag": ["鼠标", "电子产品"], "brand":"品牌B", "merit":"质量好、到货速度快、外观漂亮、好用"}
{ "index" : { "_index" : "search_vector_test", "_id" : "300265" } }
{ "text_field" : "品牌C 耳塞式耳机 白色(经典时尚)", "tag": ["耳机", "电子产品"], "brand":"品牌C", "merit":"外观漂亮、质量好"}
{ "index" : { "_index" : "search_vector_test", "_id" : "6797" } }
{ "text_field" : "品牌D 两刀头充电式电动剃须刀", "tag": ["家用电器", "电动剃须刀"], "brand":"品牌D", "merit":"好用、外观漂亮"}
{ "index" : { "_index" : "search_vector_test", "_id" : "8195" } }
{ "text_field" : "品牌E Class4 32G TF卡(micro SD)手机存储卡", "tag": ["存储设备", "存储卡", "SD卡"], "brand":"品牌E", "merit":"容量挺大的、速度快、好用、质量好"}
{ "index" : { "_index" : "search_vector_test", "_id" : "13316" } }
{ "text_field" : "品牌E 101 G2 32GB 优盘", "tag": ["存储设备","U盘", "优盘"], "brand":"品牌E", "merit":"好用、容量挺大的、速度快"}
{ "index" : { "_index" : "search_vector_test", "_id" : "14103" } }
{ "text_field" : "品牌B 64GB至尊高速移动存储卡 UHS-1制式 读写速度最高可达30MB", "tag": ["存储设备", "存储卡", "SD卡"], "brand":"品牌B", "merit":"容量挺大的、速度快、好用"}
'

数据查询

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

纯向量

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://<URL>/search_vector_test/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "text_field_embedding": {
        "query_text": "存储卡",
        "k": 10
      }
    }
  },
  "ext": {
    "lvector": {
      "ef_search": "200"
    }
  }
}'
重要
  • text-embedding 类型的Request_Processor仅适用于KNN查询,且KNN查询中指定的向量列必须包含在Pipeline配置文件里指定的outputFields列表中。

  • 在构造KNN查询时,请始终通过query_text参数来提供待Embedding的文本内容。

参数说明

  • query_text:查询文本,会用这个值做Embedding。query_text为固定Key。

  • 其余参数说明请参见参数说明

返回结果

{
  "took" : 46,
  "timed_out" : false,
  "terminated_early" : false,
  "num_reduce_phases" : 0,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : 0.7433592,
    "hits" : [
      {
        "_index" : "search_vector_test",
        "_id" : "8195",
        "_score" : 0.7433592,
        "_source" : {
          "text_field" : "品牌E Class4 32G TF卡(micro SD)手机存储卡",
          "merit" : "容量挺大的、速度快、好用、质量好",
          "tag" : [
            "存储设备",
            "存储卡",
            "SD卡"
          ],
          "brand" : "品牌E"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "14103",
        "_score" : 0.7116537,
        "_source" : {
          "text_field" : "品牌B 64GB至尊高速移动存储卡 UHS-1制式 读写速度最高可达30MB",
          "merit" : "容量挺大的、速度快、好用",
          "tag" : [
            "存储设备",
            "存储卡",
            "SD卡"
          ],
          "brand" : "品牌B"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "13316",
        "_score" : 0.6831677,
        "_source" : {
          "text_field" : "品牌E 101 G2 32GB 优盘",
          "merit" : "好用、容量挺大的、速度快",
          "tag" : [
            "存储设备",
            "U盘",
            "优盘"
          ],
          "brand" : "品牌E"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "3982",
        "_score" : 0.64234203,
        "_source" : {
          "text_field" : "品牌A 时尚节能无线鼠标(草绿)(眩光.悦动.时尚炫舞鼠标 12个月免换电池 高精度光学寻迹引擎 超细微接收器10米传输距离)",
          "merit" : "好用、外观漂亮",
          "tag" : [
            "鼠标",
            "电子产品"
          ],
          "brand" : "品牌A"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "6797",
        "_score" : 0.6357207,
        "_source" : {
          "text_field" : "品牌D 两刀头充电式电动剃须刀",
          "merit" : "好用、外观漂亮",
          "tag" : [
            "家用电器",
            "电动剃须刀"
          ],
          "brand" : "品牌D"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "323519",
        "_score" : 0.62445086,
        "_source" : {
          "text_field" : "品牌B 光学鼠标(经典黑)(智能自动对码/1000DPI高精度光学引擎)",
          "merit" : "质量好、到货速度快、外观漂亮、好用",
          "tag" : [
            "鼠标",
            "电子产品"
          ],
          "brand" : "品牌B"
        }
      },
      {
        "_index" : "search_vector_test",
        "_id" : "300265",
        "_score" : 0.62144196,
        "_source" : {
          "text_field" : "品牌C 耳塞式耳机 白色(经典时尚)",
          "merit" : "外观漂亮、质量好",
          "tag" : [
            "耳机",
            "电子产品"
          ],
          "brand" : "品牌C"
        }
      }
    ]
  }
}

向量+属性过滤

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://<URL>/search_vector_test/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "text_field_embedding": {
        "query_text": "存储卡",
        "k": 10,
        "filter": {
          "bool": {
            "filter": [{
              "match": {
                "merit": "质量好"
              }
            },
            {
              "term": {
                "brand": "品牌E"
              }
            },
            {
              "terms": {
                "tag": ["SD卡", "存储卡"]
              }
            }]
          }
        }
      }
    }
  },
  "ext": {
    "lvector": {
      "filter_type": "efficient_filter",
      "ef_search": "200"
    }
  }
}'

参数说明

  • query_text:查询文本,会用这个值做Embedding。query_text为固定Key。

  • 其余参数说明请参见参数说明

返回结果

{
  "took" : 73,
  "timed_out" : false,
  "terminated_early" : false,
  "num_reduce_phases" : 0,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.7433592,
    "hits" : [
      {
        "_index" : "search_vector_test",
        "_id" : "8195",
        "_score" : 0.7433592,
        "_source" : {
          "text_field" : "品牌E Class4 32G TF卡(micro SD)手机存储卡",
          "merit" : "容量挺大的、速度快、好用、质量好",
          "tag" : [
            "存储设备",
            "存储卡",
            "SD卡"
          ],
          "brand" : "品牌E"
        }
      }
    ]
  }
}

向量+全文+属性过滤

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://<URL>/search_vector_test/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "text_field_embedding": {
        "query_text": "存储卡",
        "filter": {
          "bool": {
            "must": [{
              "bool": {
                "must": [{
                  "match": {
                    "text_field": {
                      "query": "存储卡"
                    }
                  }
                }]
              }
            },
            {
              "bool": {
                "filter": [{
                  "match": {
                    "merit": "质量好"
                  }
                },
                {
                  "term": {
                    "brand": "品牌E"
                  }
                },
                {
                  "terms": {
                    "tag": ["SD卡", "存储卡"]
                  }
                }]
              }
            }]
          }
        },
        "k": 10
      }
    }
  },
  "ext": {
    "lvector": {
      "filter_type": "efficient_filter",
      "hybrid_search_type": "filter_rrf",
      "rrf_rank_constant": "1",
      "ef_search": "200"
    }
  }
}'

参数说明

  • query_text:查询文本,会用这个值做Embedding。query_text为固定Key。

  • 其余参数说明请参见参数说明

返回结果

{
  "took" : 95,
  "timed_out" : false,
  "terminated_early" : false,
  "num_reduce_phases" : 0,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "search_vector_test",
        "_id" : "8195",
        "_score" : 1.0,
        "_source" : {
          "text_field" : "品牌E Class4 32G TF卡(micro SD)手机存储卡",
          "merit" : "容量挺大的、速度快、好用、质量好",
          "tag" : [
            "存储设备",
            "存储卡",
            "SD卡"
          ],
          "brand" : "品牌E"
        }
      }
    ]
  }
}