文章目录
- 分词器
-
- 1.字符过滤器
- 2.自定义分词器
- 3.中文分词器
-
-
- 1.下载ik分词器7.10.0版本(跟es对应)
- 2.应用ik分词器
-
- 1.进入插件目录下创建一个ik目录
- 2.将下载的压缩包上传上去
- 3.安装unzip并解压
- 4.重启es
-
- [1.首先查找到es,然后kill -9](#1.首先查找到es,然后kill -9)
- 2.重新启动三个节点
- 3.使用head插件查看状态
- 3.测试ik分词器是否生效
- 4.ik分词器的配置文件
-
- 索引的批量操作
- 模糊搜索和智能搜索推荐
分词器
1.字符过滤器
1.介绍

2.过滤html标签

3.mappings过滤规则(屏蔽非文明用语)

4.正则替换

2.自定义分词器
1.代码
java
PUT /test_index
{
"settings": {
"analysis": {
"char_filter": {
"my_char_filter": {
"type": "html_strip" // 自定义字符过滤器,使用 html_strip 类型,剥离 HTML 标签
}
},
"filter": {
"my_stopword": {
"type": "stop", // 自定义过滤器,移除停用词
"stopwords": ["is", "a"] // 指定停用词列表,包括 "is" 和 "a"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern", // 自定义分词器,基于正则表达式进行分词
"pattern": "[,\\.?\\s]+" // 正则表达式,将逗号、句号、问号和空格作为分隔符
}
},
"analyzer": {
"my_analysis": {
"type": "custom", // 自定义分析器类型
"char_filter": ["my_char_filter"], // 使用自定义字符过滤器 "my_char_filter"
"tokenizer": "my_tokenizer", // 使用自定义分词器 "my_tokenizer"
"filter": ["my_stopword"] // 使用自定义停用词过滤器 "my_stopword"
}
}
}
}
}
2.查询
java
GET /test_index/_analyze
{
"analyzer": "my_analysis",
"text": "<p>This is a test, isn't it amazing?</p>"
}

3.中文分词器
1.下载ik分词器7.10.0版本(跟es对应)
https://release.infinilabs.com/analysis-ik/stable/


2.应用ik分词器
1.进入插件目录下创建一个ik目录
sh
cd /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins && mkdir ik
2.将下载的压缩包上传上去
sh
cd ik

3.安装unzip并解压
1.安装
sh
yum install unzip
2.解压
sh
unzip elasticsearch-analysis-ik-7.10.0.zip
3.删除zip
sh
rm -f elasticsearch-analysis-ik-7.10.0.zip
4.将这个ik目录分别复制给另外三台节点
sh
cp -r /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins/ik /usr/local/ElasticSearch/node2/plugins && cp -r /usr/local/ElasticSearch/elasticsearch-7.10.0/plugins/ik /usr/local/ElasticSearch/node3/plugins
4.重启es
1.首先查找到es,然后kill -9
sh
ps -aux | grep elasticsearch
2.重新启动三个节点
sh
su elasticsearch
sh
cd /usr/local/ElasticSearch/elasticsearch-7.10.0/bin/ && ./elasticsearch -d && cd /usr/local/ElasticSearch/node2/bin/ && ./elasticsearch -d && cd /usr/local/ElasticSearch/node3/bin/ && ./elasticsearch -d
3.使用head插件查看状态

3.测试ik分词器是否生效
1.创建索引并应用ik分词器

java
PUT /test_index
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart"
},
"description": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
2.插入数据
java
POST /test_index/_doc
{
"title": "Elasticsearch 是一个分布式搜索引擎",
"description": "IK 分词器支持中文分词,并且可以用于全文检索"
}
3.查看分词结果
java
POST /test_index/_analyze
{
"field": "title",
"text": "Elasticsearch 是一个分布式搜索引擎"
}

java
POST /test_index/_analyze
{
"field": "description",
"text": "IK 分词器支持中文分词,并且可以用于全文检索"
}

4.ik分词器的配置文件

索引的批量操作
1.基于mget的批量查询
1.环境搭建
java
PUT /new_index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
}
java
POST /new_index/_doc/1
{
"name": "Alice",
"age": 25
}
POST /new_index/_doc/2
{
"name": "Bob",
"age": 30
}
POST /new_index/_doc/3
{
"name": "Charlie",
"age": 22
}
2.根据id进行批量查询
java
GET /new_index/_mget
{
"ids": [1,2]
}

3.还可以在批量查询时指定自己想要的字段
java
GET /new_index/_mget
{
"docs": [
{
"_id": 1,
"_source": {
"include": [
"name"
]
}
},
{
"_id": 2,
"_source": {
"exclude": [
"name"
]
}
}
]
}

2.文档的四种操作类型
1.介绍

2._create 创建数据

3.删除数据

4._search 查询数据

5._update 更新数据

3._bulk

模糊搜索和智能搜索推荐
1.基本介绍

2.前缀搜索
1.基础数据
java
#prefix: 前缀搜索
POST /my_index/_bulk?filter_path=items.*.error
{ "index": { "_id": "1" } }
{ "text": "城管打电话喊商贩去摆摊摊" }
{ "index": { "_id": "2" } }
{ "text": "笑果文化回应老农去摆摊" }
{ "index": { "_id": "3" } }
{ "text": "老农耗时17年种出椅子树" }
{ "index": { "_id": "4" } }
{ "text": "夫妻结婚30多年AA制,被城管抓" }
{ "index": { "_id": "5" } }
{ "text": "黑人见义勇为阻止抢劫反被铐住" }
2.代码
java
{
"query": {
"prefix": {
"text": {
"value": "笑"
}
}
}
}
3.原理
前缀匹配是对倒排索引进行匹配的,而不是整个字段
3.通配符搜索
1.基本介绍
通配符也是匹配的倒排索引
2.代码

4.正则匹配

5.模糊查询
1.介绍

2.代码
注意:fuzzy是不分词的,match是分词的

6.短语前缀
1.介绍

2.代码

7.edge_ngram
java
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_edge_ngram": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 5
}
},
"analyzer": {
"my_ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"my_edge_ngram"
]
}
}
}
},
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "my_ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}