ElasticSearch之分词及原理
文章目录
- 1. 无分词情况
- 1.1 查看ES所有索引
- 1.2 默认创建ES索引
- 1.3 向ES添加数据
- 1.4 查询数据
- 1.5 拼音查询无效果
- 2. ES分词
- 2.1 ES分词器安装
- 2.2 创建分词索引
- 2.3 插入数据
- 2.4 查询所有数据
- 2.4.1 中文查询
- 2.4.2 拼音查询
- 2.5 插入同义词
- 2.5.1 同义词拼音查询
1. 无分词情况
1.1 查看ES所有索引
http://127.0.0.1:9200/_cat/indices?v
1.2 默认创建ES索引
http://localhost:9200/medcl
1.3 向ES添加数据
http://localhost:9200/medcl/_doc
{
"name": "刘德华"
}
1.4 查询数据
# 方式一
http://localhost:9200/medcl/_search?q=刘德华
# 方式二
http://localhost:9200/medcl/_search
{
"query":{
"match":{
"name":"刘德华"
}
}
}
1.5 拼音查询无效果
2. ES分词
ES
中分词器(analyzer
)的组成包含三部分:
character filters
:在tokenizer
之前对文本进行处理。例如删除字符、替换字符tokenizer
:将文本按照一定的规则切割成词条(term
),例如:ik_smart
、ik_max_word
tokenizer filter
:将tokenizer
输出的词条做进一步处理,例如大小写转换、同义词处理、拼音处理等
注:ES中的关键字keyword属性,不会参与分词。
对于上面的三个部分解释,上面流程图,一目了然。
2.1 ES分词器安装
ES
中所有分词器的安装都是三步:
-
下载分词器压缩包,然后解压
-
放到ES安装目录的
plugin
目录下 -
重启
ES
拼音分词器:
https://github.com/medcl/elasticsearch-analysis-pinyin
ik分词器:
https://github.com/medcl/elasticsearch-analysis-ik
2.2 创建分词索引
http://localhost:9200/medcl
{
"settings": {
"analysis": {
"analyzer": {
"pinyin_analyzer": {
"tokenizer": "my_pinyin"
}
},
"tokenizer": {
"my_pinyin": {
"type": "pinyin",
"keep_separate_first_letter": false,
"keep_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"lowercase": true,
"remove_duplicated_term": true
}
}
}
},
"mappings": {
"properties": {
# 对name字段采用拼音分词
"name": {
"type": "text",
"analyzer": "pinyin_analyzer"
}
}
}
}
2.3 插入数据
http://localhost:9200/medcl/_doc
{
"name": "刘德华"
}
{
"name": "张学友"
}
{
"name": "郭富城"
}
{
"name": "黎明"
}
2.4 查询所有数据
http://localhost:9200/medcl/_search
{
"query":{
"match_all":{}
}
}
weixin_67737190: 为什么显示JS代码报错,我Javascript都不起效果
qq_50641831: 请问解决了吗,我生成的也没有pytorch_model.bin
Hello2032World: vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 track_script { chk_http_port } authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.220.110 } }
Hello2032World: vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 track_script { chk_http_port } authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.220.110 } }
Hello2032World: vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 track_script { chk_http_port } authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.220.110 } }