# elasticsearch-analysis-ansj **Repository Path**: itxiaodz/elasticsearch-analysis-ansj ## Basic Information - **Project Name**: elasticsearch-analysis-ansj - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-31 - **Last Updated**: 2025-07-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # elasticsearch-analysis-ansj: elasticsearch 的中文分词插件 # 前言 elasticsearch-analysis-ansj 是一个基于 [ansj](https://github.com/NLPchina/ansj_seg) 分词算法 的 elasticsearch 的中文分词插件。 # 编译 ```bash mvn package ``` 编译成功后,将会生成打包好的插件压缩包:`target/releases/elasticsearch-analysis-ansj-<版本号>-release.zip`。 # 安装 ## 安装命令 在 es 安装目录下执行下面的命令安装插件: ```bash ./bin/elasticsearch-plugin install file:///<你的路径>/elasticsearch-analysis-ansj-<版本号>-release.zip ``` > 安装完成后,会生成一个默认的配置文件: `/config/elasticsearch-analysis-ansj/ansj.cfg.yml`,根据需要修改此文件即可。 ## 测试 安装完成后,启动 es 集群。通过以下方式测试是否安装正确: **方法一:** 通过 `kibana` 执行 `GET /_cat/ansj?text=中国&type=index_ansj` 命令,测试 `index_ansj` 分词器,返回内容如下: ```json { "result": [ { "name": "中国", "nature": "ns", "offe": 0, "realName": "中国", "synonyms": null }, { "name": "中", "nature": "f", "offe": 0, "realName": "中", "synonyms": null }, { "name": "国", "nature": "n", "offe": 1, "realName": "国", "synonyms": null } ] } ``` **方法二:** 通过 `kibana` 执行 `GET /_cat/ansj/config` 命令,获取配置文件内容如下: ```json { "ambiguity": [ "ambiguity" ], "stop": [ "stop" ], "synonyms": [ "synonyms" ], "crf": [ "crf" ], "isQuantifierRecognition": "true", "isRealName": "false", "isNumRecognition": "true", "isNameRecognition": "true", "dic": [ "dic" ] } ``` # 使用 + 第一步:创建索引 ```text PUT /test_index?pretty { "settings" : { "index" : { "number_of_shards" : 16, "number_of_replicas" : 1, "refresh_interval":"5s" } }, "mappings" : { "properties" : { "test_field": { "type": "text", "analyzer": "index_ansj", "search_analyzer": "query_ansj" } } } } ``` > **说明:** > + `test_index`: 用于测试的索引名称; > + `test_field`: 用于测试的字段; > + 指定字段的索引分词器为: `index_ansj` ; > + 指定字段的搜索分词器为: `query_ansj` ; 测试索引配置是否正确: ```text POST /test_index/_analyze { "field": "test_field", "text": "中国" } ``` + 第二步:添加数据 ```text PUT test_index/_bulk?refresh {"create":{ }} { "test_field" : "中国" } {"create":{ }} { "test_field" : "中华人民共和国" } {"create":{ }} { "test_field" : "中国有56个民族" } {"create":{ }} { "test_field" : "中国是社会主义国家" } ``` + 第三步:执行搜索 ```text GET test_index/_search { "query": { "match": { "test_field": { "query": "中国" } } } } ``` > **注意:** > + 上述操作语句都是在 `kibana` 的 `dev_tools` 里执行的; > + 上述操作语句仅在 es `8.x` 版本上测试过,其它版本请根据实际情况调整。 # 插件功能 安装插件后,在 es 集群中会增加以下功能: **三个 analyzer:** + index_ansj (建议索引使用) + query_ansj (建议搜索使用) + dic_ansj **三个 tokenizer:** + index_ansj (建议索引使用) + query_ansj (建议搜索使用) + dic_ansj **http 接口:** + /_cat/ansj: 执行分词 + /_cat/ansj/config: 显示全部配置 + /_ansj/flush/config: 刷新全部配置 + /_ansj/flush/dic: 更新全部词典。包括用户自定义词典,停用词典,同义词典,歧义词典,crf # 配置文件 ## 配置文件格式 ```yaml ansj: #默认参数配置 isNameRecognition: true #开启姓名识别 isNumRecognition: true #开启数字识别 isQuantifierRecognition: true #是否数字和量词合并 isRealName: false #是否保留真实词语,建议保留false #用户自定词典配置 #dic: default.dic #也可以写成 file://default.dic , 如果未配置dic,则此词典默认加载 # http方式加载 #dic_d1: http://xxx/xx.dic # jar中文件加载 #dic_d2: jar://org.ansj.dic.DicReader|/dic2.dic # 从数据库中加载 #dic_d3: jdbc://jdbc:mysql://xxxx:3306/ttt?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull|username|password|select name as name,nature,freq from dic where type=1 # 从自定义类中加载,YourClas extends PathToStream #dic_d3: class://xxx.xxx.YourClas|ohterparam #过滤词典配置 #stop: http,file,jar,class,jdbc 都支持 #stop_key1: ... #歧义词典配置 #ambiguity: http,file,jar,class,jdbc 都支持 #ambiguity_key1: ... #同义词词典配置 #synonyms: http,file,jar,class,jdbc 都支持 #synonyms_key1: ... ``` ## 配置文件示例 ### 使用本地文件词库 ```yaml ansj: # 开启姓名识别 isNameRecognition: false # 开启数字识别 isNumRecognition: true # 是否数字和量词合并 isQuantifierRecognition: false # 是否保留真实词语 isRealName: false # 词典 dic: file:///data/elasticsearch-dic/ansj/main.dic # 停词(过滤词)词典 stop: file:///data/elasticsearch-dic/ansj/stop.dic # 歧义词词典配置 ambiguity: file:///data/elasticsearch-dic/ansj/ambiguity.dic # 同义词词典配置 synonyms: file:///data/elasticsearch-dic/ansj/synonyms.dic ``` ### 使用 HTTP 协议加载词库 ```yaml ansj: # 开启姓名识别 isNameRecognition: false # 开启数字识别 isNumRecognition: true # 是否数字和量词合并 isQuantifierRecognition: false # 是否保留真实词语 isRealName: false # 词典 dic: http://example.com/elasticsearch-dic/ansj/main.dic # 停词(过滤词)词典 stop: http://example.com/elasticsearch-dic/ansj/stop.dic # 歧义词词典配置 ambiguity: http://example.com/elasticsearch-dic/ansj/ambiguity.dic # 同义词词典配置 synonyms: http://example.com/elasticsearch-dic/ansj/synonyms.dic ``` # 插件版本与 ES 版本的对应关系 | plugin | elasticsearch| |---------| -----: | | 1.0.0 | 0.90.2 | | 1.x | 1.x | | 2.1.1 | 2.1.1 | | 2.3.1 | 2.3.1 | | 2.3.2 | 2.3.2 | | 2.3.3 | 2.3.3 | | 2.3.4 | 2.3.4 | | 2.3.5 | 2.3.5 | | 2.4.0 | 2.4.0 | | 2.4.1 | 2.4.1 | | 2.4.2 | 2.4.2 | | 2.4.3 | 2.4.3 | | 2.4.4 | 2.4.4 | | 2.4.5 | 2.4.5 | | 2.4.6 |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| 7.17.9 | 7.17.9 | | 7.17.10 | 7.17.10 | | 7.17.11 | 7.17.11 | | 7.17.12 | 7.17.12 | | 7.17.13 | 7.17.13 | | 7.17.14 | 7.17.14 | | 7.17.15 | 7.17.15 | | 7.17.16 | 7.17.16 | | 7.17.17 | 7.17.17 | | 7.17.18 | 7.17.18 | | 7.17.19 | 7.17.19 | | 7.17.20 | 7.17.20 | | 7.17.21 | 7.17.21 | | 7.17.22 | 7.17.22 | | 7.17.23 | 7.17.23 | | 7.17.24 | 7.17.24 | | 7.17.25 | 7.17.25 | | 7.17.26 | 7.17.26 | | 7.17.27 | 7.17.27 | | 7.17.28 | 7.17.28 | | 8.3.3 | 8.3.3 | | 8.5.3 | 8.5.3 | | 8.6.0 | 8.6.0 | | 8.6.1 | 8.6.1 | | 8.6.2 | 8.6.2 | | 8.7.0 | 8.7.0 | | 8.7.1 | 8.7.1 | | 8.8.0 | 8.8.0 | | 8.8.1 | 8.8.1 | | 8.8.2 | 8.8.2 | | 8.9.0 | 8.9.0 | | 8.9.1 | 8.9.1 | | 8.9.2 | 8.9.2 | | 8.10.0 | 8.10.0 | | 8.10.1 | 8.10.1 | | 8.10.2 | 8.10.2 | | 8.10.3 | 8.10.3 | | 8.10.4 | 8.10.4 | | 8.11.0 | 8.11.0 | | 8.11.1 | 8.11.1 | | 8.11.2 | 8.11.2 | | 8.11.3 | 8.11.3 | | 8.11.4 | 8.11.4 | | 8.12.0 | 8.12.0 | | 8.12.1 | 8.12.1 | | 8.12.2 | 8.12.2 | | 8.13.0 | 8.13.0 | | 8.13.1 | 8.13.1 | | 8.13.2 | 8.13.2 | | 8.13.3 | 8.13.3 | | 8.13.4 | 8.13.4 | | 8.14.0 | 8.14.0 | | 8.14.1 | 8.14.1 | | 8.14.2 | 8.14.2 | | 8.14.3 | 8.14.3 | | 8.15.0 | 8.15.0 | | 8.15.1 | 8.15.1 | | 8.15.2 | 8.15.2 | | 8.15.3 | 8.15.3 | | 8.15.4 | 8.15.4 | | 8.15.5 | 8.15.5 | | 8.16.0 | 8.16.0 | | 8.16.1 | 8.16.1 | | 8.16.2 | 8.16.2 | | 8.16.3 | 8.16.3 | | 8.17.0 | 8.17.0 | | 8.17.1 | 8.17.1 | | 8.17.2 | 8.17.2 | | 8.17.3 | 8.17.3 | | 8.17.4 | 8.17.4 | | 8.17.5 | 8.17.5 | | 8.17.6 | 8.17.6 | | 8.18.0 | 8.18.0 | | 8.18.1 | 8.18.1 | | 9.0.0 | 9.0.0 | | 9.0.1 | 9.0.1 | | 9.0.2 | 9.0.2 | | 9.0.3 | 9.0.3 | # 版权 `elasticsearch-analysis-ansj` is licenced under the Apache License Version 2.0. See the [LICENSE](https://github.com/NLPchina/elasticsearch-analysis-ansj/blob/master/LICENSE) file for details.