Elastic Stack 筆記(二)Elasticsearch5.6 安裝 IK 分詞器和 Head 插件

博客地址:http://www.moonxy.comhtml

1、前言java

Elasticsearch 做爲開源搜索引擎服務器,其核心功能在於索引和搜索數據。索引是把文檔寫入 Elasticsearch 的過程,搜索是匹配查詢條件找出文檔的過程,實現全文檢索一個分析過程,分析過程主要分爲兩步,第一步是詞條化,分詞器把輸入文本轉化爲一個個的詞條流;第二步是過濾,在這個階段有若干個過濾器處理詞條流中的詞條,好比停用詞過濾器會從詞條流中去除不相干的詞條,同義詞過濾器會添加新詞條或者改變已有詞條,小寫過濾器會把全部詞條變成小寫。node

Elasticsearch 內置多種分詞器可供使用,在索引和查詢過程當中咱們能夠指定分詞器,也能夠經過安裝插件的方式使用第三方分詞工具。好比目前使用最多的第三方插件 elasticsearch-analysis-ik,其代碼託管在 GItHub 上,項目地址爲:https://github.com/medcl/elasticsearch-analysis-ik,統一版本號以後的 IK 版本與 Elasticsearch 的版本一致。linux

2、安裝 IK 分詞插件c++

2.1 參照官方文檔安裝IK分詞器git

1)直接到 https://github.com/medcl/elasticsearch-analysis-ik/releases 下載對應版本 zip 包github

在 elasticsearch的 plugin 目錄下新建 ik 目錄,而後將其解壓到 ik 目錄web

[root@masternode ~]$ unzip /usr/software/elasticsearch-analysis-ik-5.6.0.zip

目錄結構以下:算法

[esuser@masternode ik]$ pwd
/opt/elk/elasticsearch-5.6.0/plugins/ik
[esuser@masternode ik]$ ll
total 1420
-rw-r--r-- 1 esuser esuser 263965 May 30 21:43 commons-codec-1.9.jar
-rw-r--r-- 1 esuser esuser  61829 May 30 21:43 commons-logging-1.2.jar
drwxr-xr-x 2 esuser esuser    319 May 30 23:08 config
-rw-r--r-- 1 esuser esuser  51384 May 30 21:43 elasticsearch-analysis-ik-5.6.0.jar
-rw-r--r-- 1 esuser esuser 736658 May 30 21:43 httpclient-4.5.2.jar
-rw-r--r-- 1 esuser esuser 326724 May 30 21:43 httpcore-4.4.4.jar
-rw-r--r-- 1 esuser esuser   2666 May 30 21:43 plugin-descriptor.properties

2)使用 elasticsearch-plugin 命令安裝npm

[esuser@masternode elasticsearch-5.6.0]$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.0/elasticsearch-analysis-ik-5.6.0.zip

安裝 ik 插件以後,修改目錄的訪問權限爲 esuer,以下:

[root@masternode plugins]# chown -R esuser:esuser ik

在 IK 插件的配置文件(/opt/elk/elasticsearch-5.6.0/plugins/ik/config/IKAnalyzer.cfg.xml)中能夠指定詞庫的位置,也能夠自定義字典 hotwords.dic,每個詞語一行,而後添加到配置文件中,多個配置文件之間使用英文分號隔開。

好比下面的設置:

<?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">extra_main.dic;hotwords.dic</entry>
        <!--用戶能夠在這裏配置本身的擴展中止詞字典-->
        <entry key="ext_stopwords">extra_stopword.dic</entry>
        <!--用戶能夠在這裏配置遠程擴展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用戶能夠在這裏配置遠程擴展中止詞字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

設置好詞典以後,重啓 Elasticsearch,以下:

發現日誌中已經加載了咱們上面設置的詞典,代表 IK 分詞器插件已經安裝成功。

2.二、IK 分詞算法比較

IK 分詞器主要使用兩種分詞算法:ik_max_word 和 ik_smart,二者的區別以下:

ik_max_word:會將文本作最細粒度的拆分,好比會將"中華人民共和國國歌"拆分爲"中華人民共和國,中華人民,中華,華人,人民共和國,人民,人,民,共和國,共和,和,國國,國歌",會窮盡各類可能的組合;

ik_smart:會作最粗粒度的拆分,即智能切分,好比會將"中華人民共和國國歌"拆分爲"中華人民共和國,國歌"。

3、安裝 Head 插件

Elastic-Head(簡稱 Head)是一個 HTML5 編寫的集羣操做和管理工具,能夠對集羣進行不少操做。在 Head 插件中能夠顯示集羣的拓撲結構,執行索引和節點級別的操做,同時也可以輸入 RESTful 命令和 Elasticsearch 交互。ES5.0以後,head 再也不做爲插件放在其 {es_home}/plugins 目錄下,而是變爲獨立運行的本地或 server 服務,須要單獨運行。若是讓 head 做爲 server 程序部署和運行,則須要藉助 npm 和 grunt 組件完成。npm 是隨同 node.js 一塊兒安裝的包管理工具,能解決 node.js 代碼部署上的不少問題。grunt 是一個基於任務的 Javascript 命令行構建工具,grunt 及其插件是經過 npm 安裝並管理的。 

3.1 安裝 Git

elasticsearch-head 是一款開源軟件,被託管在 Github 上面,因此若是咱們要使用它,須要先安裝 git,經過 git 獲取 elasticsearch-head。

安裝 git 的源碼包

1)進入 git 官網:https://git-scm.com/download,獲取 linux 版本的 git,或者直接訪問全部版本的目錄:https://mirrors.edge.kernel.org/pub/software/scm/git/,好比下載 git-2.9.5.tar.gz,以下:

2)安裝 gcc、g++ 以及編譯 git 所須要的包

安裝 gcc 編譯器

[root@masternode ~]# yum install gcc

安裝 g++ 編譯器

[root@masternode ~]# yum install gcc-c++

安裝編譯所依賴的其餘包

[root@masternode ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

[root@masternode ~]# yum install gcc perl-ExtUtils-MakeMaker

3)編譯並安裝 git

[root@masternode ~]# tar zxvf /usr/software/git-2.9.5.tar.gz

[root@masternode git-2.9.5]# ./configure --prefix=/opt/git/git-2.9.5 && make install

編譯並指定安裝目錄 /opt/git/git-2.9.5,而後安裝。

4)設置 git 的環境變量

[root@masternode bin]# vi /etc/profile

在文件最後一行添加

export PATH=/opt/git/git-2.9.5/bin:$PATH

保存後執行

[root@masternode bin]# source /etc/profile

使配置文件生效

3.2 安裝 Node.js

運行 elasticsearch-head 會用到 grunt,而 grunt 須要 npm 包管理器,因此 node.js 是必需要安裝的。

安裝編譯後的可執行文件 Node.js

訪問 node.js 的官網: https://nodejs.org/en/

選擇 LTS 版本,以下:

通常開放源代碼軟件都會至少有兩個版本發佈:

Source Code 是源代碼版,你須要本身編譯成可執行軟件。

Linux Binaries 是二進制可執行版,已經給你編譯好了,直接下載解壓就可使用。

此處下載二進制包 node-v8.11.2-linux-x64.tar.xz,而後解壓:

[root@masternode software]# xz -d node-v8.11.2-linux-x64.tar.xz

[root@masternode software]# tar -xvf node-v8.11.2-linux-x64.tar -C /opt/nodejs

[root@masternode software]# vi /etc/profile

而後輸入雙引號裏面的命令:":$",跳到文件最後一行

將下面這行拷貝到文件最底部,並保存

export PATH=/opt/nodejs/node-v8.11.2-linux-x64/bin:$PATH

更新 profile,使更改生效

[root@masternode software]# source /etc/profile

安裝 cnpm

因爲使用 npm 安裝依賴包太慢(都是國外的),因此使用國內淘寶的鏡像吧,安裝 cnpm

[root@masternode software]# npm install -g cnpm --registry=https://registry.npm.taobao.org

3.3 驗證以上軟件

能夠經過查看其版本號來驗證以上安裝的軟件,以下:

[root@masternode software]# node -v
v8.11.2
[root@masternode software]# npm -v
5.6.0
[root@masternode software]# cnpm -v
cnpm@5.3.0 (/opt/nodejs/node-v8.11.2-linux-x64/lib/node_modules/cnpm/lib/parse_argv.js)
npm@5.10.0 (/opt/nodejs/node-v8.11.2-linux-x64/lib/node_modules/cnpm/node_modules/npm/lib/npm.js)
node@8.11.2 (/opt/nodejs/node-v8.11.2-linux-x64/bin/node)
npminstall@3.6.2 (/opt/nodejs/node-v8.11.2-linux-x64/lib/node_modules/cnpm/node_modules/npminstall/lib/index.js)
prefix=/opt/nodejs/node-v8.11.2-linux-x64
linux x64 3.10.0-862.el7.x86_64
registry=https://registry.npm.taobao.org

如上所示,說明 Node 環境已經配置成功。

3.4 安裝 head 插件

[root@masternode ~]# cd /opt/eshead

使用 git 命令下載 elasticsearch-head 到本地:

[root@masternode eshead]# git clone git://github.com/mobz/elasticsearch-head.git

[root@masternode eshead]# cd elasticsearch-head

安裝 elasticsearch-head 依賴包:

[root@masternode elasticsearch-head]# cnpm install

若是運行 # cnpm install 以後出現以下錯誤:

optional install error: Package require os(darwin) not compatible with your platform(linux)

可運行以下命令解決:

[root@masternode elasticsearch-head]# cnpm rebuild node-sass

而後再次運行 # cnpm install 進行安裝:

[root@masternode elasticsearch-head]# cnpm install

修改 Head 插件的配置文件

[root@masternode elasticsearch-head]# vi Gruntfile.js

找到以下元素,將 hostname 的值修改成 '*',表示容許全部 IP 能夠訪問 head 插件:

保存後退出 vi。

修改 Elasticsearch 的配置文件

編輯 /opt/elk/elasticsearch-5.6.0/config/elasticsearch.yml,添加以下設置:

http.cors.enabled: true
http.cors.allow-origin: "*"

3.5 開啓 9100 端口

若是須要在別的機器上訪問 head 插件,防火牆必須放行 9100 端口。

永久開啓 9100 端口

[root@masternode elasticsearch-head]# firewall-cmd --zone=public --add-port=9100/tcp --permanent

重啓防火牆

[root@masternode elasticsearch-head]# firewall-cmd --reload

3.6 分別啓動 Elasticsearch 和 head 插件

將 head 插件的目錄全部者修改成 esuser:

[root@masternode opt]# chown -R esuser:esuser eshead

啓動 Elasticsearch,以下:

[esuser@masternode elasticsearch-5.6.0]$ pwd
/opt/elk/elasticsearch-5.6.0
[esuser@masternode elasticsearch-5.6.0]$ ./bin/elasticsearch

啓動 head 插件,以下:

[esuser@masternode elasticsearch-head]$ pwd
/opt/eshead/elasticsearch-head
[esuser@masternode elasticsearch-head]$ ./node_modules/grunt/bin/grunt server
(node:6654) ExperimentalWarning: The http2 module is an experimental API.
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

如上,表示 elasticsearch-head 插件啓動成功。

3.7 訪問 head 插件

瀏覽器輸入 http://192.168.56.110:9100,單節點返回以下頁面。前提是 Gruntfile.js 中的 hostname 修改成 '*',且防火牆放行 9100 端口。

實心五角星表示主節點(master node)。

集羣返回以下頁面:

實心圓圈表示工做節點(data node)。

建立索引以後顯示以下:

其中每個綠色小方格均表明一個數據分片(shard),橫向每一行均表明一個節點(node),縱向每一列均表明一個索引(index)。

附錄

其餘安裝細節可參考以下文章:

centos7安裝elasticsearch-head

相關文章
相關標籤/搜索