ElasticSearch配置IK灵活匹配单个汉字与词组

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 需求:在检索单个中文字符时,能够匹配包含该单字的文档;在检索词语时,就不按单字进行匹配。也就是说以商品为例,如果搜索“酒”字,能够匹配到关于“啤酒”“白酒”“红酒”等所有的文档;但如果搜索“啤酒”词语,就只匹配“啤酒”。另外,在匹配时,能够全文匹配的结果排在前面,包含分词匹配的结果排在后面,并且要按匹配度与销量来排序。

1. 环境说明

  • elasticsearch7.9.3
  • elasticsearch-analysis-ik-7.9.3
  • kibana7.9.3(与此需求无关)

2. 分析思路

  • 由于es在存储数据时如果使用ik分词器, 进行如下配置:
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5  },
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart", 
"index": true,
"store": false      }
      }
}
  • 在分词过程中, 默认IK分词器只会处理分, 但是单个字是不会变成term储存进倒排表的
  • 所以如果要做单个字的全文检索, 就需要增加额外字典
  • 对检索进行优化, 对检索词会进行最大粒度分词, 比如: 在检索:"手机壳"的时候, 就不会将"手机壳"拆分为"手机"和"手机壳"等, 避免搜索手机壳的时候出现手机的结果

3. analysis-ik配置

  • 修改配置文件elasticsearch-7.9.3\plugins\elasticsearch-analysis-ik-7.9.3\config
  • 配置中相对路径都是以config下路径
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd"><properties><comment>IKAnalyzer扩展配置</comment><!--用户可以在这里配置自己的扩展字典--><entrykey="ext_dict">extra_single_word.dic</entry><!--用户可以在这里配置自己的扩展停止词字典--><entrykey="ext_stopwords"></entry><!--用户可以在这里配置远程扩展字典--><!--<entrykey="remote_ext_dict">words_location</entry>--><!--用户可以在这里配置远程扩展停止词字典--><!--<entrykey="remote_ext_stopwords">words_location</entry>--></properties>

4. 重启ES并重建索引

再一次建立以下索引:

{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5  },
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart", 
"index": true,
"store": false      }
      }
}

此时, 再存入的数据会根据IK分词器+额外字典(单字字典)进行分词。


kibana测试分词

POST_analyze{
"analyzer": "ik_max_word",
"text": ["我们是共产主义接班人。"]
}

分词结果

{
"tokens" : [
    {
"token" : "我们",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0    },
    {
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_WORD",
"position" : 1    },
    {
"token" : "们",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2    },
    {
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3    },
    {
"token" : "共产主义",
"start_offset" : 3,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4    },
    {
"token" : "共产",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 5    },
    {
"token" : "共",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 6    },
    {
"token" : "产",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 7    },
    {
"token" : "主义",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 8    },
    {
"token" : "主",
"start_offset" : 5,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 9    },
    {
"token" : "义",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 10    },
    {
"token" : "接班人",
"start_offset" : 7,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 11    },
    {
"token" : "接班",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 12    },
    {
"token" : "接",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 13    },
    {
"token" : "班",
"start_offset" : 8,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 14    },
    {
"token" : "人",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 15    }
  ]
}

参考文章: https://blog.csdn.net/nazeniwaresakini/article/details/104220237

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
相关文章
|
12月前
|
存储 缓存 固态存储
优化Elasticsearch 硬件配置
优化Elasticsearch 硬件配置
504 5
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
247 5
|
12月前
|
缓存 监控 安全
优化Elasticsearch 集群配置
优化Elasticsearch 集群配置
314 4
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
450 5
|
12月前
|
监控 负载均衡 安全
Elasticsearch集群配置优化
Elasticsearch集群配置优化
260 1
|
测试技术 API 开发工具
ElasticSearch的IK分词器
ElasticSearch的IK分词器
226 7
|
运维 监控 数据可视化
大数据-171 Elasticsearch ES-Head 与 Kibana 配置 使用 测试
大数据-171 Elasticsearch ES-Head 与 Kibana 配置 使用 测试
415 1
|
存储 数据采集 数据处理
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
612 12
|
存储 Ubuntu Oracle
在Ubuntu 14.04上安装和配置Elasticsearch的方法
在Ubuntu 14.04上安装和配置Elasticsearch的方法
197 0
|
存储 安全 Java
在CentOS 7上安装和配置Elasticsearch的方法
在CentOS 7上安装和配置Elasticsearch的方法
1021 0