跳到正文

Sparse encoding 处理器 ​

sparse_encoding 处理器用于从文本字段生成稀疏向量/词元和权重,以使用稀疏检索进行神经稀疏搜索。

NOTE

前提条件
在使用 sparse_encoding 处理器之前,你必须设置机器学习(ML)模型。有关更多信息,请参阅选择模型。

以下是 sparse_encoding 处理器的语法:

json
{
  "sparse_encoding": {
    "model_id": "<model_id>",
    "field_map": {
      "<input_field>": "<vector_field>"
    }
  }
}

配置参数 ​

下表列出了 sparse_encoding 处理器的必需和可选参数。

参数数据类型必需/可选描述
model_id字符串必需将用于生成嵌入的模型 ID。模型必须在 OpenSearch 中部署才能用于神经搜索。有关更多信息,请参阅在 OpenSearch 中使用自定义模型和神经稀疏搜索。
field_map对象必需包含指定文本字段到 rank_features 字段映射的键值对。
field_map.<input_field>字符串必需用于获取文本以生成向量嵌入的字段名称。
field_map.<vector_field>字符串必需用于存储生成的向量嵌入的向量字段名称。
description字符串可选处理器的简要描述。
tag字符串可选处理器的标识符标签。用于调试以区分相同类型的处理器。

使用处理器 ​

按照以下步骤在管道中使用处理器。创建处理器时必须提供模型 ID。有关更多信息,请参阅在 OpenSearch 中使用自定义模型。

步骤 1:创建管道

以下示例请求创建一个摄取管道,其中 passage_text 中的文本将被转换为文本嵌入,嵌入将存储在 passage_embedding 中:

json
PUT /_ingest/pipeline/nlp-ingest-pipeline
{
  "description": "稀疏编码摄取管道",
  "processors": [
    {
      "sparse_encoding": {
        "model_id": "aP2Q8ooBpBj3wT4HVS8a",
        "field_map": {
          "passage_text": "passage_embedding"
        }
      }
    }
  ]
}

步骤 2(可选):测试管道

TIP

建议在摄取文档之前测试管道。

要测试管道,请运行以下查询:

json
POST _ingest/pipeline/nlp-ingest-pipeline/_simulate
{
  "docs": [
    {
      "_index": "testindex1",
      "_id": "1",
      "_source":{
         "passage_text": "hello world"
      }
    }
  ]
}

响应 ​

响应确认除了 passage_text 字段外,处理器还在 passage_embedding 字段中生成了文本嵌入:

json
{
  "docs" : [
    {
      "doc" : {
        "_index" : "testindex1",
        "_id" : "1",
        "_source" : {
          "passage_embedding" : {
            "!" : 0.8708904,
            "door" : 0.8587369,
            "hi" : 2.3929274,
            "worlds" : 2.7839446,
            "yes" : 0.75845814,
            "##world" : 2.5432441,
            "born" : 0.2682308,
            "nothing" : 0.8625516,
            "goodbye" : 0.17146169,
            "greeting" : 0.96817183,
            "birth" : 1.2788506,
            "come" : 0.1623208,
            "global" : 0.4371151,
            "it" : 0.42951578,
            "life" : 1.5750692,
            "thanks" : 0.26481047,
            "world" : 4.7300377,
            "tiny" : 0.5462298,
            "earth" : 2.6555297,
            "universe" : 2.0308156,
            "worldwide" : 1.3903781,
            "hello" : 6.696973,
            "so" : 0.20279501,
            "?" : 0.67785245
          },
          "passage_text" : "hello world"
        },
        "_ingest" : {
          "timestamp" : "2023-10-11T22:35:53.654650086Z"
        }
      }
    }
  ]
}

创建摄取管道后,你需要为摄取创建索引并将文档摄取到索引中。要了解更多信息,请参阅神经稀疏搜索中的步骤 2:为摄取创建索引和步骤 3:将文档摄取到索引。


后续步骤 ​