es 3期 第26节-文本分词深入探查
#### 1.Elasticsearch是数据库,不是普通的Java应用程序,传统数据库需要的硬件资源同样需要,提升性能最有效的就是升级硬件。
#### 2.Elasticsearch是文档型数据库,不是关系型数据库,不具备严格的ACID事务特性,任何企图直接替代严格事务性场景的应用项目都会失败!!!
#### 3.Elasticsearch原则上适合一切非事务性应用场景或能够容许一定的延迟的事务性场景;能最大限度的替代mongodb与传统关系型数据库
##### 索引字段与属性都属于静态设置,若后期变更历史数据需要重建索引才可生效
##### 对历史数据无效!!!!
##### 一定要重建索引!!!!
#### 1、文本分词概念介绍
### Analyzer分词概念
## 分词概念
# 名词解释Analyzer,分词器
## 分词概念
# 文本信息按照一定规则算法进行拆解,分成多个独立的词项;便于搜索时,基于分词之后的字符去检索。
# 如:杭州亚运会
### 倒排索引
# 倒排索引是ES存储的基础,文本信息分词之后基于排索引构建索引库,即使没有分词,也是一个独立此项,也会基于倒排存储;
# 倒排泛指索引的规则,而不是只一种具体的算法,ES内部基于多种算法实现。
行式:
列式:
### 分词需求应用场景
# 应用场景
# 全文检索
# 电商搜索
# 知识库检索,wiki系统
#### 2、常用分词器实战
### Text文本字段类型
# 字段类型
# Text:文本类型,默认分词,具体分词算法由分词器负责。
### 常用内置分词器
## 内置分词器
# Standard,默认,中文按照单字分词,支持标点符号
# Simple,转换英文大小写,去掉数字,中文分词,按照空格
# Whitespance,按照空格拆分词,不转换其中任何的字符,中文分词时,也是按照空格分词
# 其它
### 测试分词器API
POST _analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025"
],
"analyzer": "standard"
}
POST _analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025"
],
"analyzer": "simple"
}
POST _analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025"
],
"analyzer": "whitespace"
}
#### 3、深入分词器组成
### 分词器组成部分
# Character filters:分词器组成字符过滤转换,语法关键字: char_filter
# Tokenizer:基于分词规则算法,执行文本分词,语法关键字:tokenizer
# Token filter:基于前面文本分词之后的词项,做一些过滤信息,处理一些词项,达到归一化处理,语法关键字: filter
## html_strip
# html_strip,去除 html,过滤字符
# char_filter,字符过滤表达式
POST _analyze
{
"text": [
"<h1>Hello,大卫</h1>,I am software engineer, This year is 2025"
],
"char_filter": ["html_strip"]
}
POST _analyze
{
"text": [
"AAABBBCCCDDD"
],
"char_filter": [
{
"type": "mapping",
"mappings": [
"A=>1",
"B=>2",
"C=>3"
]
}
]
}
## Tokenizer
# 分词器组成
# 词项分析分词器:Tokenizer
POST _analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025"
],
"tokenizer": "standard"
}
## 分词器组成部分
# 词项过滤转换:Token filter
## lowercase
# filter,字符过滤转换
# lowercase,统一小写
POST _analyze
{
"text": [
"Hello,大卫,I Am Software Engineer, This Year Is 2025"
],
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
## stop
# 停用词,用于去除分词之后的一些特殊词项
# 第1个去除“a"与 ”and";第2个去除自定义词
POST _analyze
{
"text": [
"Hello,大卫,I a software engineer and people,the This year is 2025"
],
"tokenizer": "standard",
"filter": [
"stop"
]
}
## synonym
# 同义词转换
# 将语句中的"hello,Hello“转换为"您好
POST _analyze
{
"text": [
"Hello,大卫,I a software engineer and people,the This year is 2025"
],
"tokenizer": "standard",
"filter": [
{
"type": "synonym",
"synonyms": [
"Hello=>你好"
]
}
]
}
## 组合一起
# 以上3者组合一起,才是一个完整的分词器
POST _analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025 AABBCC"
],
"tokenizer": "standard",
"filter":[
{
"type":"stop",
"stopwords":[
"software","engineer", "year"
]
},
"uppercase"
],
"char_filter": [
{
"type": "mapping",
"mappings": [
"A=>1",
"B=>2",
"C=>3"
]
}
]
}
#### 4、自定义分词器实战
## 参数说明
# analysis,分词器表达式入口
# analyzer,分词器内容表达式入口
# type,分词器类型,custom 表示自定义
# tokenizer,选择分词分词器
# char_filter,字符过滤转换
# filter,词项过滤器
# mappings,在字段中设置刚刚创建的分词器
DELETE device-001
# 创建带分词器索引
PUT device-001
{
"settings": {
"analysis": {
"analyzer": {
// 自定义名称
"my_analysis": {
"type": "custom",
"char_filter": [
"html_strip"
],
"tokenizer": "standard",
"filter": [
// 转成大写
"uppercase"
]
}
}
}
},
"mappings": {
"properties": {
"f1": {
"type": "text",
// 指定写入数据时分词器
"analyzer": "my_analysis",
// 指定查询时的分词器,这个不指定默认是使用写入时的分词器,不要随便设置成不一样的!!!
"search_analyzer":"my_analysis"
},
"f2": {
"type": "text",
"analyzer": "standard"
}
}
}
}
# 测试分词器,分词器与索引绑定,需要再前面加索引名
POST device-001/_analyze
{
"text": [
"Hello,大卫,I am software engineer, This year is 2025 AABBCC"
],
"analyzer": "my_analysis"
}
# 新增数据
POST device-001/_doc
{
"f1":"Hello,大卫,I am software engineer, This year is 2025 AABBCC",
"f2":"Hello,大卫,I am software engineer, This year is 2025 AABBCC"
}
# 查询
GET device-001/_search
{
"query":{
"match": {
// 在索引里面分词后会转成大写,这里输入的是小写还能查出来的原因是使用了这个字段上的分词器进行分词,把software转成了大写,所以能查出来
"f1": "software"
}
}
}
# 这指定成默认的分词器就查不出来了
GET device-001/_search
{
"query":{
"match": {
// 指定默认分词器,查不出来
"f1": {
"query": "software",
"analyzer": "standard"
}
}
}
}
#### 5、IK中文分词配置
### 在线安装
# 选择{ES HOME}/bin/目录
# 执行如下插件安装命令
# linux版本
# elasticsearch-plugin
# instal https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
### 离线安装
# 生产环境,建议采用离线安装,安装完成一个之后,可以复制到其它节点实例。
# 下载源码
# 对应版本下载地址
# https://github.com/infinilabs/analysis-ik/archive/refs/heads/master.zip
## 编译源码
# 编译命令(需要注意pom文件中的es版本是否对应)
# mvn clean
# mvn compile
# mvn package
## 解压程序
# 编译完成后会生成一个压缩包,需要解压开
# 压缩包路径
# {IK_HOME}targetlreleaseslelasticsearch-analysis-ik-{version}.zip
#### 6、文本分词建议与经验分享
### 分词领域博大精深(NLP/LTR)
### 文本长度限制(限制大小text=100MB)
### 分词器变更,索引需要重建
### 现代化分词器:分词+统计+机器学习(目标千人千面搜索)
# analysis 官方文档
# https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.htm
# standard 默认分词器
# https://www.elastic.co/guide/en/elasticsearch/reference/8.6/analysis-standard-analyzer.htm
# 创建自定义分词器参考
# https://www.elastic.co/guide/en/elasticsearch/reference/8.6/analysis-custom-analyzer.htm
# IK 参考
# https://github.com/medcl/elasticsearch-analysis-ik