es 3期 第14节-全文文本分词查询
#### 1.Elasticsearch是数据库,不是普通的Java应用程序,传统数据库需要的硬件资源同样需要,提升性能最有效的就是升级硬件。
#### 2.Elasticsearch是文档型数据库,不是关系型数据库,不具备严格的ACID事务特性,任何企图直接替代严格事务性场景的应用项目都会失败!!!
##### 索引字段与属性都属于静态设置,若后期变更历史数据需要重建索引才可生效
##### 对历史数据无效!!!!
##### 一定要重建索引!!!
#### 全文文本概念
### 概念介绍
## 1.文章语句分词
## 2.分词之后,支持基于分词检索
## 3.分词算法很多,分词领域很深入
## 4.基于倒排索引算法-Inverted-Index
## 5.分词检索的打分算法TF/IDF=>BM25
## 6.字段类型仅限于text类型
## 全文搜索内容较深,初步学习使用即可
# es测试分词器默认api语法,默认分词算法 standard 按照空格、逗号这种方式分
# 初步理解分词,数据在入库前已经做好了分词并建立了索引
POST _analyze
{"text": ["hello every body, 我是DavidSoCool, 我正在学习es"],"analyzer":"standard"
}
### 全文文本检索
# Match-all:全查询
# Match:标准分词
# 准备数据
DELETE kibana_sample_data_flights_fulltext
POST _reindex
{"source": {"index": "kibana_sample_data_flights"},"dest": {"index": "kibana_sample_data_flights_fulltext"}
}
## match-all 全匹配
# 1.Match all没有限制条件,直接等同于search查询
# 2.boost:可以调整加权数值
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_all": {"boost": 10}}
}
## match_none,反向全匹配,可用于测试索引健康,不同与查询数据消耗性能
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_none": {}}
}
## match,文本匹配,最常用的
# 排序默认根据_score分值,匹配的次越多,分值就越高,可以用于做简单的推荐系统
GET kibana_sample_data_flights_fulltext/_mapping
# 先测试下分词结果,分成了4个词
POST _analyze
{"text": ["Cape Town International Airport"],"analyzer":"standard"
}
# 任意匹配一个词就能查询出来
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":"Cape Town International Airport"}}
}
# 看5000条以后_score分值和Origin字段匹配的数量
GET kibana_sample_data_flights_fulltext/_search
{"from":1000,"track_total_hits": true,"query":{"match": {"Origin":"Cape Town International Airport"}}
}
# 看9000条以后_score分值和Origin字段匹配的数量
GET kibana_sample_data_flights_fulltext/_search
{"from":9000,"track_total_hits": true,"query":{"match": {"Origin":"Cape Town International Airport"}}
}
## Request 请求参数
# query:查询表达式
# analyzer:指定分词器,对于查询输入的文本进行分词
# operator:分词之间关联关系,默认是or
# minimum_should_match:分词最小匹配数量
# 这条语句等价于下面那条
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or"}}}
}
# 这条语句等价于上面那条
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":"Cape Town International Airport"}}
}
# 使用operator=and,表示所有词都匹配上,注意看total
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "and"}}}
}
# 去掉前两个词后,跳过100条看看
GET kibana_sample_data_flights_fulltext/_search
{"from": 100,"track_total_hits": true,"query":{"match": {"Origin":{"query": "International Airport","analyzer": "standard","operator": "and"}}}
}
# minimum_should_match,控制匹配词的精确度,可以使用数字和百分比
# 只能用or,and会查不出数据
GET kibana_sample_data_flights_fulltext/_search
{"from": 100,"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or","minimum_should_match": 2}}}
}
# 跳过数据看看,total总数111条,跳过110条
# 第111条还是全匹配数据,112开始就只有2个词匹配的数据了
GET kibana_sample_data_flights_fulltext/_search
{"from": 110,"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or","minimum_should_match": 2}}}
}
# 如何minimum_should_match=4就相当于使用and的了
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or","minimum_should_match": 4}}}
}
# minimum_should_match 使用百分比,这里不是简单看分词的比例,需要看文档理解
# 建议还是使用数字,如果词很多可以使用百分比
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or","minimum_should_match": "50%"}}}
}
# minimum_should_match 也可以使用负数,相当于是负相关,不建议使用
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Cape Town International Airport","analyzer": "standard","operator": "or","minimum_should_match": -1}}}
}
# fuzziness 纠错搜索,可以帮助我们纠正输入错误的词,具体看文档
# 将Cape输入成错误的Capa
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":{"query": "Capa","analyzer": "standard","operator": "or","fuzziness": 1}}}
}
## Match boolPrefix前缀匹配
# 集成了match和bool
# 去掉最后的Airport,并且把International最后的l去掉,相当于前面2个单词全匹配,最后一个Internationa使用的前缀匹配
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_bool_prefix": {"Origin":"Cape Town Internationa"}}
}
# 原语句
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match": {"Origin":"Cape Town International Airport"}}
}
## match_phrase 短语搜索,按照我们输入的词顺序匹配,之前的是每个词各自匹配
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase": {"Origin":"Cape Town International Airport"}}
}
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase": {"Origin":"Cape Town"}}
}
# 中间跳过一个词Town就查不出来,因为没有这个短语
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase": {"Origin":"Cape International Airport"}}
}
# slop参数,匹配允许短语间隔误差词数量,中间跳过一个词Town也可以查出来
# slop会耗费计算资源
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase": {"Origin":{"query": "Cape International Airport","slop": 1}}}
}
# slop参数,中间跳过两个词
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase": {"Origin":{"query": "Cape Airport","slop": 2}}}
}
## Match phase prefix
# 短语前缀查询,集成了短语匹配+前缀
# 前面分词走短语查询
# 最后的分词走前缀查询
# 把Airport的末尾t去掉,效率比slop高效些
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query":{"match_phrase_prefix": {"Origin":"Cape Town International Airpor"}}
}
## Multi match 多字段
# 很多应用场景需要同时查询多个字段,查询内容一样如电商领域,商品标题与商品描述
# Multimatch专门解决此场景需求,单个字段查询时等同与match匹配
## type 匹配类型
# best_fields,多字段中选择分值最高的字段,默认匹配类型
# most_fields,多字段分值累计和
# cross_fields,多字段查询时,部分分词在第一个字段里,其它的分词在另外的字段里phrase,短语匹配,等同match_phase
# phrase_prefix,短语前缀匹配,等同match_phase_prefix
# bool_prefix,全文匹配逻辑前缀,等同match_bool_prefix.
# tie_breaker,选择多字段分值计算方式,0-选择其中较大的,1-选择合并
# 切换不同的类型(best_fields/most_fields),测试对比前后的分值与结果数量
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query": {"multi_match": {"query": "Cape Town International Airport","type": "best_fields","fields": ["Origin","Dest"]}}
}
# 还可以使用模糊匹配字段
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query": {"multi_match": {"query": "Cape Town International Airport","fields": "*rigin"}}
}
# 多个字段匹配,使用^符号和后面增加权重值数字,增加某个字段的权重,类同于单独写boost
GET kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query": {"multi_match": {"query": "Cape Town International Airport","type": "best_fields","fields": ["Origin","Dest^2"]}}
}
## Intervals文本顺序间隔,这个比较复杂一般用不上,需要深入研究
# 间隔查询是全文分词非常⾼级的查询能⼒,容许控制输入分词查询与内容之间的间隔。⽀持了多种间隔类型机制。
# 多个查询检索条件有先后,先基于第⼀个条件查询,之后在结果集上执⾏后⾯的查询条件,类似于 if,then 逻辑
## intervals match 间隔匹配查询
# match,关键字,间隔查询的全文分词⽅式,等同前⾯的match查询
# query,关键字,查询输入的内容
# max_gaps,关键字,容许中间间隔最⼤的词数量,默认-1,不限制
# ordered,关键字,查询的内容是否必须符合顺序,取值true/false,默认false
# analyzer,关键字,分词器
# filter,关键字,⼆级查询过滤器,⽀持多种过滤类型
# use_field,⾃定义字段类型,
## filter 参数说明,⼆级查询过滤器,⽀持多种过滤类型
# 类型 说明
# after query查询在此之后执⾏
# before query查询在此之前执⾏
# contained_by 包含此执⾏条件之内的结果
# containing 包含此执⾏条件
# not_contained_by 不在此执⾏结果之内
# not_containing 不包含此条件
# not_overlapping 不重叠条件
# overlapping 重叠条件
# script 基于painless脚本限制
POST kibana_sample_data_flights_fulltext/_search
{"track_total_hits": true,"query": {"intervals": {"Dest": {"match": {"ordered": true,"query": "Sydney Smith Airport","analyzer": "standard","max_gaps": 2,"filter": {"containing": {"match": {"query": "International"}}}}}}}
}
## Query String查询字符
# DSL查询比较复杂,ES也提供了类似SOL表达式的查询方式,但功能性上并未超越DSL,仅仅是方便
# 优缺点优点:简单直接
# 缺点:语法阅读困难,表达能力有限,建议尽量不使用
# 查询Dest,用or的方式
POST kibana_sample_data_flights_fulltext/_search
{"query":{"query_string": {"query": "Dest:(Phoenix or Ministro)"}}
}
# 查询数字范围
POST kibana_sample_data_flights_fulltext/_search
{"query":{"query_string": {"query": "FlightDelayMin:[10 TO 100]"}}
}
## Url查询字符
# 查询表达式基于URL的形式
## 优缺点
# 优点:简洁直接
# 缺点:表达能力局限,极少情况下应用,建议使用DSL
POST kibana_sample_data_flights_fulltext/_search?q=(Dest:Phoenix) AND (Origin:Chubu)
### 查询性能分析
## Profile性能分析
# 1.基于查询树,生成性能分析报告
# 2.与传统关系型数据库执行计划一样等价
# 3.Kibana具备可视化功能,看懂需要一定功力
POST kibana_sample_data_flights_fulltext/_search
{"profile":true,"query":{"query_string": {"query": "Dest:(Phoenix or Ministro)"}}
}
profile查询解结果如下

还可使用search profiler如下

## Explain分值计算评估,有兴趣可以深入
# 1.解释分值计算逻辑与规则
# 2.帮助理解全文查询分值计算信息
POST kibana_sample_data_flights_fulltext/_explain/74TR0Y8BbWz2Sn6EhZCn
{"query":{"match": {"Dest": "Ministro Pistarini International Airport"}}
}
_explain结果如下,这是Dest字段ministro的分值计算

## 全文查询建议
# 全文文本查询是非精确查询(可以通过一些参数控制位精确查询)
# 查询关联度与分词算法(需要去了解,查询结果不是想要的并非是es错误)
# 查询精确度问题(近似值)
elasticsearch text 文本字段类型官⽅参考 https://www.elastic.co/guide/en/elasticsearch/reference/8.6/text.html
elasticsearch analysis-analyzers 内置分词器官⽅参考 https://www.elastic.co/guide/en/elasticsearch/reference/8.6/analysis-analyzers.html
elasticsearch full-text-queries 全文查询官⽅参考 https://www.elastic.co/guide/en/elasticsearch/reference/8.6/full-text-queries.html
elasticsearch query-dsl-intervals-query 间隔查询官⽅参考 https://www.elastic.co/guide/en/elasticsearch/reference/8.6/query-dsl-intervals-query.html
elasticsearch index-modules-similarity
elasticsearch similarity 相似度算法官⽅参考 https://www.elastic.co/guide/en/elasticsearch/reference/8.6/index-modules-similarity.html https://www.elastic.co/guide/en/elasticsearch/reference/8.6/similarity.html
相关文章:
es 3期 第14节-全文文本分词查询
#### 1.Elasticsearch是数据库,不是普通的Java应用程序,传统数据库需要的硬件资源同样需要,提升性能最有效的就是升级硬件。 #### 2.Elasticsearch是文档型数据库,不是关系型数据库,不具备严格的ACID事务特性ÿ…...
六安市第二届网络安全大赛复现
misc 听说你也喜欢俄罗斯方块? ppt拼接之后 缺三个角补上 flag{qfnh_wergh_wqef} 流量分析 流量包分离出来一个压缩包 出来一张图片 黑色代表0白色代表1 101010 1000 rab 反的压缩包 转一下 密码:拾叁拾陆叁拾贰陆拾肆 密文:4p4n5758…...
Sarcomere仿人灵巧手ARTUS,20个自由度拓宽机器人作业边界
Sarcomere Dynamics 是一家深度技术先驱,通过开发和商业化仿人机械来改变机器人行业。专注于为科研人员,系统集成商和制造商提供更实惠、更轻便且更灵活的末端执行器替代品。凭借创新的致动器技术,创造了一款紧凑、轻便且非常坚固的机械手Art…...
Django drf 基于serializers 快速使用
1. 安装: pip install djangorestframework 2. 添加rest_framework到您的INSTALLED_APPS设置。 settings.pyINSTALLED_APPS [...rest_framework, ] 3. 定义模型 models.pyfrom django.db import modelsclass BookModel(models.Model):name models.CharField(max_length64)…...
pycharm集成环境中关于安装sklearn库报错问题分析及解决
在输入pip install sklearn后,出现如下提示: pip install sklearn Collecting sklearn Using cached sklearn-0.0.post12.tar.gz (2.6 kB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-…...
AI - 浅聊一下基于LangChain的AI Agent
AI - 浅聊一下基于LangChain的AI Agent 大家好,今天我们来聊聊一个很有意思的主题: AI Agent ,就是目前非常流行的所谓的AI智能体。AI的发展日新月异,都2024年末了,如果此时小伙伴们对这个非常火的概念还不清楚的话&a…...
《【Linux】深入理解进程管理与 fork 系统调用的实现原理》
一、引言 在 Linux 操作系统中,进程管理是核心功能之一。进程是操作系统进行资源分配和调度的基本单位。理解进程管理的原理以及 fork 系统调用的实现对于深入掌握 Linux 系统的运行机制至关重要。本文将深入探讨 Linux 中的进程管理以及 fork 系统调用的实现原理&a…...
docker-compose部署skywalking 8.1.0
一、下载镜像 #注意 skywalking-oap-server和skywalking java agent版本强关联,版本需要保持一致性 docker pull elasticsearch:7.9.0 docker pull apache/skywalking-oap-server:8.1.0-es7 docker pull apache/skywalking-ui:8.1.0二、部署文件docker-compose.yam…...
AI 总结的的 AI 学习路线
一、入门阶段:数学基础与编程语言 数学基础 线性代数 当年白纸黑字推演, 都是泪啊,草稿本都用了一卷。 学习向量、矩阵的基本概念,包括向量的加法、减法、点积和叉积,矩阵的乘法、转置等运算。例如,在计算…...
离散傅里叶级数(DFS)详解
1. 引言 离散傅里叶级数(Discrete Fourier Series, DFS)是信号处理领域中一项基础且重要的数学工具,用于分析和处理周期性的离散信号。它通过将离散时间信号表示为一组正弦和余弦的和,从而使得信号在频域上得到更清晰的描述。与连…...
Java 类加载机制详解
🧑 博主简介:CSDN博客专家,历代文学网(PC端可以访问:https://literature.sinhy.com/#/literature?__c1000,移动端可微信小程序搜索“历代文学”)总架构师,15年工作经验,…...
1.1 Beginner Level学习之“编写简单的发布服务器和订阅服务器”(第十一节)
学习大纲: 1. 编写发布服务器节点 在 ROS 中,节点是连接到 ROS 网络的可执行文件。我创建了一个名为 talker 的发布者节点,它会向一个主题 chatter 不断发送消息。 首先,进入你的工作包 beginner_tutorials(假设你已…...
AIQuora:开启论文写作新篇章
在这个信息爆炸的时代,学术写作已成为研究者不可或缺的技能。然而,面对繁重的写作任务,许多学者和学生常常感到力不从心。AIQuora,一个专业的文理工科论文智能写作助手,以其免费开题报告生成功能,为学术写作…...
【C语言】库函数常见的陷阱与缺陷(1):字符串处理函数
目录 一、 strcpy 函数 1.1. 功能与常见用法 1.2. 陷阱与缺陷 1.3. 安全替代 1.4. 代码示例 二、strcat 函数 2.1. 功能与常见用法 2.2. 陷阱与缺陷 2.3. 安全替代 2.4. 代码示例 三、strcmp 函数 3.1. 功能与常见用法 3.2. 陷阱与缺陷 3.3. 安全替代 3.4. 代…...
Mysql索引原理及优化——岁月云实战笔记
根据Mysql索引原理及优化这个博主的视频学习,对现在的岁月云项目中mysql进行优化,我要向这个博主致敬,之前应用居多,理论所知甚少,于是将学习到东西,应用下来,看看是否有好的改观。 1 索引原理…...
AGCRN论文解读
一、创新点 传统GCN只能基于静态预定义图建模全局共享模式,而AGCRN通过两种GCN的增强模块(NAPL、DAGG)实现了更精细的节点特性学习和图结构生成。 1 节点自适应参数学习模块(NAPL) 传统GCN通过共享参数(权重…...
Python机器学习笔记(五、决策树集成)
集成(ensemble)是合并多个机器学习模型来构建更强大模型的方法。这里主要学习两种集成模型:一是随机森林(random forest);二是梯度提升决策树(gradient boosted decision tree)。 1…...
Kafka单机及集群部署及基础命令
目录 一、 Kafka介绍1、kafka定义2、传统消息队列应用场景3、kafka特点和优势4、kafka角色介绍5、分区和副本的优势6、kafka 写入消息的流程 二、Kafka单机部署1、基础环境2、iptables -L -n配置3、下载并解压kafka部署包至/usr/local/目录4、修改server.properties5、修改/etc…...
如何使用 Python 实现链表的反转?
在Python中实现链表的反转可以通过几种不同的方法。这里,我将向你展示如何使用迭代和递归两种方式来反转链表。 1. 迭代方法 迭代方法是通过遍历链表,逐个节点地改变其指向来实现反转的。 class ListNode: def __init__(self, val0, nextNone): …...
react跳转传参的方法
传参 首先下载命令行 npm react-router-dom 然后引入此代码 前面跳转的是页面 后面传的是你需要传的参数接参 引入此方法 useLocation():这是 react-router-dom 提供的一个钩子,用于获取当前路由的位置对象location.state:这是从其他页面传…...
[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?
🧠 智能合约中的数据是如何在区块链中保持一致的? 为什么所有区块链节点都能得出相同结果?合约调用这么复杂,状态真能保持一致吗?本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里…...
python打卡day49
知识点回顾: 通道注意力模块复习空间注意力模块CBAM的定义 作业:尝试对今天的模型检查参数数目,并用tensorboard查看训练过程 import torch import torch.nn as nn# 定义通道注意力 class ChannelAttention(nn.Module):def __init__(self,…...
uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖
在前面的练习中,每个页面需要使用ref,onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入,需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...
CentOS下的分布式内存计算Spark环境部署
一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架,相比 MapReduce 具有以下核心优势: 内存计算:数据可常驻内存,迭代计算性能提升 10-100 倍(文档段落:3-79…...
智能仓储的未来:自动化、AI与数据分析如何重塑物流中心
当仓库学会“思考”,物流的终极形态正在诞生 想象这样的场景: 凌晨3点,某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径;AI视觉系统在0.1秒内扫描包裹信息;数字孪生平台正模拟次日峰值流量压力…...
代理篇12|深入理解 Vite中的Proxy接口代理配置
在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
python报错No module named ‘tensorflow.keras‘
是由于不同版本的tensorflow下的keras所在的路径不同,结合所安装的tensorflow的目录结构修改from语句即可。 原语句: from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后: from tensorflow.python.keras.lay…...
人机融合智能 | “人智交互”跨学科新领域
本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...
Qemu arm操作系统开发环境
使用qemu虚拟arm硬件比较合适。 步骤如下: 安装qemu apt install qemu-system安装aarch64-none-elf-gcc 需要手动下载,下载地址:https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x…...
