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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 需求:在检索单个中文字符时,能够匹配包含该单字的文档;在检索词语时,就不按单字进行匹配。也就是说以商品为例,如果搜索“酒”字,能够匹配到关于“啤酒”“白酒”“红酒”等所有的文档;但如果搜索“啤酒”词语,就只匹配“啤酒”。另外,在匹配时,能够全文匹配的结果排在前面,包含分词匹配的结果排在后面,并且要按匹配度与销量来排序。

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

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
23天前
|
存储 Ubuntu Oracle
在Ubuntu 14.04上安装和配置Elasticsearch的方法
在Ubuntu 14.04上安装和配置Elasticsearch的方法
23 0
|
23天前
|
存储 安全 Java
在CentOS 7上安装和配置Elasticsearch的方法
在CentOS 7上安装和配置Elasticsearch的方法
56 0
|
2月前
|
存储 数据采集 数据处理
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
84 12
|
2月前
|
存储 安全 文件存储
【elasticsearch】es6重启服务后数据消失,es6如何配置数据持久化储存
【elasticsearch】es6重启服务后数据消失,es6如何配置数据持久化储存
30 1
|
3月前
|
自然语言处理 搜索推荐
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
153 1
|
2月前
|
存储 分布式计算 网络协议
【Elasticsearch】elasticsearch.yml配置文件解读,ES配置详解
【Elasticsearch】elasticsearch.yml配置文件解读,ES配置详解
47 0
|
3月前
|
自然语言处理 数据可视化 Linux
ElasticSearch安装ik分词器_使用_自定义词典
ElasticSearch安装ik分词器_使用_自定义词典
39 1
|
3月前
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch,帮助你快速搭建起这个强大的工具
【6月更文挑战第7天】本文档详细介绍了如何在Rocky Linux 8上安装和配置Elasticsearch,首先通过添加Elasticsearch仓库并使用yum安装。接着,配置Elasticsearch,包括修改`elasticsearch.yml`、设置内存和文件描述符,以及可选的安全设置。启动Elasticsearch后,通过验证日志和测试连接确保其正常运行。文章还列举了常见问题及解决方法,如启动失败、内存不足和网络问题。按照这些步骤,用户可以在Rocky Linux 8上成功部署Elasticsearch,为数据管理与分析提供强大支持。
83 5
|
3月前
|
缓存 安全 Java
Elasticsearch—生产环境集群核心配置
Elasticsearch—生产环境集群核心配置
35 0
|
4月前
|
存储 运维 监控
Elasticsearch 配置文件 path.data 中可以配置多个数据目录的路径吗?
Elasticsearch 配置文件 path.data 中可以配置多个数据目录的路径吗?
113 7

热门文章

最新文章

下一篇
DDNS