在我之前的文章 “Elasticsearch:使用 ELSER 進行語義搜索”,我們展示了如何使用 ELESR v1 來進行語義搜索。在使用 ELSER 之前,我們必須注意的是:
重要:雖然 ELSER V2 已正式發(fā)布,但 ELSER V1 仍處于 [預(yù)覽] 狀態(tài)。此功能處于技術(shù)預(yù)覽階段,可能會在未來版本中更改或刪除。 Elastic 將努力解決任何問題,但技術(shù)預(yù)覽版中的功能不受官方 GA 功能的支持 SLA 的約束,并將保留在技術(shù)預(yù)覽版中。
也就是說 ELSER v1 不建議在生產(chǎn)環(huán)境中使用。在生產(chǎn)的環(huán)境中,我們建議使用 ELSER v2。由于兩個版本中的使用方法稍有不同。在今天的文章中,我們以 ELSER v2 為例來進行展示。
簡介
Elastic Learned Sparse EncodeR(或 ELSER)是由 Elastic 訓練的 NLP 模型,使你能夠使用稀疏向量表示來執(zhí)行語義搜索。 語義搜索不是根據(jù)搜索詞進行字面匹配,而是根據(jù)搜索查詢的意圖和上下文含義來檢索結(jié)果。
本教程中的說明向你展示如何使用 ELSER 對數(shù)據(jù)執(zhí)行語義搜索。
注意:在使用 ELSER 進行語義搜索期間,僅考慮每個字段提取的前 512 個標記。 請參閱此頁面了解更多信息。
要求
要使用 ELSER 執(zhí)行語義搜索,你必須在集群中部署 NLP 模型。 請參閱 ELSER 文檔以了解如何下載和部署模型。
注意:如果關(guān)閉部署自動擴展,則 Elasticsearch Service 中用于部署和使用 ELSER 模型的最小專用 ML 節(jié)點大小為 4 GB。 建議打開自動擴展功能,因為它允許你的部署根據(jù)需求動態(tài)調(diào)整資源。 通過使用更多分配或每次分配更多線程可以實現(xiàn)更好的性能,這需要更大的 ML 節(jié)點。 自動擴展可在需要時提供更大的節(jié)點。 如果關(guān)閉自動擴展,你必須自己提供適當大小的節(jié)點。
創(chuàng)建索引映射
首先,必須創(chuàng)建目標索引的映射 - 包含模型根據(jù)你的文本創(chuàng)建的 token 的索引。 目標索引必須具有?sparse_vector?或?rank_features?字段類型的字段才能對 ELSER 輸出進行索引。
注意:ELSER 輸出必須攝取到具有 sparse_vector 或? rank_features 字段類型的字段中。 否則,Elasticsearch 會將 token 權(quán)重對解釋為文檔中的大量字段。 如果你收到類似于 “Limit of total fields [1000] has been exceeded while adding new fields” 的錯誤,則 ELSER 輸出字段未正確映射,并且其字段類型不同于 sparse_vector 或 rank_features。
PUT my-index
{
"mappings": {
"properties": {
"content_embedding": {
"type": "sparse_vector"
},
"content": {
"type": "text"
}
}
}
}
- content_embedding 包含生成的 token 的字段的名稱。 必須在下一步的推理管道配置中引用它。
- sparse_vector 定義字段是 sparse_vector 字段。
- 用于創(chuàng)建稀疏向量表示的字段的名稱。 在此示例中,字段的名稱是 content。 必須在下一步的推理管道配置中引用它。
- text 定義字段類型為文本。
使用推理處理器創(chuàng)建攝取管道
創(chuàng)建帶有 inference processor 的 ingest pipeline,以使用 ELSER 來推理管道中攝取的數(shù)據(jù)。
PUT _ingest/pipeline/elser-v2-test
{
"processors": [
{
"inference": {
"model_id": ".elser_model_2",
"input_output": [
{
"input_field": "content",
"output_field": "content_embedding"
}
]
}
}
]
}
- input_output:配置對象,定義推理過程的輸入字段和包含推理結(jié)果的輸出字段。
加載數(shù)據(jù)
在此步驟中,你將加載稍后在推理攝取管道中使用的數(shù)據(jù),以從中提取 token。
使用 msmarco-passagetest2019-top1000 數(shù)據(jù)集,該數(shù)據(jù)集是 MS MARCO Passage Ranking 數(shù)據(jù)集的子集。 它由 200 個查詢組成,每個查詢都附有相關(guān)文本段落的列表。 所有獨特的段落及其 ID 均已從該數(shù)據(jù)集中提取并編譯到 tsv 文件中。
下載文件并使用機器學習 UI 中的數(shù)據(jù)可視化工具將其上傳到集群。 將名稱 id 分配給第一列,將內(nèi)容分配給第二列。 索引名稱是 test-data。 上傳完成后,你可以看到名為 test-data 的索引,其中包含 182469 個文檔。
關(guān)于如何加載這個數(shù)據(jù),請詳細閱讀文章 “Elasticsearch:如何部署 NLP:文本嵌入和向量搜索”。
我們可以在 Discover 中進行查看:
通過推理攝取管道攝取數(shù)據(jù)
通過使用 ELSER 作為推理模型的推理管道重新索引數(shù)據(jù),從文本創(chuàng)建標記。
POST _reindex?wait_for_completion=false
{
"source": {
"index": "test-data",
"size": 50
},
"dest": {
"index": "my-index",
"pipeline": "elser-v2-test"
}
}
- Reindex 的默認批量大小為 1000。將大小減小到較小的數(shù)字可以使重新索引過程的更新更快,使你能夠密切跟蹤進度并盡早發(fā)現(xiàn)錯誤。
該調(diào)用返回一個任務(wù) ID 以監(jiān)控進度:
我們等到 completed 的狀態(tài)變?yōu)?true:
你還可以打開訓練模型 UI,選擇 ELSER 下的 Pipelines 選項卡來跟蹤進度。
使用 text_expansion 查詢進行語義搜索
要執(zhí)行語義搜索,請使用 text_expansion 查詢,并提供查詢文本和 ELSER 模型 ID。 下面的示例使用查詢文本 “How to avoid muscle soreness after running?”,content_embedding 字段包含生成的 ELSER 輸出:
GET my-index/_search
{
"_source": false,
"fields": [
"content"
],
"query":{
"text_expansion":{
"content_embedding":{
"model_id":".elser_model_2",
"model_text":"How to avoid muscle soreness after running?"
}
}
}
}
結(jié)果是 my-index 索引中按相關(guān)性排序的與你的查詢文本含義最接近的前 10 個文檔。 結(jié)果還包含為每個相關(guān)搜索結(jié)果提取的 token 及其權(quán)重。 標記是捕獲相關(guān)性的學習關(guān)聯(lián),它們不是同義詞。 要了解有關(guān) token 是什么的更多信息,請參閱此頁面。 可以從源中排除 token,請參閱下面的章節(jié)以了解更多信息。
{
"took": 1531,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 25.669455,
"hits": [
{
"_index": "my-index",
"_id": "fd96yo0BZ-5LgFdRMD7b",
"_score": 25.669455,
"fields": {
"content": [
"There are a few foods and food groups that will help to fight inflammation and delayed onset muscle soreness (both things that are inevitable after a long, hard workout) when you incorporate them into your postworkout eats, whether immediately after your run or at a meal later in the day. Advertisement. Advertisement."
]
}
},
{
"_index": "my-index",
"_id": "nt96yo0BZ-5LgFdRLQCy",
"_score": 23.388044,
"fields": {
"content": [
"What so many out there do not realize is the importance of what you do after you work out. You may have done the majority of the work, but how you treat your body in the minutes and hours after you exercise has a direct effect on muscle soreness, muscle strength and growth, and staying hydrated. Cool Down. After your last exercise, your workout is not over. The first thing you need to do is cool down. Even if running was all that you did, you still should do light cardio for a few minutes. This brings your heart rate down at a slow and steady pace, which helps you avoid feeling sick after a workout."
]
}
},
{
"_index": "my-index",
"_id": "ot96yo0BZ-5LgFdRLzD0",
"_score": 22.550915,
"fields": {
"content": [
"If you’ve been exercising more, you may be suffering from the aches and pains of having overdone it at the gym. I’ve been there. Making sure your workout is challenging without overdoing it is one way to prevent muscle soreness. But research also points to some foods and beverages that can help ward off and minimize exercise-related muscle soreness, which we’ve reported on in EatingWell Magazine. Related: Foods to Eat to Improve Your Workout Post-Workout Breakfast Recipes What to Drink Before, During and After You Exercise. Blueberries."
]
}
}
...
將語義搜索與其他查詢相結(jié)合
你可以將 text_expansion 與復合查詢中的其他查詢結(jié)合起來。 例如,在布爾查詢或全文查詢中使用過濾子句,該查詢可能會或可能不會使用與 text_expansion 查詢相同的查詢文本。 這使你能夠合并兩個查詢的搜索結(jié)果。
text_expansion 查詢的搜索命中得分往往高于其他 Elasticsearch 查詢。 可以通過使用 boost 參數(shù)增加或減少每個查詢的相關(guān)性分數(shù)來對這些分數(shù)進行正則化。 當存在不太相關(guān)的結(jié)果時,text_expansion 查詢的召回率可能很高。 使用 min_score 參數(shù)來修剪那些不太相關(guān)的文檔。
GET my-index/_search
{
"query": {
"bool": {
"should": [
{
"text_expansion": {
"content_embedding": {
"model_text": "How to avoid muscle soreness after running?",
"model_id": ".elser_model_2",
"boost": 1
}
}
},
{
"query_string": {
"query": "toxins",
"boost": 4
}
}
]
}
},
"min_score": 10
}
- text_expansion 和 query_string 查詢都位于 bool 查詢的 should 子句中。
- text_expansion 查詢的 boost 值為 1,這是默認值。 這意味著該查詢結(jié)果的相關(guān)性得分不會提高。
- query_string 查詢的提升值為 4。 該查詢結(jié)果的相關(guān)性得分增加,導致它們在搜索結(jié)果中排名更高。
- 僅顯示分數(shù)等于或高于 10 的結(jié)果。
優(yōu)化性能
通過從文檔源中排除 ELSER token 來節(jié)省磁盤空間
必須對 ELSER 生成的 token 進行索引,以便在 text_expansion 查詢中使用。 但是,沒有必要在文檔源中保留這些術(shù)語。 你可以通過使用 source exclude 映射從文檔源中刪除 ELSER 術(shù)語來節(jié)省磁盤空間。
警告:重新索引使用文檔源來填充目標索引。 一旦 ELSER 術(shù)語從源中排除,就無法通過重新索引來恢復它們。 從源中排除 token 是一種節(jié)省空間的優(yōu)化,只有在你確定將來不需要重新索引時才應(yīng)應(yīng)用該優(yōu)化! 仔細考慮這種權(quán)衡并確保從源中排除 ELSER 術(shù)語符合你的特定要求和用例,這一點很重要。 仔細查看禁用 _source 字段和從 _source 中包含/排除字段部分,以詳細了解從 _source 中排除 token 可能產(chǎn)生的后果。
可以通過以下 API 調(diào)用創(chuàng)建從 _source 字段中排除 content_embedding 的映射:
PUT my-index
{
"mappings": {
"_source": {
"excludes": [
"content_embedding"
]
},
"properties": {
"content_embedding": {
"type": "sparse_vector"
},
"content": {
"type": "text"
}
}
}
}
注意:根據(jù)你的數(shù)據(jù),使用 track_total_hits: false 時文本擴展查詢可能會更快。文章來源:http://www.zghlxwxcb.cn/news/detail-835996.html
更多閱讀:Elasticsearch:使用 ELSER v2 文本擴展進行語義搜索文章來源地址http://www.zghlxwxcb.cn/news/detail-835996.html
到了這里,關(guān)于Elasticsearch:使用 ELSER v2 進行語義搜索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!