littlebot
Published on 2025-04-16 / 4 Visits
0

【源码】基于Elasticsearch和IK分词器的中文搜索引擎插件

项目简介

本项目是一个基于Elasticsearch和IK分词器的中文搜索引擎插件。IK分词器是针对中文的分词插件,能对中文文本进行高效、准确的分词处理,支持自定义词典和热更新功能。该插件集成在Elasticsearch中,可提供强大的中文搜索能力。

项目的主要特性和功能

  • 支持中文文本的分词处理,提供ik_smartik_max_word两种分词模式。
  • 支持用户自定义词典,可根据业务需求添加特定词汇。
  • 支持热更新功能,能在不重启Elasticsearch的情况下更新词典。
  • 支持多个Elasticsearch版本,从0.16.2到7.x。
  • 提供与Lucene的兼容适配器,方便在Lucene中进行中文搜索。

安装使用步骤

假设用户已经下载了本项目的源码文件。

1. 下载或编译插件

  • 下载预编译包
  • 创建插件目录:cd your-es-root/plugins/ && mkdir ik
  • 解压插件到目录:your-es-root/plugins/ik
  • 使用Elasticsearch插件工具安装(适用于v5.5.1及以上版本): bash 注意:请将6.3.0替换为你的Elasticsearch版本。

2. 重启Elasticsearch

安装完成后,重启Elasticsearch以加载插件。

3. 创建索引和映射

  • 创建索引bash curl -XPUT http://localhost:9200/index
  • 创建映射bash curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d' { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }'

4. 索引文档

bash curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"} '

5. 查询文档

bash curl -XPOST http://localhost:9200/index/_search -H 'Content-Type:application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } '

6. 自定义词典配置

可以在配置文件IKAnalyzer.cfg.xml中配置自定义词典和热更新词典。 ```xml

IK Analyzer 扩展配置 custom/mydict.dic;custom/single_word_low_freq.dic custom/ext_stopword.dic location http://xxx.com/xxx.dic ```

7. 热更新IK分词

通过配置远程扩展字典和停止词字典,可以实现热更新功能。热更新无需重启Elasticsearch,只需更新远程词典文件即可。 xml <entry key="remote_ext_dict">http://yoursite.com/getCustomDict</entry> <entry key="remote_ext_stopwords">http://yoursite.com/getCustomStopword</entry>

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】