Elasticsearch:通过摄取管道加上嵌套向量对大型文档进行分块轻松地实现段落搜索

作者:VECTOR SEARCH

向量搜索是一种基于含义而不是精确或不精确的 token 匹配技术来搜索数据的强大方法。 然而,强大的向量搜索的文本嵌入模型只能按几个句子的顺序处理短文本段落,而不是可以处理任意大量文本的基于 BM25 的技术。 现在,Elasticsearch 可以将大型文档与向量搜索无缝结合。

简单地说,它是如何在发挥作用的呢?

Elasticsearch 功能(例如摄取管道、脚本处理器的灵活性以及对使用密集向量的嵌套文档的新支持)的组合允许以一种简单的方式在摄取时将大文档分成足够小的段落,然后由文本嵌入模型处理这些段落以生成表示大型文档的全部含义所需的所有向量。

像平常一样摄取文档数据,并向摄取管道添加一个脚本处理器,将大型文本数据分解为句子数组或其他类型的块,然后添加一个 for_each 处理器,对每个块运行推理处理器。 索引的映射被定义为使得块数组被设置为嵌套对象,其中以密集向量映射作为子对象,然后该子对象将正确索引每个向量并使它们可搜索。

让我们详细了解一下:

加载文本嵌入模型

你需要的第一件事是一个模型,用于从块中创建文本嵌入,你可以使用任何你想要的东西,但此示例将在 all-distilroberta-v1 模型上端到端运行。 创建 Elastic Cloud 集群或准备另一个 Elasticsearch 集群后,我们可以使用 eland 库上传文本嵌入模型。

ini 复制代码
1.  MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
2.  ELASTIC_PASSWORD = "YOURPASSWORD"
3.  CLOUD_ID = "YOURCLOUDID"

5.  eland_import_hub_model \
6.      --cloud-id $CLOUD_ID \
7.      --es-username elastic \
8.      --es-password $ELASTIC_PASSWORD \
9.      --hub-model-id $MODEL_ID \
10.      --task-type text_embedding \
11.      --start

映射示例

下一步是准备映射以处理将在摄取管道期间创建的句子数组和向量对象。 对于这个特定的文本嵌入模型,维度为 384,dot_product 相似度将用于最近邻计算:

json 复制代码
1.  PUT chunker
2.  {
3.    "mappings": {
4.      "dynamic": "true",
5.      "properties": {
6.        "passages": {
7.          "type": "nested",
8.          "properties": {
9.            "vector": {
10.              "properties": {
11.                "predicted_value": {
12.                  "type": "dense_vector",
13.                  "index": true,
14.                  "dims": 384,
15.                  "similarity": "dot_product"
16.                }
17.              }
18.            }
19.          }
20.        }
21.      }
22.    }
23.  }

摄取管道示例

最后的准备步骤是定义一个摄取管道,将 body_content 字段分解为存储在 passages 字段中的文本块。 该管道有两个处理器,第一个脚本处理器将 body_content 字段分解为通过正则表达式存储在 passages 字段中的句子数组。 要进一步研究,请阅读正则表达式的高级功能,例如负向后查找和正向后查找,以了解它如何尝试在句子边界上正确分割,而不是在 Mr. 或 Mrs. 或 Ms. 上分割,并保留句子中的标点符号。 只要总字符串长度低于传递给脚本的参数,它还会尝试将句子块连接在一起。 接下来每个处理器通过推理处理器在每个句子上运行文本嵌入模型:

xml 复制代码
1.  PUT _ingest/pipeline/chunker
2.  {
3.    "processors": [
4.      {
5.        "script": {
6.          "description": "Chunk body_content into sentences by looking for . followed by a space",
7.          "lang": "painless",
8.          "source": """
9.            String[] envSplit = /((?<!M(r|s|rs)\.)(?<=\.) |(?<=\!) |(?<=\?) )/.split(ctx['body_content']);
10.            ctx['passages'] = new ArrayList();
11.            int i = 0;
12.            boolean remaining = true;
13.            if (envSplit.length == 0) {
14.              return
15.            } else if (envSplit.length == 1) {
16.              Map passage = ['text': envSplit[0]];ctx['passages'].add(passage)
17.            } else {
18.              while (remaining) {
19.                Map passage = ['text': envSplit[i++]];
20.                while (i < envSplit.length && passage.text.length() + envSplit[i].length() < params.model_limit) {passage.text = passage.text + ' ' + envSplit[i++]}
21.                if (i == envSplit.length) {remaining = false}
22.                ctx['passages'].add(passage)
23.              }
24.            }
25.            """,
26.          "params": {
27.            "model_limit": 400
28.          }
29.        }
30.      },
31.      {
32.        "foreach": {
33.          "field": "passages",
34.          "processor": {
35.            "inference": {
36.              "field_map": {
37.                "_ingest._value.text": "text_field"
38.              },
39.              "model_id": "sentence-transformers__all-minilm-l6-v2",
40.              "target_field": "_ingest._value.vector",
41.              "on_failure": [
42.                {
43.                  "append": {
44.                    "field": "_source._ingest.inference_errors",
45.                    "value": [
46.                      {
47.                        "message": "Processor 'inference' in pipeline 'ml-inference-title-vector' failed with message '{{ _ingest.on_failure_message }}'",
48.                        "pipeline": "ml-inference-title-vector",
49.                        "timestamp": "{{{ _ingest.timestamp }}}"
50.                      }
51.                    ]
52.                  }
53.                }
54.              ]
55.            }
56.          }
57.        }
58.      }
59.    ]
60.  }

添加一些文档

现在我们可以在 body_content 中添加包含大量文本的文档,并自动将它们分块,并将每个块文本通过模型嵌入到向量中:

less 复制代码
1.  PUT chunker/_doc/1?pipeline=chunker
2.  {
3.  "title": "Adding passage vector search to Lucene",
4.  "body_content": "Vector search is a powerful tool in the information retrieval tool box. Using vectors alongside lexical search like BM25 is quickly becoming commonplace. But there are still a few pain points within vector search that need to be addressed. A major one is text embedding models and handling larger text input. Where lexical search like BM25 is already designed for long documents, text embedding models are not. All embedding models have limitations on the number of tokens they can embed. So, for longer text input it must be chunked into passages shorter than the model's limit. Now instead of having one document with all its metadata, you have multiple passages and embeddings. And if you want to preserve your metadata, it must be added to every new document. A way to address this is with Lucene's "join" functionality. This is an integral part of Elasticsearch's nested field type. It makes it possible to have a top-level document with multiple nested documents, allowing you to search over nested documents and join back against their parent documents. This sounds perfect for multiple passages and vectors belonging to a single top-level document! This is all awesome! But, wait, Elasticsearch® doesn't support vectors in nested fields. Why not, and what needs to change? The key issue is how Lucene can join back to the parent documents when searching child vector passages. Like with kNN pre-filtering versus post-filtering, when the joining occurs determines the result quality and quantity. If a user searches for the top four nearest parent documents (not passages) to a query vector, they usually expect four documents. But what if they are searching over child vector passages and all four of the nearest vectors are from the same parent document? This would end up returning just one parent document, which would be surprising. This same kind of issue occurs with post-filtering."
5.  }

7.  PUT chunker/_doc/3?pipeline=chunker
8.  {
9.  "title": "Automatic Byte Quantization in Lucene",
10.  "body_content": "While HNSW is a powerful and flexible way to store and search vectors, it does require a significant amount of memory to run quickly. For example, querying 1MM float32 vectors of 768 dimensions requires roughly 1,000,000∗4∗(768+12)=3120000000≈31,000,000∗4∗(768+12)=3120000000bytes≈3GB of ram. Once you start searching a significant number of vectors, this gets expensive. One way to use around 75% less memory is through byte quantization. Lucene and consequently Elasticsearch has supported indexing byte vectors for some time, but building these vectors has been the user's responsibility. This is about to change, as we have introduced int8 scalar quantization in Lucene. All quantization techniques are considered lossy transformations of the raw data. Meaning some information is lost for the sake of space. For an in depth explanation of scalar quantization, see: Scalar Quantization 101. At a high level, scalar quantization is a lossy compression technique. Some simple math gives significant space savings with very little impact on recall. Those used to working with Elasticsearch may be familiar with these concepts already, but here is a quick overview of the distribution of documents for search. Each Elasticsearch index is composed of multiple shards. While each shard can only be assigned to a single node, multiple shards per index gives you compute parallelism across nodes. Each shard is composed as a single Lucene Index. A Lucene index consists of multiple read-only segments. During indexing, documents are buffered and periodically flushed into a read-only segment. When certain conditions are met, these segments can be merged in the background into a larger segment. All of this is configurable and has its own set of complexities. But, when we talk about segments and merging, we are talking about read-only Lucene segments and the automatic periodic merging of these segments. Here is a deeper dive into segment merging and design decisions."
11.  }

13.  PUT chunker/_doc/2?pipeline=chunker
14.  {
15.  "title": "Use a Japanese language NLP model in Elasticsearch to enable semantic searches",
16.  "body_content": "Quickly finding necessary documents from among the large volume of internal documents and product information generated every day is an extremely important task in both work and daily life. However, if there is a high volume of documents to search through, it can be a time-consuming process even for computers to re-read all of the documents in real time and find the target file. That is what led to the appearance of Elasticsearch® and other search engine software. When a search engine is used, search index data is first created so that key search terms included in documents can be used to quickly find those documents. However, even if the user has a general idea of what type of information they are searching for, they may not be able to recall a suitable keyword or they may search for another expression that has the same meaning. Elasticsearch enables synonyms and similar terms to be defined to handle such situations, but in some cases it can be difficult to simply use a correspondence table to convert a search query into a more suitable one. To address this need, Elasticsearch 8.0 released the vector search feature, which searches by the semantic content of a phrase. Alongside that, we also have a blog series on how to use Elasticsearch to perform vector searches and other NLP tasks. However, up through the 8.8 release, it was not able to correctly analyze text in languages other than English. With the 8.9 release, Elastic added functionality for properly analyzing Japanese in text analysis processing. This functionality enables Elasticsearch to perform semantic searches like vector search on Japanese text, as well as natural language processing tasks such as sentiment analysis in Japanese. In this article, we will provide specific step-by-step instructions on how to use these features."
17.  }

19.  PUT chunker/_doc/5?pipeline=chunker
20.  {
21.  "title": "We can chunk whatever we want now basically to the limits of a document ingest",
22.  "body_content": """Chonk is an internet slang term used to describe overweight cats that grew popular in the late summer of 2018 after a photoshopped chart of cat body-fat indexes renamed the "Chonk" scale grew popular on Twitter and Reddit. Additionally, "Oh Lawd He Comin'," the final level of the Chonk Chart, was adopted as an online catchphrase used to describe large objects, animals or people. It is not to be confused with the Saturday Night Live sketch of the same name. The term "Chonk" was popularized in a photoshopped edit of a chart illustrating cat body-fat indexes and the risk of health problems for each class (original chart shown below). The first known post of the "Chonk" photoshop, which classifies each cat to a certain level of "chonk"-ness ranging from "A fine boi" to "OH LAWD HE COMIN," was posted to Facebook group THIS CAT IS C H O N K Y on August 2nd, 2018 by Emilie Chang (shown below). The chart surged in popularity after it was tweeted by @dreamlandtea[1] on August 10th, 2018, gaining over 37,000 retweets and 94,000 likes (shown below). After the chart was posted there, it began growing popular on Reddit. It was reposted to /r/Delighfullychubby[2] on August 13th, 2018, and /r/fatcats on August 16th.[3] Additionally, cats were shared with variations on the phrase "Chonk." In @dreamlandtea's Twitter thread, she rated several cats on the Chonk scale (example, shown below, left). On /r/tumblr, a screenshot of a post featuring a "good luck cat" titled "Lucky Chonk" gained over 27,000 points (shown below, right). The popularity of the phrase led to the creation of a subreddit, /r/chonkers,[4] that gained nearly 400 subscribers in less than a month. Some photoshops of the chonk chart also spread on Reddit. For example, an edit showing various versions of Pikachu on the chart posted to /r/me_irl gained over 1,200 points (shown below, left). The chart gained further popularity when it was posted to /r/pics[5] September 29th, 2018."""
23.  }

搜索那些文档

要搜索数据并返回与查询最匹配的块,你可以使用 inner_hits 和 knn 子句,以在查询的命中输出中返回文档的最佳匹配块:

bash 复制代码
1.  GET chunker/_search
2.  {
3.    "_source": false,
4.    "fields": [
5.      "title"
6.    ],
7.    "knn": {
8.      "inner_hits": {
9.        "_source": false,
10.        "fields": [
11.          "passages.text"
12.        ]
13.      },
14.      "field": "passages.vector.predicted_value",
15.      "k": 1,
16.      "num_candidates": 100,
17.      "query_vector_builder": {
18.        "text_embedding": {
19.          "model_id": "sentence-transformers__all-minilm-l6-v2",
20.          "model_text": "Can I use multiple vectors per document now?"
21.        }
22.      }
23.    }
24.  }

将返回最佳文档和较大文档文本的相关部分:

json 复制代码
1.  {
2.    "took": 4,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 1,
13.        "relation": "eq"
14.      },
15.      "max_score": 0.75261426,
16.      "hits": [
17.        {
18.          "_index": "chunker",
19.          "_id": "1",
20.          "_score": 0.75261426,
21.          "_ignored": [
22.            "body_content.keyword",
23.            "passages.text.keyword"
24.          ],
25.          "fields": {
26.            "title": [
27.              "Adding passage vector search to Lucene"
28.            ]
29.          },
30.          "inner_hits": {
31.            "passages": {
32.              "hits": {
33.                "total": {
34.                  "value": 1,
35.                  "relation": "eq"
36.                },
37.                "max_score": 0.75261426,
38.                "hits": [
39.                  {
40.                    "_index": "chunker",
41.                    "_id": "1",
42.                    "_nested": {
43.                      "field": "passages",
44.                      "offset": 3
45.                    },
46.                    "_score": 0.75261426,
47.                    "fields": {
48.                      "passages": [
49.                        {
50.                          "text": [
51.                            "This sounds perfect for multiple passages and vectors belonging to a single top-level document! This is all awesome! But, wait, Elasticsearch® doesn't support vectors in nested fields. Why not, and what needs to change? The key issue is how Lucene can join back to the parent documents when searching child vector passages."
52.                          ]
53.                        }
54.                      ]
55.                    }
56.                  }
57.                ]
58.              }
59.            }
60.          }
61.        }
62.      ]
63.    }
64.  }

回顾一下

这里使用的方法展示了利用 Elasticsearch 的不同功能来解决更大问题的力量。

摄取管道允许你在索引之前对文档进行预处理,虽然有许多处理器可以执行特定的目标任务,但有时你需要脚本语言的强大功能才能执行诸如将文本分解为句子数组之类的操作。 因为你可以在对文档建立索引之前访问该文档,所以只要所有信息都在文档本身中,你就可以以几乎任何你能想象到的方式重新创建数据。 foreach 处理器允许我们包装一些可能运行零到 N 次的东西,而无需事先知道它需要执行多少次。 在本例中,我们使用它来运行与提取的句子一样多的句子,以运行推断处理器来生成向量。

索引的映射准备好处理原始文档中不存在的文本和向量的现在的对象数组,并使用嵌套对象来索引数据,以便我们可以正确搜索文档。

使用带有向量嵌套支持的 knn 允许使用 inner_hits 来呈现文档的最佳评分部分,这可以替代通常通过在 BM25 查询中使用 highlight 显示来完成的操作。

原文:Chunking Large Documents via Ingest pipelines plus nested vectors equals easy passage search --- Elastic Search Labs

相关推荐
Elastic 中国社区官方博客1 小时前
如何将数据从 AWS S3 导入到 Elastic Cloud - 第 3 部分:Elastic S3 连接器
大数据·elasticsearch·搜索引擎·云计算·全文检索·可用性测试·aws
掘金-我是哪吒1 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
研究是为了理解3 小时前
Git Bash 常用命令
git·elasticsearch·bash
晨欣6 小时前
Elasticsearch和Lucene之间是什么关系?(ChatGPT回答)
elasticsearch·chatgpt·lucene
筱源源12 小时前
Elasticsearch-linux环境部署
linux·elasticsearch
Elastic 中国社区官方博客1 天前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理
Shenqi Lotus1 天前
ELK-ELK基本概念_ElasticSearch的配置
elk·elasticsearch
yeye198912241 天前
10-Query & Filtering 与多字符串多字段查询
elasticsearch
Narutolxy1 天前
精准优化Elasticsearch:磁盘空间管理与性能提升技巧20241106
大数据·elasticsearch·jenkins
谢小涛2 天前
ES管理工具Cerebro 0.8.5 Windows版本安装及启动
elasticsearch·es·cerebro