Elasticsearch:使用 ELSER v2 文本扩展进行语义搜索

Elastic 提供了一个强大的 ELSER 供我们进行语义搜索。ELSER 是一种稀疏向量的搜索方法。我们无需对它做任何的微调及训练。它是一种 out-of-domain 的模型。目前它仅对英文进行支持。希望将来它能对其它的语言支持的更好。更多关于 ELSER 的知识,请参阅文章 “Elasticsearch:使用 ELSER 释放语义搜索的力量:Elastic Learned Sparse EncoderR”。在本文中,我们将使用第二版的  ELSER 来进行语义搜索。我将使用 Jupyter notebook 演示如何使用 ELSER 模型 .elser_model_2 模型,该模型提供了更高的检索精度。

如果你已使用 ELSER 模型 .elser_model_1 设置索引,并且想要升级到 ELSER v2 模型 - .elser_model_2,请按照文章升级索引以使用 elser 模型的说明进行操作 来进行升级。

 安装

如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考文章:

安装 Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,那么请参考一下的文章来进行安装:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch

  • Kibana:如何在 Linux,MacOS 及 Windows 上安装 Elastic 栈中的 Kibana

在安装的时候,请选择 Elastic Stack 8.x 进行安装。在安装的时候,我们可以看到如下的安装信息:

​​

为了能够上传向量模型,我们必须订阅白金版或试用。

​​

​​

Python

我们需要安装相应的 Elasticsearch 包:

$ pwd
/Users/liuxg/python/elser
$ pip3 install elasticsearch -qU
$ pip3 list | grep elasticseach
elasticsearch             8.11.1
rag-elasticsearch         0.0.1        /Users/liuxg/python/rag-elasticsearch/my-app/packages/rag-elasticsearch

环境变量

在启动 Jupyter 之前,我们设置如下的环境变量:

export ES_USER="elastic"
export ES_PASSWORD="yarOjyX5CLqTsKVE3v*d"
export ES_ENDPOINT="localhost"

拷贝 Elasticsearch 证书

我们把 Elasticsearch 的证书拷贝到当前的目录下:

$ pwd
/Users/liuxg/python/elser
$ cp ~/elastic/elasticsearch-8.11.0/config/certs/http_ca.crt .
$ ls
 find_books_about_christmas_without_searching_for_christmas.ipynb
Chatbot with LangChain conversational chain and OpenAI.ipynb
ElasticKnnSearch.ipynb
ElasticVectorSearch.ipynb
ElasticsearchStore.ipynb
Mental Health FAQ.ipynb
Multilingual semantic search.ipynb
NLP text search using hugging face transformer model.ipynb
Question Answering with Langchain and OpenAI.ipynb
RAG-langchain-elasticsearch.ipynb
Semantic search - ELSER.ipynb
Semantic search quick start.ipynb
book_summaries_1000_chunked.json
books.json
data.json
http_ca.crt
lib
sample_data.json
upgrading-index-to-use-elser.ipynb
vector_search_implementation_guide_api.ipynb
workplace-docs.json

在上面,我们把  Elasticsearch 的证书 http_ca.crt 拷贝到当前的目录下。

运行应用

连接到 Elasticsearch

from elasticsearch import Elasticsearch
import os
 
elastic_user=os.getenv('ES_USER')
elastic_password=os.getenv('ES_PASSWORD')
elastic_endpoint=os.getenv("ES_ENDPOINT")
 
url = f"https://{elastic_user}:{elastic_password}@{elastic_endpoint}:9200"
es = Elasticsearch(url, ca_certs = "./http_ca.crt", verify_certs = True)
 
print(es.info())

从上面的输出中,我们可以看到,连接到 Elasticsearch 是成功的。

如果你对如何连接到 Elasticsearch 还不是很熟悉的话,那么请阅读文章 “Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”。

下载及部署 ELSER 模型

下面,我们来尝试通过软件的方式来针对 ELSER 进行手动部署。在此示例中,我们将下载 ELSER 模型并将其部署到 ML 节点中。 确保你有一个 ML 节点才能运行 ELSER 模型。如果你之前已经下载过,我们通过软件的方式来进行删除,并安装最新的模型:

# delete model if already downloaded and deployed
try:
  es.ml.delete_trained_model(model_id=".elser_model_2",force=True)
  print("Model deleted successfully, We will proceed with creating one")
except exceptions.NotFoundError:
  print("Model doesn't exist, but We will proceed with creating one")

# Creates the ELSER model configuration. Automatically downloads the model if it doesn't exist. 
es.ml.put_trained_model(
    model_id=".elser_model_2",
    input={
      "field_names": ["text_field"]
    }
  )

我们回到 Kibana 的界面中进行查看:

上面显示, .elser_model_2 正在被下载。我们需要等一段时间才能下载完毕。这个依赖于你自己的网路速度。使用以下命令检查模型下载的状态。

while True:
    status = es.ml.get_trained_models(
        model_id=".elser_model_2",
        include="definition_status"
    )
    
    if (status["trained_model_configs"][0]["fully_defined"]):
        print("ELSER Model is downloaded and ready to be deployed.")
        break
    else:
        print("ELSER Model is downloaded but not ready to be deployed.")
    time.sleep(5)

在 Kibana 中显示的状态为:

下载完模型后,我们可以将模型部署到 ML 节点中。 使用以下命令部署模型。

import time

# Start trained model deployment if not already deployed
es.ml.start_trained_model_deployment(
  model_id=".elser_model_2",
  number_of_allocations=1,
  wait_for="starting"
)

如上所示,在 Kibana 的界面中,我们可以看到 .elser_model_2 已经被成功地部署了。我们可以使用如下的代码来查看状态:

while True:
  status = es.ml.get_trained_models_stats(
    model_id=".elser_model_2",
  )
  if (status["trained_model_stats"][0]["deployment_stats"]["state"] == "started"):
    print("ELSER Model has been successfully deployed.")
    break
  else:
    print("ELSER Model is currently being deployed.")
  time.sleep(5)

摄入一些文档到 Elasticsearch

为了在我们的 Elasticsearch 中使用 ELSER,我们需要创建一个包含运行 ELSER 模型的推理处理器的摄取管道。 让我们使用 put_pipeline 方法添加该管道。

es.ingest.put_pipeline(
    id="elser-ingest-pipeline", 
    description="Ingest pipeline for ELSER",
    processors=[
    {
      "inference": {
        "model_id": ".elser_model_2",
        "input_output": [
            {
              "input_field": "plot",
              "output_field": "plot_embedding"
            }
          ]
      }
    }
  ]
)

让我们记下该 API 调用中的一些重要参数:

  • inference:使用机器学习模型执行推理的处理器。
  • model_id:指定要使用的机器学习模型的 ID。 在此示例中,模型 ID 设置为 .elser_model_2。
  • input_output:指定输入和输出字段
  • input_field:创建稀疏向量表示的字段名称。
  • output_field:包含推理结果的字段名称。

创建索引

要在索引时使用 ELSER 模型,我们需要创建支持 text_expansion 查询的索引映射。 该映射包括一个 sparse_vector 类型的字段,用于处理我们感兴趣的特征向量。 该字段包含 ELSER 模型根据输入文本创建的 token-weight 对。

让我们使用我们需要的映射创建一个名为 elser-example-movies 的索引。

es.indices.delete(index="elser-example-movies", ignore_unavailable=True)
es.indices.create(
  index="elser-example-movies",
  settings={
      "index": {
          "default_pipeline": "elser-ingest-pipeline"
      }
  },
  mappings={
    "properties": {
      "plot": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "plot_embedding": { 
        "type": "sparse_vector" 
      }
    }
  }
)

摄入文档

让我们插入 12 部电影的示例数据集。

如果出现错误,请检查模型是否已部署并且在 ML 节点中可用。 在较新版本的 Elastic Cloud 中,ML 节点是自动缩放的,并且 ML 节点可能尚未准备好。 等待几分钟,然后重试。在进行下面的运行之前,我们先在项目的根目录下创建如下的一个 movies.json 文档:

movies.json

[
    {
    "title": "Pulp Fiction",
    "runtime": "154",
    "plot": "The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.",
    "keyScene": "John Travolta is forced to inject adrenaline directly into Uma Thurman's heart after she overdoses on heroin.",
    "genre": "Crime, Drama",
    "released": "1994"
    },
    {
    "title": "The Dark Knight",
    "runtime": "152",
    "plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.",
    "keyScene": "Batman angrily responds 'I’m Batman' when asked who he is by Falcone.",
    "genre": "Action, Crime, Drama, Thriller",
    "released": "2008"
    },
    {
    "title": "Fight Club",
    "runtime": "139",
    "plot": "An insomniac office worker and a devil-may-care soapmaker form an underground fight club that evolves into something much, much more.",
    "keyScene": "Brad Pitt explains the rules of Fight Club to Edward Norton. The first rule of Fight Club is: You do not talk about Fight Club. The second rule of Fight Club is: You do not talk about Fight Club.",
    "genre": "Drama",
    "released": "1999"
    },
    {
    "title": "Inception",
    "runtime": "148",
    "plot": "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into thed of a C.E.O.",
    "keyScene": "Leonardo DiCaprio explains the concept of inception to Ellen Page by using a child's spinning top.",
    "genre": "Action, Adventure, Sci-Fi, Thriller",
    "released": "2010"
    },
    {
    "title": "The Matrix",
    "runtime": "136",
    "plot": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",
    "keyScene": "Red pill or blue pill? Morpheus offers Neo a choice between the red pill, which will allow him to learn the truth about the Matrix, or the blue pill, which will return him to his former life.",
    "genre": "Action, Sci-Fi",
    "released": "1999"
    },
    {
    "title": "The Shawshank Redemption",
    "runtime": "142",
    "plot": "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.",
    "keyScene": "Andy Dufresne escapes from Shawshank prison by crawling through a sewer pipe.",
    "genre": "Drama",
    "released": "1994"
    },
    {
    "title": "Goodfellas",
    "runtime": "146",
    "plot": "The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.",
    "keyScene": "Joe Pesci's character Tommy DeVito shoots young Spider in the foot for not getting him a drink.",
    "genre": "Biography, Crime, Drama",
    "released": "1990"
    },
    {
    "title": "Se7en",
    "runtime": "127",
    "plot": "Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as his motives.",
    "keyScene": "Brad Pitt's character David Mills shoots John Doe after he reveals that he murdered Mills' wife.",
    "genre": "Crime, Drama, Mystery, Thriller",
    "released": "1995"
    },
    {
    "title": "The Silence of the Lambs",
    "runtime": "118",
    "plot": "A young F.B.I. cadet must receive the help of an incarcerated and manipulative cannibal killer to help catch another serial killer, a madman who skins his victims.",
    "keyScene": "Hannibal Lecter explains to Clarice Starling that he ate a census taker's liver with some fava beans and a nice Chianti.",
    "genre": "Crime, Drama, Thriller",
    "released": "1991"
    },
    {
    "title": "The Godfather",
    "runtime": "175",
    "plot": "An organized crime dynasty's aging patriarch transfers control of his clandestine empire to his reluctant son.",
    "keyScene": "James Caan's character Sonny Corleone is shot to death at a toll booth by a number of machine gun toting enemies.",
    "genre": "Crime, Drama",
    "released": "1972"
    },
    {
    "title": "The Departed",
    "runtime": "151",
    "plot": "An undercover cop and a mole in the police attempt to identify each other while infiltrating an Irish gang in South Boston.",
    "keyScene": "Leonardo DiCaprio's character Billy Costigan is shot to death by Matt Damon's character Colin Sullivan.",
    "genre": "Crime, Drama, Thriller",
    "released": "2006"
    },
    {
    "title": "The Usual Suspects",
    "runtime": "106",
    "plot": "A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which began when five criminals met at a seemingly random police lineup.",
    "keyScene": "Kevin Spacey's character Verbal Kint is revealed to be the mastermind behind the crime, when his limp disappears as he walks away from the police station.",
    "genre": "Crime, Mystery, Thriller",
    "released": "1995"
    }
]
$ pwd
/Users/liuxg/python/elser
$ ls Install\ ELSER.ipynb 
Install ELSER.ipynb
$ ls movies.json 
movies.json
import json
from elasticsearch import helpers
 
with open('movies.json') as f:
   data_json = json.load(f)
 
# Prepare the documents to be indexed
documents = []
for doc in data_json:
    documents.append({
        "_index": "elser-example-movies",
        "_source": doc,
    })
 
# Use helpers.bulk to index
helpers.bulk(es, documents)
 
print("Done indexing documents into `elser-example-movies` index!")
time.sleep(3)

检查新文档以确认它现在有一个 plot_embedding 字段,其中包含新的附加术语列表。 这些术语是创建管道时在 input_field 中用于 ELSER 推理的目标字段的文本扩展。 ELSER 实质上创建了一个扩展术语树,以提高文档的语义可搜索性。 我们将能够使用 text_expansion 查询来搜索这些文档。我们可以在 Kibana 中查看:

但首先让我们从简单的关键字搜索开始,看看 ELSER 如何提供开箱即用的语义相关结果。

搜索文档

response = es.search(
    index='elser-example-movies', 
    size=3,
    query={
        "text_expansion": {
            "plot_embedding": {
                "model_id":".elser_model_2",
                "model_text":"fighting movie"
            }
        }
    }
)

for hit in response['hits']['hits']:
    doc_id = hit['_id']
    score = hit['_score']
    title = hit['_source']['title']
    plot = hit['_source']['plot']
    print(f"Score: {score}\nTitle: {title}\nPlot: {plot}\n")

上面的所有源码可以在地址 https://github.com/liu-xiao-guo/semantic_search_es/blob/main/Install%20ELSER.ipynb 进行下载。

下一步

现在我们有了使用 ELSER 进行语义搜索的工作示例,你可以在自己的数据上尝试一下。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/278503.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

YOLOv8改进 | 细节创新篇 | iAFF迭代注意力特征融合助力多目标细节涨点

一、本文介绍 本文给大家带来的改进机制是iAFF(迭代注意力特征融合),其主要思想是通过改善特征融合过程来提高检测精度。传统的特征融合方法如加法或串联简单,未考虑到特定对象的融合适用性。iAFF通过引入多尺度通道注意力模块(我…

winserver2008 r2服务器iis配置支持flv,f4v,mp4格式视频

很多政府单位网站一直在使用WIN服务器,大部分网站都使用多年基本使用.NET或者CMS系统建站,系统环境也一直是老版本,今天在维护过程中又出现了新问题,上传的MP4文件不支持网站上播放,顺便也分享下解决过程。当我们架设的…

数字身份验证:跨境电商如何应对账户安全挑战?

在数字化时代,随着跨境电商的蓬勃发展,账户安全问题逐渐成为行业和消费者关注的焦点。随着网络犯罪日益猖獗,用户的数字身份安全面临着更加复杂的威胁。本文将深入探讨数字身份验证在跨境电商中的重要性,并探讨各种创新技术和策略…

回归预测 | MATLAB实ZOA-LSTM基于斑马优化算法优化长短期记忆神经网络的多输入单输出数据回归预测模型 (多指标,多图)

回归预测 | MATLAB实ZOA-LSTM基于斑马优化算法优化长短期记忆神经网络的多输入单输出数据回归预测模型 (多指标,多图) 目录 回归预测 | MATLAB实ZOA-LSTM基于斑马优化算法优化长短期记忆神经网络的多输入单输出数据回归预测模型 (…

C++系列-第1章顺序结构-4-整型int

C系列-第1章顺序结构-4-整型int 在线练习: http://noi.openjudge.cn/ https://www.luogu.com.cn/ 总结 本文是C系列博客,主要讲述整型int的用法 整型int 在C中,int 是一个关键字,用于声明整型变量。int 类型用于存储整数&…

PHP序列化总结2--常见的魔术方法

魔术方法的概念 PHP的魔术方法是一种特殊的方法,用于覆盖PHP的默认操作。它们以双下划线(__)开头,后面跟着一些特定的字符串,如__construct()、__destruct()、__get()等。这些魔术方法在对象执行特定操作时被自动调用…

三台CentOS7.6虚拟机搭建Hadoop完全分布式集群(三)

这个是笔者大学时期的大数据课程使用三台CentOS7.6虚拟机搭建完全分布式集群的案例,已成功搭建完全分布式集群,并测试跑实例。 9 安装hbase 温馨提示:安装hbase先在master主节点上配置,然后远程复制到slave01或slave02 &#xf…

win上使用wireshark 抓包 | 安装、实战抓包、筛选规则

先随便讲两句吧 win 上抓包,使用wireshark 直接运行,通过选定网卡、配置筛选规则 相比,在linux 上抓包,直接使用命令 tcpdump 再添加筛选规则 就可以 好像wireshark的一个插件不维护,导致需要重新安装插件,…

PBR纹理贴图类型详解

在线工具推荐: 3D数字孪生场景编辑器 - GLTF/GLB材质纹理编辑器 - 3D模型在线转换 - Three.js AI自动纹理开发包 - YOLO 虚幻合成数据生成器 - 三维模型预览图生成器 - 3D模型语义搜索引擎 PBR 纹理是一种帮助 3D 艺术家使他们的 3D 渲染看起来更逼真的技术。…

鸿鹄电子招投标系统:基于Spring Boot、Mybatis、Redis和Layui的企业电子招采平台源码与立项流程

在数字化时代,企业需要借助先进的数字化技术来提高工程管理效率和质量。招投标管理系统作为企业内部业务项目管理的重要应用平台,涵盖了门户管理、立项管理、采购项目管理、采购公告管理、考核管理、报表管理、评审管理、企业管理、采购管理和系统管理等…

学习笔记:R语言基础

文章目录 一、R语言简介二、选择R的原因三、R基本数据对象(一)向量(二)矩阵(三)数组(四)因子(五)列表(六)数据框(七&#…

Android笔记(二十三):Paging3分页加载库结合Compose的实现分层数据源访问

在Android笔记(二十二):Paging3分页加载库结合Compose的实现网络单一数据源访问一文中,实现了单一数据源的访问。在实际运行中,往往希望不是单纯地访问网络数据,更希望将访问的网络数据保存到移动终端的SQL…

分布式系统架构设计之分布式系统实践案例和未来展望

分布式系统在过去的几十年里经历了长足的发展,从最初的简单分布式架构到今天的微服务、云原生等先进架构,取得了丰硕的成果。本文将通过实际案例分享分布式系统的架构实践,并展望未来可能的发展方向。 一、实践案例 1、微服务化实践 背景 …

【neo4j】desktop下载

【neo4j】desktop下载 https://neo4j.com/download/ 点击download,填写表格 之后就可以正常使用了

Python+Yolov5+Qt交通标志特征识别窗体界面相片视频摄像头

程序示例精选 PythonYolov5Qt交通标志特征识别窗体界面相片视频摄像头 如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助! 前言 这篇博客针对《PythonYolov5Qt交通标志特征识别窗体界面相片视频摄像头》编写代码&a…

4.16 构建onnx结构模型-And

前言 构建onnx方式通常有两种: 1、通过代码转换成onnx结构,比如pytorch —> onnx 2、通过onnx 自定义结点,图,生成onnx结构 本文主要是简单学习和使用两种不同onnx结构, 下面以 And 结点进行分析 方式 方法一&…

GitHub Copilot 终极详细介绍

编写代码通常是一项乏味且耗时的任务。现代开发人员一直在寻找新的方法来提高编程的生产力、准确性和效率。 像 GitHub Copilot 这样的自动代码生成工具可以使这成为可能。 GitHub Copilot 到底是什么? GitHub Copilot 于 2021 年 10 月推出,是 GitHub 的…

【Azure 架构师学习笔记】- Azure Databricks (4) - 使用Azure Key Vault 管理ADB Secret

本文属于【Azure 架构师学习笔记】系列。 本文属于【Azure Databricks】系列。 接上文 【Azure 架构师学习笔记】- Azure Databricks (3) - 再次认识DataBricks 前言 Azure Databricks有access token,是具有ADB内部最高权限的token。在云环境中这些高级别权限的sec…

一体化、一站式!智能视频客服加码全媒体云呼叫中心能力

凭借对电话、短信、邮件、社交媒体、视频等数种沟通渠道强大的统一集成能力,全媒体云呼叫中心已跃升成为现代企业客户服务的核心工具,高效便捷地为企业提供客户服务。而随着消费者需求愈加多元化和个性化,传统的语音通话方式已无法满足部分消…

C语言实例_stdlib.h库函数功能及其用法详解

一、前言 C语言作为一种高效、灵活的编程语言,标准库的使用对于开发人员来说是不可或缺的。其中,stdlib.h是C语言中一个重要的标准库头文件,提供了许多常用的函数和工具,以便开发人员能够更加便捷地进行内存管理、字符串处理、随…