当前位置: 首页 > news >正文

10 分钟上手 Elasticsearch 语义搜索(Serverless Cloud 本地双版本教程)

1. 场景 & 术语速览

概念解释
语义搜索用自然语言检索「含义相近」的文档,而非纯关键词匹配
向量/Embedding把文本映射为 N 维数组;相似文本→距离更近
ELSERElastic 默认稀疏向量模型 (Learned Sparse Encoder),无需微调即可跨领域检索
semantic_textES 8.11 + 内置字段类型,存储向量+自动推断模型,免手动 token/vocab

2. 环境准备

Serverless(推荐新同学)

  1. 进入 Elasticsearch → Home
  2. 选择 “Semantic search workflow” → Create a semantic-optimized index
  3. 自动创建向量索引 + 示例数据

Elastic Cloud / 本地集群

# Kibana DevTools / Console
PUT /semantic-index
  • superuser(本地)或 开发者 / 管理员(Cloud)角色
  • 若初次加载 ELSER,模型会在后台下载(1-5 分钟)

3. 创建向量索引 & 映射

PUT /semantic-index/_mapping
{"properties": {"content": {        # 任意字段名"type": "semantic_text"# 嵌入模型自动 = ELSER}}
}

为什么选 semantic_text

  • 默认填好稀疏向量参数
  • 自动拆分大段文本、做 chunking
  • 查询时 match / knn 语法保持一致

4. 批量写入示例文档

POST /_bulk?pretty
{ "index": { "_index": "semantic-index" } }
{ "content": "Yellowstone National Park …" }
{ "index": { "_index": "semantic-index" } }
{ "content": "Yosemite National Park …" }
{ "index": { "_index": "semantic-index" } }
{ "content": "Rocky Mountain National Park …" }
  • 408/504 超时:等待模型加载完后重试
  • 文本会被 自动切片 + 编码为稀疏向量

5. Discover 浏览 & ES | QL 入口

  1. Kibana 左侧导航 → Discover
  2. 选中 semantic-index
  3. 任意文档点击 “⬆️” 展开查看字段
  4. 顶栏 → Try ES|QL 打开查询编辑器

6. ES | QL 语义检索

6.1 入门查询

FROM semantic-index
| WHERE content: "what's the biggest park?"
| LIMIT 10
  • content: 语法自动走 语义匹配(ELSER 稀疏向量)
  • LIMIT 控制返回行数

6.2 查看相关性分值并排序

FROM semantic-index METADATA _score
| WHERE content: "best spot for rappelling"
| KEEP content, _score
| SORT _score DESC
| LIMIT 10
命令作用
METADATA _score暴露 Lucene 相关度得分
KEEP控制显示列
SORT _score DESC按得分降序

查看结果:Rocky Mountain National Park… 通常排第 1,最适合攀岩。

7. 整个查询走 REST API(脚本 / 自动化用)

POST /_query?format=txt
{"query": """FROM semantic-index METADATA _score| WHERE content: "best spot for rappelling"| KEEP content, _score| SORT _score DESC| LIMIT 10"""
}

删除样例数据:

DELETE /semantic-index

8. 常见问题 FAQ

症状处理方式
model download timeout检查 Stack Management → Trained Models,确认 ELSER 状态 started
向量检索慢确保索引设置 default_pipeline 未做阻塞式处理;生产环境建议调整 segment 刷新周期
想混合关键字 + 语义创建 text + semantic_text 双字段,用 bool 查询或 ESQL WHERE 逻辑组合

9. 深度扩展方向

  1. Hybrid Search{ "multi_match": … } + knn / semantic weight 融合
  2. Dense Vector:接入 OpenAI / HuggingFace Embedding,自定义 dense_vector 字段
  3. RAG:向量检索 + LLM Prompt,构建企业检索增强生成 (Retrieval-Augmented Generation)
  4. A/B Relevance:使用 [Search Playground] 比较 BM25 vs ELSER 召回质量
  5. Serverless Pipeline:结合 Elastic Serverless 功能,0 DevOps 成本托管向量索引
http://www.dtcms.com/a/305883.html

相关文章:

  • 在 Kubernetes 上部署 Label Studio
  • 汽车免拆诊断案例 | 免拆诊断发动机起动困难故障2例
  • Visual Studio Code 使用指南 (2025年版)
  • lesson28:Python单例模式全解析:从基础实现到企业级最佳实践
  • 【网络工程师软考版】路由协议 + ACL
  • 网络资源模板--基于Android Studio 实现的图书商城App
  • 如何不让android studio自动换行
  • UDP通讯和TCP通讯的区别-UDP(用户数据报协议)和 TCP(传输控制协议)
  • 第十七章 追新词
  • Python 闭包(Closure) 特性剖析
  • Linux ps -ef 命令解析
  • 三十二、【Linux网站服务器】搭建httpd服务器演示虚拟主机配置、网页重定向功能
  • STM32学习记录--Day3
  • ubuntu编译opendds
  • STM32CubeMX 生成时钟获取函数的分析
  • OmniParser:提升工作效率的视觉界面解析工具
  • react+ant design怎么样式穿透-tooltip怎么去掉箭头
  • 地下停车场照明节能:技术方案与价值重构
  • day7 替换数字那道题需要多写几次
  • 容器与虚拟机的本质差异:从资源隔离到网络存储机制
  • InfluxDB 3 数据库命名与创建全攻略:规范、限制与实战指南
  • Linux 进程管理与计划任务详解
  • 在 CentOS 系统上安装 Docker
  • ComfyUI自定义图片导出路径
  • SpringBoot 2.7.18 升级 3.4.6
  • PHP语法高级篇(七):MySQL数据库
  • 具身智能 自动驾驶相关岗位的技术栈与能力地图
  • 微信小程序中实现页面跳转的方法
  • 爬虫验证码处理:ddddocr 的详细使用(通用验证码识别OCR pypi版)
  • 第2章 cmd命令基础:常用基础命令(2)