elasticsearch近幾年版本更新迭代的速度之已經超出了我的想象,想着我2016,2017年還在用着2.4.x版本,最近幾年直接5.x,6.x,7.x版本了,看了一下下更新迭代的小版本時間幾乎幾個月一個小版本,簡直不要太迅速了,不過也能理解,畢竟最近幾年elasticsearch火熱程度大幅上升,所謂是需求所致。
此處我並沒有直接跳轉到最新版本,因爲我看了一下說明,2.x版本是不能直接升級到6.x,7.x的,需要由2.x ->5.x ,然後由5.x ->6.x或7.x,所以此處我首先是基於5.x版本構建,後續會先了解6.x,7.x的特性和區別,然後再基於最新版本構建。
說明:注意查看官方文檔,每個大版本所要求的的jdk版本是不一樣的,6.x、7.x應該是需要jdk11,jdk12。
環境準備:jdk1.8、elasticsearch5.6.x
一.安裝配置jdk1.8
vi /etc/profile
export MAVEN_HOME=/home/apache-maven-3.5.4
export JAVA_HOME=/usr/local/java/jdk
export PATH=$JAVA_HOME/bin:$data/erlang/bin:/home/git/bin:$MAVEN_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
二.配置ES
1.下載並上傳elasticsearch5.6.x 壓縮包,並解壓
2. 新建用戶(elasticsearch不用能root用戶啓動)
#新建組
groupadd xiaofeng
#新建用戶並指定組
useradd xiaofeng -g xiaofeng
#賦予es目錄操作權限
chown -R xiaofeng:xiaofeng /data/es
3.修改配置文件
新建目錄:/data/es/data ,數據存儲目錄
/data/es/logs ,日誌存儲目錄
打開配置文件修改相應位置:
vi /data/es/elasticsearch-5.6.1/config/elasticsearch.yml
#集羣名
cluster.name: es
#節點名,集羣節點則需要指定不同的名字
node.name: master
#數據存儲目錄
path.data: /data/es/data
#日誌文件存放目錄
path.logs: /data/es/logs
#主機
network.host: 10.0.2.19
#端口
http.port: 9200
4.切換用戶
切換到xiaofeng用戶,進入到bin目錄,然後啓動
./elasticsearch -d
三.Centos啓動elasticsearch的一些錯誤
(解決方案摘自網絡)
ERROR: [4] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [elasticsearch] is too low,increase to at least [2048]
[3]: max virtual memory areasvm.max_map_count [65530] is too low, increase to at least [262144]
[4]: system call filters failed to install; check the logs and fixyour configuration or disable system call filters at your own risk
解釋:
1.彈性搜索過程的最大文件描述符[4096]太低
2用戶[elsearch]的最大線程數[1024]太低,增加到至少[2048]
3.最大虛擬內存區域vm.max_map_count [65530]太低,增加到至少[262144]
4.系統調用篩選器未能安裝;請檢查日誌並修復配置或禁用系統調用篩選器,風險自負
根據網上找的方法一個個解決:
問題[1]:[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
修改vi /etc/security/limits.conf
文件,在後面追加
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
問題[2]:[2]: max number of threads [1024] for user [elasticsearch] is too low,increase to at least [2048]
vi /etc/security/limits.d/90-nproc.conf
把 * soft nproc 1024 修改成 * soft nproc 2048
問題[3]:[3]: max virtual memory areasvm.max_map_count [65530] is too low, increase to at least [262144]
使用root用戶vi /etc/sysctl.conf
添加下面配置然後執行 sysctl -p
vm.max_map_count=655360
問題[4]:[4]: system call filters failed to install; check the logs and fixyour configuration or disable system call filters at your own risk
這個問題網上說是因爲ES啓動時默認會啓動一個檢測,而當前系統上不能通過這個檢測,直接導致啓動失敗
解決方法爲修改ES的配置文件(elasticsearch.yml),使其不檢測,在Memory添加這兩條配置
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
修改完以上配置,重啓es,在瀏覽器輸入http://10.0.2.19:9200/
四:安裝elasticsearch-head插件
head插件的作用我就不做詳細介紹了,就是一個可視化插件。
如上所述elasticsearch從5.x版本後就不支持sudo elasticsearch/bin/plugin install mobz/elasticsearch-head 方式安裝了。
1.下載head插件,進入/data/es 目錄 wget https://github.com/mobz/elasticsearch-head/archive/master.zip
2.安裝nodejs,(依賴nodejs啓動)
因爲head插件本質上還是一個nodejs的工程,因此需要安裝node
yum安裝:yum install -y nodejs
源碼安裝:
下載源碼:http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz
解壓:tar -zvxf node-v0.10.26.tar.gz
編譯及安裝:make && make install
驗證是否安裝配置成功:node -v
3.安裝grunt
因爲head插件需要使用grunt啓動,所以需要安裝grunt,grunt是一個很方便的構建工具,可以進行打包壓縮、測試、執行等等的工作
進入head的解壓目錄
安裝grunt npm install -g grunt-cli 會生成一個node_modules文件夾
然後修改vi Gruntfile.js文件:增加hostname屬性,設置爲*
修改 vi _site/app.js
文件:修改head的連接地址:把localhsot改成本地地址
(注:如果出現node 命令問題則直接執行npm install)
4.啓動head服務
啓動之前需要進行跨域配置,否則head插件界面是無法訪問es服務的。
打開elasticsearch.yml,增加如下配置然後重啓es服務:
http.cors.enabled: true
http.cors.allow-origin: "*"
配置完成後使用 grunt server &
後臺啓動 ,訪問瀏覽器:http://10.0.2.19:9100
看到master節點,以及集羣狀態爲gree即說明連接成功。
五:集成IK中文分詞
安裝ik分詞插件。
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.1/elasticsearch-analysis-ik-5.6.1.zip
注意版本一定要對應,比如我使用的是es5.6.1,則ik分詞的版本也爲5.6.1
然後重啓es服務即可。
注意:需要重建索引,並在相關屬性字段中加入ik索引配置(此處使用的是ik_smart,也可以使用ik_max_word,區別爲籤前者粗粒度後者細粒度,具體查看官方文檔)。
示例如下:
{
"settings": {
"number_of_replicas": 0
},
"mappings": {
"house": {
"dynamic": false,
"properties": {
"houseId": {
"type": "long"
},
"title": {
"type": "text",
"index": "analyzed",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"price": {
"type": "integer"
},
"area": {
"type": "integer"
},
"createTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"lastUpdateTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"cityEnName": {
"type": "keyword"
},
"suggest": {
"type": "completion"
},
"location": {
"type": "geo_point"
}
}
}
}
}
測試效果如下: