docker部署Ik热分词插件

  1. 启动容器 (默认内存是2g)
    docker run -d -p 9201:9200 -p 9300:9300 -e "discovery.type=single-node" ES_JAVA_OPTS="-Xms666m -Xms666m" container_id

  2. 安装IK分词
    ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.1/elasticsearch-analysis-ik-7.10.1.zip

或者文件拷贝到服务器:
scp elasticsearch-analysis-ik-7.10.1.zip root@xxxxxx:~
docker cp elasticsearch-analysis-ik-7.10.1.zip container_id:/usr/share/elasticsearch/plugins/

  1. 词典配置
    /usr/share/elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
 	<!--用户可以在这里配置远程扩展字典 -->
	<entry key="remote_ext_dict">location</entry>
 	<!--用户可以在这里配置远程扩展停止词字典-->
	<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

如配置热分词词典remote_ext_dict ,结合nginx:
其中 location 是指一个 url,比如 http://yoursite.com/getCustomDict,该请求只需满足以下两点即可完成分词热更新。
该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。
该 http 请求返回的内容格式是一行一个分词,换行符用 即可。
ES会定时拉一下看下文件是否有变化,有则重新配置分词

    location /my_dic.txt {
        alias /xxx/path_to_dic.txt;
    }
指定分词:
{
            "settings": {
                "analysis": {
                    "analyzer": {
                        "douhao": {
                            "type": "pattern",
                            "pattern": ","
                        }
                    }
                }
            },
            "mappings": {
                "properties": {
                    "title": {
                        "type": "text",
                        "analyzer": "ik_max_word",
                        "search_analyzer": "ik_smart"
                    },
                    "subway": {
                        "type": "text",
                        "analyzer": "douhao",
                        "search_analyzer": "douhao"
                    },
                    "create_time": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss",
                    }
                }
            }
        }

效果:

原文地址:https://www.cnblogs.com/donghaoblogs/p/14299731.html