1. 程式人生 > 實用技巧 >docker部署Ik熱分詞外掛

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 請求返回的內容格式是一行一個分詞,換行符用 \n 即可。
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",
                    }
                }
            }
        }

效果: