Elasticsearch:同义词在 RAG 中重要吗?

作者:来自 Elastic Jeffrey Rengifo 及 Tomás Murúa

探索 RAG 应用程序中 Elasticsearch 同义词的功能。

同义词允许我们使用具有相同含义的不同词语在文档中搜索,以确保用户无论使用什么确切的词语都能找到他们所寻找的内容。你可能会认为,由于 RAG 应用程序使用语义/向量搜索,同义词功能的一部分已经被同义词涵盖(因为根据定义,同义词是语义相关的词)。

这是真的吗?语义搜索真的能取代同义词吗?在本文中,我们将分析在 RAG 应用程序中使用同义词的影响。

步骤

  • 配置端点
  • 配置同义词
  • 索引文档
  • 语义搜索
  • 同义词和 RAG

配置推理端点

对于这个例子,我们将在 HR 环境中实现带有和不带有同义词的 RAG(Retrieval-Augmented Generation - 检索增强生成)系统。我们将使用术语 PTO(Paid Time Off - 带薪休假)的变体(如 “vacation” 或 “holiday”)为不同的文档编制索引。然后我们将配置同义词来展示这些关系如何提高搜索的相关性和准确性。

首先,让我们通过在 Kibana DevTools 中运行以下命令,使用带有推理 API(inference api) 的 ELSER 模型创建一个端点:

PUT _inference/sparse_embedding/code-wave_inference
{"service": "elasticsearch","service_settings": {"num_allocations": 1,"num_threads": 1}
}

配置同义词

Elasticsearch 中的同义词是什么?

在 Elasticsearch 中,同义词(synonyms)是具有相同或相似含义的单词或短语,存储为同义词集,可以作为文件或通过 API 进行管理。它们允许用户找到相关信息,即使他们使用不同的术语来指代同一概念。

因此,例如,如果我们创建一组同义词,其中 “holiday” 和 “vacation” 是 “Paid Time Off” 的同义词,当员工搜索其中任何一个词时,他们就会找到与所有词相关的文档。

你可以在这篇文章中阅读有关它们的更多信息。

让我们使用同义词 API(synonyms API:) 创建一组同义词:

PUT _synonyms/code-wave_synonyms
{"synonyms_set": [{"synonyms": "holidays, paid time off"}]
}

值得注意的是,同义词集必须先进行配置,然后才能应用于索引。

现在,让我们定义数据的设置和映射:

PUT /code-wave_index
{"settings": {"analysis": {"filter": {"synonyms_filter": {"type": "synonym_graph","synonyms_set": "code-wave_synonyms","updateable": true}},"analyzer": {"my_search_analyzer": {"type": "custom","tokenizer": "standard","filter": ["lowercase","synonyms_filter"]}}}},"mappings": {"properties": {"text_field": {"type": "text","analyzer": "standard","copy_to": "semantic_field","fields": {"synonyms": {"type": "text","analyzer": "standard","search_analyzer": "my_search_analyzer"}}},"semantic_field": {"type": "semantic_text","inference_id": "code-wave_inference"}}}
}

我们将使用 semantic_text 字段进行语义搜索,并使用 synonyms graph token filter 来处理多词同义词。

我们还创建了 text_field.synonym 版本和 text_field 版本的字段(可以针对这两种不同的类型进行搜索。请注意的是这两个类型都是 text 类型),以便更好地控制如何使用或不考虑同义词来查询字段。

最后,我们使用 copy_to 将 text_field 的值复制到该字段的 semantic_text 版本,以实现全文和语义查询。

索引文档

我们现在将使用批量 API 索引我们的文档:

POST _bulk
{"index":{"_index":"code-wave_index","_id":"1"}}
{"semantic_field":"Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.","text_field":"Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."}
{"index":{"_index":"code-wave_index","_id":"2"}}
{"semantic_field":"Holidays: Paid public holidays recognized each calendar year.","text_field":"Holidays: Paid public holidays recognized each calendar year."}
{"index":{"_index":"code-wave_index","_id":"3"}}
{"semantic_field":"Sick leave: Paid sick leave of up to 15 days per year.","text_field":"Sick leave: Paid sick leave of up to 15 days per year."}
{"index":{"_index":"code-wave_index","_id":"4"}}
{"semantic_field":"Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!","text_field":"Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!"}
{"index":{"_index":"code-wave_index","_id":"5"}}
{"semantic_field":"Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations.","text_field":"Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations."}
{"index":{"_index":"code-wave_index","_id":"6"}}
{"semantic_field":"Holidays travel: Find the best deals for your holidays flights and accommodations this season.","text_field":"Holidays travel: Find the best deals for your holidays flights and accommodations this season."}
{"index":{"_index":"code-wave_index","_id":"7"}}
{"semantic_field":"Holidays music: Stream your favorite holidays classics and discover new seasonal hits.","text_field":"Holidays music: Stream your favorite holidays classics and discover new seasonal hits."}
{"index":{"_index":"code-wave_index","_id":"8"}}
{"semantic_field":"Holidays decorations: Our store offers a wide range of holidays decorations to make your home festive.","text_field":"Holidays decorations: Our store offers a wide range of holidays decorations to make your home festive."}
{"index":{"_index":"code-wave_index","_id":"9"}}
{"semantic_field":"Holidays movies: Check out our list of must-watch holidays movies for cozy winter nights.","text_field":"Holidays movies: Check out our list of must-watch holidays movies for cozy winter nights."}
{"index":{"_index":"code-wave_index","_id":"10"}}
{"semantic_field":"Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food.","text_field":"Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food."}
{"index":{"_index":"code-wave_index","_id":"11"}}
{"semantic_field":"Holidays weather: Stay updated with our holidays weather forecast to plan your activities.","text_field":"Holidays weather: Stay updated with our holidays weather forecast to plan your activities."}
{"index":{"_index":"code-wave_index","_id":"12"}}
{"semantic_field":"Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list.","text_field":"Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list."}
{"index":{"_index":"code-wave_index","_id":"13"}}
{"semantic_field":"Holidays traditions: Explore unique holidays traditions celebrated around the world.","text_field":"Holidays traditions: Explore unique holidays traditions celebrated around the world."}

我们现在就可以开始搜索了!但首先,让我们通过搜索 holidays 来确保同义词有效:

GET code-wave_index/_search
{"_source": {"excludes": ["*embeddings","*chunks"]},"query": {"multi_match": {"query": "holidays","fields": ["text_field^10","text_field.synonyms^0.6"]}}
}

我们对 boost 进行调整,使同义词的得分低于原始单词。

检查响应:

{"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 12,"relation": "eq"},"max_score": 5.2014494,"hits": [{"_index": "code-wave_index","_id": "2","_score": 3.0596757,"_source": {"text_field": "Holidays: Paid public holidays recognized each calendar year.","semantic_field": {"inference": {"inference_id": "code-wave_inference","model_settings": {"task_type": "sparse_embedding"}},"text": "Holidays: Paid public holidays recognized each calendar year."}}},{"_index": "code-wave_index","_id": "1","_score": 3.023004,"_source": {"text_field": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.","semantic_field": {"inference": {"inference_id": "code-wave_inference","model_settings": {"task_type": "sparse_embedding"}},"text": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."}}},{"_index": "code-wave_index","_id": "13","_score": 2.9230676,"_source": {"text_field": "Holidays traditions: Explore unique holidays traditions celebrated around the world.","semantic_field": {"inference": {"inference_id": "code-wave_inference","model_settings": {"task_type": "sparse_embedding"}},"text": "Holidays traditions: Explore unique holidays traditions celebrated around the world."}}},...]}
}

我们可以看到,当我们搜索 “holidays” 时,第二个文档有同义词:“Paid Time Off”。

混合搜索

混合搜索使我们能够将全文和语义搜索查询的结果组合成一个规范化的结果集,方法是使用 RRF(Reciprocal Rank Fusion - 倒述排序融合)来平衡来自不同检索器的分数。

GET code-wave_index/_search
{"_source": "text_field","retriever": {"rrf": {"retrievers": [{"standard": {"query": {"nested": {"path": "semantic_field.inference.chunks","query": {"sparse_vector": {"inference_id": "code-wave_inference","field": "semantic_field.inference.chunks.embeddings","query": "holidays"}}}}}},{"standard": {"query": {"multi_match": {"query": "holidays","fields": ["text_field.synonyms"]}}}}]}}
}

回复:

{"took": 11,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 13,"relation": "eq"},"max_score": 0.03175403,"hits": [{"_index": "code-wave_index","_id": "7","_score": 0.03175403,"_source": {"text_field": "Holidays music: Stream your favorite holidays classics and discover new seasonal hits."}},{"_index": "code-wave_index","_id": "13","_score": 0.031257633,"_source": {"text_field": "Holidays traditions: Explore unique holidays traditions celebrated around the world."}},{"_index": "code-wave_index","_id": "4","_score": 0.031009614,"_source": {"text_field": "Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!"}},{"_index": "code-wave_index","_id": "2","_score": 0.030834913,"_source": {"text_field": "Holidays: Paid public holidays recognized each calendar year."}},{"_index": "code-wave_index","_id": "6","_score": 0.03079839,"_source": {"text_field": "Holidays travel: Find the best deals for your holidays flights and accommodations this season."}},{"_index": "code-wave_index","_id": "11","_score": 0.02964427,"_source": {"text_field": "Holidays weather: Stay updated with our holidays weather forecast to plan your activities."}},{"_index": "code-wave_index","_id": "5","_score": 0.029418126,"_source": {"text_field": "Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations."}},{"_index": "code-wave_index","_id": "12","_score": 0.028991597,"_source": {"text_field": "Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list."}},{"_index": "code-wave_index","_id": "1","_score": 0.016393442,"_source": {"text_field": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."}},{"_index": "code-wave_index","_id": "10","_score": 0.016393442,"_source": {"text_field": "Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food."}}]}
}

该查询将返回语义和文本相关的文档。

同义词和 RAG

在本节中,我们将评估同义词和语义搜索如何改进 RAG 系统中的查询。我们将使用一个关于休息日的常见问题作为此示例:

How many vacation days are provided for holidays?

对于这个问题,我们对文档 1 中的信息感兴趣。文档 2 更接近我们想要的结果,但并不精确。当我们不使用同义词进行搜索时,我们将得到此结果。我们来看看它们的内容:

  • [1] Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.
  • [2] Holidays: Paid public holidays recognized each calendar year.

这两个文档都包含与休息日(days off)相关的信息,但只有文档 2 特别使用了术语 “holidays”,因此我们可以测试同义词和语义搜索在 Playground 中的工作方式。

你可以从 Search>Playground 访问 Playground。从那里,你需要配置你想要使用的 LLM 并选择我们已经创建的索引作为上下文发送。你可以在此处阅读有关 Playground 及其配置的更多信息

配置完 Playground 后,如果我们点击查询按钮,我们可以看到同义词已被停用:

对于每个问题,我们会将前一个查询的前三个结果发送给 LLM,作为上下文:

现在,让我们向 Playground 提出问题并检查停用同义词后的结果:

由于前三个搜索结果中没有列出说明员工每年可享受多少假期的文件,因此 LLM 无法回答这个问题。在这种情况下,最接近的结果在文档 [2] 中。

注意:通过点击 “Snippet”,我们可以看到答案在 Elasticsearch 中的具体内容。

让我们清理聊天记录,激活同义词并再次提出同样的问题:

请注意,当你启用 semantic_text 字段和 text 字段时,Playground 将自动生成混合搜索查询:

让我们重复一下这个问题,现在激活同义词:

现在,答案确实包含了我们正在搜索的文档,因为同义词允许将文档 [1] 发送到 LLM。

结论

在本文中,我们发现同义词是搜索系统的基本组成部分,即使在使用语义搜索时也不一定涵盖同义词功能。

同义词允许我们根据用例控制要提升的文档,并通过调整相关性来提高准确性。另一方面,语义搜索对于 recall 很有用,这意味着它可以引入潜在的相关结果,而无需我们为每个相关术语添加同义词。

通过混合搜索,我们可以同时进行同义词和语义搜索,实现两全其美的效果。使用 Playground,如果我们选择语义和文本字段的组合作为搜索字段,它将自动为我们构建混合查询。

想要获得 Elastic 认证吗?了解下一期 Elasticsearch 工程师培训何时举行!

Elasticsearch 包含许多新功能,可帮助你为你的用例构建最佳的搜索解决方案。深入了解我们的示例笔记本以了解更多信息,开始免费云试用,或立即在本地机器上试用 Elastic。

原文:Are synonyms important in RAG? - Elasticsearch Labs

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法与数据结构(最小栈)

题目 思路 为了返回栈中的最小元素,我们需要额外维护一个辅助栈 min_stack,它的作用是记录当前栈中的最小值。 min_stack的作用: min_stack的栈顶元素始终是当前栈 st 中的最小值。 每当st中压入一个新元素时,如果这个元素小于等…

midjourney 一 prompt 提示词

midjourney 不需要自然语言的描述,它只需要关键词即可。 一个完整的Midjourney prompt通常包括三个部分 图片提示(Image Prompts)、文本提示(Text Prompt)和参数(Parameters)。 1、图片提示(…

英码科技基于昇腾算力实现DeepSeek离线部署

DeepSeek-R1 模型以其创新架构和高效能技术迅速成为行业焦点。如果能够在边缘进行离线部署,不仅能发挥DeepSeek大模型的效果,还能确保数据处理的安全性和可控性。 英码科技作为AI算力产品和AI应用解决方案服务商,积极响应市场需求&#xff0…

Android13-包安装器PackageInstaller-之apk安装流程

目的 我们最终是为了搞明白安装的整个流程通过安卓系统自带的包安装器来了解PMS 安装流程实现需求定制:静默安装-安装界面定制-安装拦截验证。【核心目的】 安装流程和PMS了解不用多说了; 安装定制相关: 如 手机上安装时候弹出锁屏界面需要输入密码;安…

新型基于Go语言的恶意软件利用Telegram作为C2通信渠道

研究人员发现了一种新型后门恶意软件,使用Go语言编写,并利用Telegram作为其命令与控制(C2)通信渠道。尽管该恶意软件似乎仍处于开发阶段,但它已经具备完整的功能,能够执行多种恶意活动。这种创新的C2通信方…

5分钟了解! 探索 AnythingLLM,借助开源 AI 打造私有化智能知识库,熟悉向量数据库

本文是系列文章,在前面提到安装Ollama和AnythingLLM的教程,本文会着重解决本地文档向量化的过程,同时本地应用的管理。 图1. 上传本地文档进行向量化处理 • 构建向量数据库特别慢:支持的文档格式很多,但在我的电脑32…

电商小程序(源码+文档+部署+讲解)

引言 随着移动互联网的快速发展,电商小程序成为连接消费者与商家的重要桥梁。电商小程序通过数字化手段,为消费者提供了一个便捷、高效的购物平台,从而提升购物体验和满意度。 系统概述 电商小程序采用前后端分离的架构设计,服…

基于SpringBoot的“高考志愿智能推荐系统”的设计与实现(源码+数据库+文档+PPT)

基于SpringBoot的“高考志愿智能推荐系统”的设计与实现(源码数据库文档PPT) 开发语言:Java 数据库:MySQL 技术:SpringBoot 工具:IDEA/Ecilpse、Navicat、Maven 系统展示 系统总体结构图 系统首页界面 系统注册页…

合并多次commit记录

合并多次commit记录 1. 首先先提交多次记录2. 某个版本之前的commit记录合并 1. 首先先提交多次记录 在log中可以看到有多次commit 记录 然后拉取最新代码 (base) ➜ gaolijie git:(master) git pull --rebase origin masterFrom https://gitee.com/Blue_Pepsi_Cola/gaoliji…

哈希表(C语言版)

文章目录 哈希表原理实现(无自动扩容功能)代码运行结果 分析应用 哈希表 如何统计一段文本中,小写字母出现的次数? 显然,我们可以用数组 int table[26] 来存储每个小写字母出现的次数,而且这样处理,效率奇高。假如我们想知道字…

uniapp商城之首页模块

文章目录 前言一、自定义导航栏1.静态结构2.修改页面配置3.组件安全区适配二、通用轮播组件1. 静态结构组件2.自动导入全局组件3.首页轮播图数据获取三、首页分类1.静态结构2.首页获取分类数据并渲染四、热门推荐1.静态结构2.首页获取推荐数据并渲染3.首页跳转详细推荐页五、猜…

CNAPPgoat:一款针对云环境的安全实践靶场

关于CNAPPgoat CNAPPgoat是一款针对云环境的安全实践靶场,该工具旨在帮助广大研究人员在云环境中模块化地提供故意留下安全缺陷的设计组件,专为防御者和渗透测试人员提供练习场地而设计。 CNAPPgoat的主要功能是跨多个云服务提供商部署故意留下安全缺陷…

【学习资源】时间序列数据分析方法(2)-mWDN和AutoEncoder

接着上次的【学习资源】时间序列数据分析方法(1)-CSDN博客,本次介绍mWDN和AutoEncoder 解决时序数据分类的方法。介绍模型原理、应用场景和参考代码。也从模型性能、训练效率、模型复杂度、计算复杂度、可解释性、适应性和泛化能力、健壮性、…

【C++】stack 和 queue 的适配器模式与实现

> 🍃 本系列为初阶C的内容,如果感兴趣,欢迎订阅🚩 > 🎊个人主页:[小编的个人主页])小编的个人主页 > 🎀 🎉欢迎大家点赞👍收藏⭐文章 > ✌️ 🤞 &#x1…

Chrome多开终极形态解锁!「窗口管理工具+IP隔离插件

Web3项目多开,继ads指纹浏览器钱包被盗后,更多人采用原生chrome浏览器,当然对于新手,指纹浏览器每月成本也是一笔不小开支,今天逛Github发现了这样一个解决方案,作者开发了窗口管理工具IP隔离插件&#xff…

从零开始部署DeepSeek:基于Ollama+Flask的本地化AI对话系统

从零开始部署DeepSeek:基于OllamaFlask的本地化AI对话系统 一、部署背景与工具选型 在AI大模型遍地开花的2025年,DeepSeek R1凭借其出色的推理能力和开源特性成为开发者首选。本文将以零基础视角,通过以下工具链实现本地化部署: …

python旅游推荐系统+爬虫+可视化(协同过滤算法)

✅️基于用户的协同过滤算法 ✅️有后台管理 ✅️2w多数据集 这个旅游数据分析推荐系统采用了Python语言、Django框架、MySQL数据库、requests库进行网络爬虫开发、机器学习中的协同过滤算法、ECharts数据可视化技术,以实现从网站抓取旅游数据、个性化推荐和直观展…

以 Serverless 低成本的⽅式 快速在亚马逊云科技上部署 DeepSeek

2025年春节,最令人瞩目的无疑是DeepSeek的惊艳亮相,它以颠覆性的创新迅速席卷全球,成为街谈巷议的热点。无论是在地铁车厢里,还是公司茶水间,DeepSeek都成了人们津津乐道的话题。社交平台上,网友们争相分享…

win10 系统 自定义Ollama安装路径 及模型下载位置

win10 系统 自定义Ollama安装路径 及模型下载位置 由于Ollama的exe安装软件双击安装的时候默认是在C盘,以及后续的模型数据下载也在C盘,导致会占用C盘空间,所以这里单独写了一个自定义安装Ollama安装目录的教程。 Ollama官网地址&#xff1…

CAP与BASE:分布式系统设计的灵魂与妥协

CAP 理论 CAP理论起源于 2000 年,由加州大学伯克利分校的 Eric Brewer 教授在分布式计算原理研讨会(PODC)上提出,因此 CAP 定理又被称作 布鲁尔定理(Brewer’s theorem) 2 年后,麻省理工学院的 …