ElasticSearch
Docker部署ElasticSearch鏡像
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.6.2
ElasticSearch的默認端口是9200,我們把宿主環境9200端口映射到Docker容器中的9200端口,就可以訪問到Docker容器中的ElasticSearch服務了,同時我們把這個容器命名爲elasticsearch。
- 9300:集羣節點間通訊接口
- 9200:客戶端訪問接口
- discovery.type=single-node :表示單節點啓動
驗證是否啓動成功
訪問 http://127.0.0.1:9200,返回以下內容,說明部署成功
{
"name" : "c10333fb1897",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "JoD93JHsS9-pbCQqmhBx2g",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
ik分詞器
Elasticsearch默認也能對中文進行分詞。但是是按照每個字進行分詞的,這種在實際應用裏肯定達不到想要的效果,因此,我們安裝ik分詞器來進行中文分詞。
ES內置的Analyzer分析器
ES自帶了許多內置的Analyzer分析器,無需配置就可以直接在index中使用:
- 標準分詞器(standard):以單詞邊界切分字符串爲terms,根據Unicode文本分割算法。它會移除大部分的標點符號,小寫分詞後的term,支持停用詞。
- 簡單分詞器(simple):該分詞器會在遇到非字母時切分字符串,小寫所有的term。
- 空格分詞器(whitespace):遇到空格字符時切分字符串,
- 停用詞分詞器(stop):類似簡單分詞器,同時支持移除停用詞。
- 關鍵詞分詞器(keyword):無操作分詞器,會輸出與輸入相同的內容作爲一個single term。
- 模式分詞器(pattern):使用正則表達式講字符串且分爲terms。支持小寫字母和停用詞。
- 語言分詞器(language):支持許多基於特定語言的分詞器,比如english或french。
- 簽名分詞器(fingerprint):是一個專家分詞器,會產生一個簽名,可以用於去重檢測。
- 自定義分詞器:如果內置分詞器無法滿足你的需求,可以自定義custom分詞器,根據不同的character filters,tokenizer,token filters的組合 。例如IK就是自定義分詞器。
安裝ik分詞器
#首先需要啓動elasticsearch容器
#然後進入elasticsearch容器
docker exec -it elasticsearch /bin/bash
#列出所有文件
ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
#進入plugin目錄
cd plugins/
#安裝ik分詞器,注意和elasticsearch版本對應
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
#重啓下容器
docker restart elasticsearch
#查看當前目錄下文件
ls
analysis-ik
cd analysis-ik/
ls
commons-codec-1.9.jar commons-logging-1.2.jar elasticsearch-analysis-ik-7.6.2.jar httpclient-4.5.2.jar httpcore-4.4.4.jar plugin-descriptor.properties plugin-security.policy
IK支持兩種分詞模式:
- ik_max_word: 會將文本做最細粒度的拆分,會窮盡各種可能的組合
- ik_smart: 會做最粗粒度的拆分
驗證分詞插件是否安裝成功
增加一個叫test001的索引
curl -X PUT http://localhost:9200/test001
成功返回 {“acknowledged”:true,“shards_acknowledged”:true,“index”:“test001”}
使用ik_smart分詞
curl -X POST \
'http://127.0.0.1:9200/test001/_analyze?pretty=true' \
-H 'Content-Type: application/json' \
-d '{"text":"我們是軟件工程師","tokenizer":"ik_smart"}'
得到如下結果
{
"tokens" : [
{
"token" : "我們",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "軟件",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "工程師",
"start_offset" : 5,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 3
}
]
}
使用ik_max_word分詞
curl -X POST \
'http://127.0.0.1:9200/test001/_analyze?pretty=true' \
-H 'Content-Type: application/json' \
-d '{"text":"我們是軟件工程師","tokenizer":"ik_max_word"}'
得到如下結果:
{
"tokens" : [
{
"token" : "我們",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "軟件工程",
"start_offset" : 3,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "軟件",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "工程師",
"start_offset" : 5,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "工程",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "師",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 6
}
]
}
可以看到ik_max_word分詞更精細。
自定義分詞詞典
新建自己的詞典只需要簡單幾步就可以完成:
- 在
elasticsearch-7.2.6/config/analysis-ik/
目錄增加一個my.dic
.dic
爲詞典文件,其實就是簡單的文本文件,詞語與詞語直接需要換行。注意是UTF8編碼。
[root@c10333fb1897 elasticsearch]# cd config/
[root@c10333fb1897 config]# ls
analysis-ik jvm.options roles.yml
elasticsearch.keystore log4j2.properties users
elasticsearch.yml role_mapping.yml users_roles
[root@c10333fb1897 config]# cd analysis-ik/
[root@c10333fb1897 analysis-ik]# ls
IKAnalyzer.cfg.xml extra_single_word_low_freq.dic quantifier.dic
extra_main.dic extra_stopword.dic stopword.dic
extra_single_word.dic main.dic suffix.dic
extra_single_word_full.dic preposition.dic surname.dic
[root@c10333fb1897 analysis-ik]# touch my.dic
[root@c10333fb1897 analysis-ik]# echo 朝陽公園>my.dic
[root@c10333fb1897 analysis-ik]# cat my.dic
朝陽公園
- 修改
elasticsearch-7.2.6/config/analysis-ik/IKAnalyzer.cfg.xml
文件
[root@c10333fb1897 analysis-ik]# vi 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">my.dic</entry>
<!--用戶可以在這裏配置自己的擴展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶可以在這裏配置遠程擴展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用戶可以在這裏配置遠程擴展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
- 擴展停止詞字典,這個是用來輔助斷句的,也就是IK分詞器遇到這些詞就認爲前面的詞語不會與這些詞構成詞語。
- 遠程詞典,遠程詞典的好處是支持熱更新。詞典格式和本地的一致,都是一行一個分詞(換行符用
\n
)。
-
重啓ES,進行測試,測試結果如下
slimshady@MacBook-Pro ~ % curl -X POST \ > 'http://127.0.0.1:9200/test001/_analyze?pretty=true' \ > -H 'Content-Type: application/json' \ > -d '{"analyzer": "ik_smart","text": "去朝陽公園"}' { "tokens" : [ { "token" : "去", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "朝陽公園", "start_offset" : 1, "end_offset" : 5, "type" : "CN_WORD", "position" : 1 } ] }
說明自定義詞典生效了。如果有多個詞典,使用英文分號隔開:
<entry key="ext_dict">my.dic;custom/single_word_low_freq.dic</entry>
Kibana
Docker部署kibana鏡像
docker run --name kibana --link=elasticsearch -p 5601:5601 docker.elastic.co/kibana/kibana:7.6.2
- –link=elasticsearch:連接elasticsearch
- –name kibana:命名爲kibana
驗證是否啓動成功
訪問 http://127.0.0.1:5601,進入以下頁面,說明部署成功