1. 程式人生 > >es+ik+head+叢集

es+ik+head+叢集

前置工作

當前伺服器為CentOS7+ 64bit

新建 elasticsearch使用者,ES無法用root啟動

useradd elasticsearch

groupadd elasticsearch

chown -R elasticsearch:elasticsearch /XXX目錄

獲取elasticsearch,本文用的v2.4.0 找到要獲取的版本

(https://www.elastic.co/downloads/past-releases)

ES已經有5.x版本,然而Spring data目前還只支援ES2.x版本,Springdata支援ES版本說明

(https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix)

瞭解ES與mysql的對應關係 

index –> DB 

type –> Table 

Document –> row

安裝head兩種方法

一. elasticsearch5.0版本以下HEAD安裝方式

1), 直接安裝

./bin/plugin install mobz/elasticsearch-head

2),zip包安裝

1. https://github.com/mobz/elasticsearch-head下載zip 解壓 2. 建立elasticsearch-2.4.0\plugins\head檔案 3. 將解壓後的elasticsearch-head-master資料夾下的檔案copy到head 4. 執行es

3), 安裝其他外掛

${ES_HOME}/bin/plugin --install lukas-vlcek/bigdesk

# 安裝完成訪問:http://localhost:9200/_plugin/bigdesk/#nodes

${ES_HOME}/bin/plugin -install royrusso/elasticsearch-HQ

# 安裝完成訪問:http://localhost:9200/_plugin/HQ/

${ES_HOME}/bin/plugin -install lmenezes/elasticsearch-kopf

# 安裝完成訪問:http://localhost:9200/_plugin/kopf/#!/cluster

官方的文件: 

二.elasticsearch5.0以下版本可以直接在elasticsearch目錄下執行對應的命令安裝head外掛,但是5.0以上不支援直接安裝,所以需要藉助其他方法安裝head外掛,要和elasticsearch的安裝目錄區別開

ES安裝

cd /opt

tar -zxvf elasticsearch-2.4.0.tar.gz

mv elasticsearch-2.4.0 /usr/es/elasticsearch

vim /usr/es/elasticsearch/config/elasticsearch/elasticsearch.yml

 

mkdir /usr/es/data

mkdir /usr/es/logs

IK外掛安裝

可以根據參考官網查詢elasticsearch版本對應的ik版本和安裝ik的教程

中找到elasticsearch的ik對應版本,我這裡是2.4.0,對應版本為v1.10.0

cd /usr/es/elasticsearch/plugins/

mkdir ik(以root身份)

cd ik

unzip elasticsearch-analysis-ik-1.10.0.zip

mv elasticsearch-analysis-ik-1.10.0.zip /opt/elasticsearch-analysis-ik-1.10.0.zip

如果es啟動了需要重啟才能生效

測試ik以下命令報錯

上面這個rest請求在2.4.0版本的es伺服器上會執行失敗,原因是es 2.3.4上面還沒有text這種型別,"type":"text" 改為 "type":"String"即可

HEAD外掛安裝

如果是5.0版本以上那麼

cd /opt

mv elasticsearch-head-master /usr/es/elasticsearch-head-master

如果是5.0以下版本那麼(參考上面說的幾種方式)

cd /usr/es/elasticsearch

./bin/plugin install mobz/elasticsearch-head

增加hostname,設定所有主機都能訪問

vim Gruntfile.js

 

vim _site/app.js

 

將localhost換成ip,我用的2.4.0用./bin/plugin install mobz/elasticsearch-head方式下載的head外掛該js裡沒有找到該方法,就忽略了

安裝node之前如果有自帶工具,先解除安裝之前的node和npm

1.自帶工具刪除

yum remove nodejs npm -y

2.手動刪除殘留

進入 /usr/local/lib 刪除所有 node 和 node_modules資料夾

進入 /usr/local/include 刪除所有 node 和 node_modules 資料夾

進入 /usr/local/bin 刪除 node 的可執行檔案node和npm

檢查 ~ 資料夾裡面的"local" "lib" "include" 資料夾,然後刪除裡面的所有 "node" 和 "node_modules" 資料夾

安裝node

cd /opt

mkdir node

cd node

wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz

tar -zxvf node-v4.4.7-linux-x64.tar.gz

配置環境變數

vim /etc/profile

#node

export NODE_HOME=/opt/node/node-v4.4.7-linux-x64

export PATH=$PATH:$NODE_HOME/bin

export NODE_PATH=$NODE_HOME/lib/node_modules

source /etc/profile

配置環境目錄

ln -s /opt/node/node-v4.4.7-linux-x64/bin/node /usr/local/bin/node

ln -s /opt/node/node-v4.4.7-linux-x64/bin/npm /usr/local/bin/npm

在head目錄下執行

npm install -g grunt-cli 或 npm install -g grunt //執行後會生成node_modules資料夾

如果5.0以上

cd /usr/es/elasticsearch-head-master

npm install -g grunt-cli

如果5.0以下

cd /usr/es/elasticsearch/plugins/head/

npm install -g grunt-cli

然後後臺執行

nohup grunt server &

如果出現

 

說明Gruntfile.js缺少對應的依賴,可以忽略命令後加上 --force

否則可以去下載對應的module,命令如下:

npm install grunt-contrib-clean

npm install grunt-contrib-concat

npm install grunt-contrib-watch

npm install grunt-contrib-connect

npm install grunt-contrib-copy

npm install grunt-contrib-jasmine

附Grunt常用外掛說明:

1)grunt-contrib-uglify:壓縮js程式碼

2)grunt-contrib-concat:合併js檔案

3)grunt-contrib-qunit:單元測試

4)grunt-contrib-jshint:js程式碼檢查

5)grunt-contrib-watch:檔案監控

6)grunt-contrib-sass:Scss編譯

7)grunt-contrib-connect:建立本地伺服器

最後後臺啟動ES(前臺啟動當然是將 -d去掉啦)

./usr/es/elasticsearch/bin/elasticsearch -d

在瀏覽器ip:9100訪問,如果沒有head外掛則訪問ip:9200

測試一下,往搜尋引擎放點資料

curl -XPUT 'http://192.168.110.66:9200/myappname/myblog/1?pretty' -d '{"title":"我的標題","content":"我的內容"}

curl -XPUT 'http://192.168.110.66:9200/myappname/myblog/2?pretty' -d '{"title":"第二篇標題","content":"第二篇內容"}

 

或者通過head外掛插入

 

讀取剛剛錄入的資料

curl -XGET 'http://192.168.110.66:9200/myappname/myblog/_search?pretty=true' -d '{"query":{"match_all":{}}}'

 

或者通過head外掛查詢

 

用的是post,不知道為啥get會搜尋出全部的,並沒有按關鍵詞搜尋

測試之前安裝的ik外掛是否生效

並且實驗ik外掛不同的切詞效果

 

 

 

通過返回結果可以看出,ik_max_word切詞把中華人民共和國國歌切成了“中華人民共和國”、“中華人民”、“中華”、“華人”、“人民共和國”、“人民”、“共和國”、“共和”、“國”、“國歌”

也就是說我們搜尋這些詞中的任意一個都能把這句話搜到,如果不安裝ik外掛的話,那隻會切成:“中”、“華”、“人”、“民”、“共”、“和”、“國”、“國”、“歌”,不夠智慧,搜尋也不好進行了

"analyzer"可以為“ik","ik_smart","ik_max_word”,選擇不同的切詞效果

上面幾條命令都是json形式,elasticSearch就是這麼人性化,沒治了。

這裡面的myappname是你自己可以改成自己應用的名字,這在elasticSearch資料儲存中是完全隔離的,而myblog是我們在同一個app中想要維護的不同的資料,就是你的不同資料,比如文章、使用者、評論,他們最好都分開,這樣搜尋的時候也不會混

pretty引數就是讓返回的json有換行和縮排,容易閱讀,除錯時可以加上,開發到程式裡就可以去掉了

analyzer就是切詞器,我們指定的ik_max_word在前面配置檔案裡遇到過,表示最大程度切詞,各種切,360度切

返回結果裡的hits就是“命中”,total是命中了幾條,took是花了幾毫秒,_score就是相關性程度,可以用來做排序的依據

叢集安裝

僅為了測試,將叢集部署在一臺虛擬機器上

cp -R /usr/es /usr/es1

cp -R /usr/es /usr/es2

分別進入到es對應的elasticsearch.yml修改配置檔案

vim /usr/es/elasticsearch/config/elasticsearch.yml

cluster1

 

 

cluster2

 

cluster3

 

 

cluster.name: es

node.name: es-node03

path.data: /usr/es2/data

path.logs: /usr/es2/logs

network.bind_host: 192.168.110.66

network.publish_host: 192.168.110.66

network.host: 0.0.0.0

http.cors.enabled: true

http.cors.allow-origin: "*"

http.port: 9202

tansport.tcp.port: 9302

discovery.zen.ping.unicast.hosts: ["192.168.110.66", "192.168.110.66:9301","192.168.110.66:9302"]

discovery.zen.minimum_master_nodes: 2

discovery.zen.ping.timeout: 40s

discovery.zen.ping.multicast.enabled: false

PS:黑色字型部分為單機配置

總結:啟動elasticsearch和head外掛(無法訪問 iptables -F 關閉防火牆試試)

su elasticsearch

------------------------------------------------

單機

/usr/es/elasticsearch/bin/elasticsearch -d

叢集

/usr/es/elasticsearch/bin/elasticsearch -d

/usr/es1/elasticsearch/bin/elasticsearch -d

/usr/es2/elasticsearch/bin/elasticsearch -d

-------------------------------------------------

cd /usr/es/elasticsearch/plugins/head/

nohup grunt server &

Ps:

一.執行

編輯 /usr/es/elasticsearch/bin/elasticsearch.in.sh, 設定 ES_MIN_MEM和ES_MAX_MEM,確保二者數值一致,或者可以在啟動es時指定,

/usr/es/elasticsearch/bin/elasticsearch -d -Xms512m -Xmx512m

二.關閉

前臺執行:可以通過”CTRL+C”組合鍵來停止執行

後臺執行,可以通過”kill -9 程序號”停止.也可以通過REST API介面:

curl -XPOST http://主機IP:9200/_cluster/nodes/_shutdown

來關閉整個叢集,通過:

curl -XPOST http://主機IP:9200/_cluster/nodes/節點標示符(如es-node01)/_shutdown