全文搜索引擎 Elasticsearch 集羣搭建入門教程

介紹

ElasticSearch 是一個基於 Lucene 的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於 RESTful web 接口。Elasticsearch 是用 Java 開發的,並做爲 Apache 許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,可以達到實時搜索,穩定,可靠,快速,安裝使用方便。基百科、Stack Overflow、Github 都採用它。html

本文從零開始,講解如何使用 Elasticsearch 搭建本身的全文搜索引擎。每一步都有詳細的說明,你們跟着作就能學會。
<!-- more -->html5

環境

一、VMwarejava

二、Centos 6.6node

三、Elasticsearch 5.5.2linux

四、JDK 1.8git

VMware 安裝以及在 VMware 中安裝 Centos 這個就不說了,環境配置直接默認就好,不過度配給機器的內存最好設置大點(建議 2G),github

使用 dhclient 命令來自動獲取 IP 地址,查看獲取的 IP 地址則使用命令 ip addr 或者 ifconfig ,則會看到網卡信息和 lo 卡信息。web

給虛擬機額中的 linux 設置固定的 ip(由於後面發現每次機器重啓後又要從新使用 dhclient 命令來自動獲取 IP 地址)apache

vim  /etc/sysconfig/network-scripts/ifcfg-eth0

修改:npm

onboot=yes
bootproto=static

增長:(下面可設置可不設置)

IPADDR=192.168.1.113            網卡IP地址
GATEWAY=192.168.1.1
NETMASK=255.255.255.0

設置好以後,把網絡服務重啓一下, service network restart

修改 ip 地址參考: http://jingyan.baidu.com/arti...

大環境都準備好了,下面開始安裝步驟:

安裝 JDK 1.8

先卸載自帶的 openjdk,查找 openjdk

rpm -qa | grep java

卸載 openjdk

yum -y remove  java-1.7.0-openjdk-1.7.0.65-2.5.1.2.el65.x8664
yum -y remove java-1.6.0-openjdk-1.6.0.0-11.1.13.4.el6.x86_64

解壓 JDK 安裝包:

附上jdk1.8的下載地址:
http://www.oracle.com/technet...

解壓完成後配置一下環境變量就 ok

一、在/usr/local/下建立Java文件夾

cd /usr/local/                                    進入目錄
mkdir java                                     新建java目錄

二、文件夾建立完畢,把安裝包拷貝到 Java 目錄中,而後解壓 jdk 到當前目錄

cp /usr/jdk-8u144-linux-x64.tar.gz /usr/local/java/    **注意匹配你本身的文件名**  拷貝到java目錄
tar -zxvf jdk-8u144-linux-x64.tar.gz       解壓到當前目錄(Java目錄)

三、解壓完以後,Java目錄中會出現一個jdk1.8.0_144的目錄,這就解壓完成了。以後配置一下環境變量。
編輯/etc/下的profile文件,配置環境變量

vi /etc/profile                  進入profile文件的編輯模式

在最後邊追加一下內容(**配置的時候必定要根據本身的目錄狀況而定哦!**)

 JAVA_HOME=/usr/local/java/jdk1.8.0_144
 CLASSPATH=$JAVA_HOME/lib/
 PATH=$PATH:$JAVA_HOME/bin
 export PATH JAVA_HOME CLASSPATH

以後保存並退出文件以後。

讓文件生效: source /etc/profile

在控制檯輸入Java 和 Java -version 看有沒有信息輸出,以下: java -version

java version "1.8.0_144"
 Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
 Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode)

能顯示以上信息,就說明 JDK 安裝成功啦


安裝 Maven

由於後面可能會用到 maven ,先裝上這個。

一、下載 maven

wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz

二、解壓至 /usr/local 目錄

tar -zxvf apache-maven-3.2.5-bin.tar.gz

三、配置公司給的配置

替換成公司給的 setting.xml 文件,修改關於本地倉庫的位置, 默認位置: ${user.home}/.m2/repository

四、配置環境變量etc/profile 最後添加如下兩行

export MAVEN_HOME=/usr/local/apache-maven-3.2.5
export PATH=${PATH}:${MAVEN_HOME}/bin

五、測試

[root@localhost ~]# mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T09:29:23-08:00)
Maven home: /usr/local/apache-maven-3.2.5

VMware 虛擬機裏面的三臺機器 IP 分別是:

192.168.153.133
192.168.153.134
192.168.153.132

配置 hosts

在 /etc/hosts下面編寫:ip node 節點的名字(域名解析)

vim  /etc/hosts

新增:

192.168.153.133  es1
192.168.153.134  es2
192.168.153.132  es3

設置 SSH 免密碼登陸

安裝expect命令 : yum -y install expect

將 ssh_p2p.jar 隨便解壓到任何目錄下: (這個 jar 包能夠去網上下載)

unzip ssh_p2p.zip

修改 resource 的 ip 值

vim /ssh_p2p/deploy_data/resource  (各個節點和帳戶名,密碼,free表明相互均可以無密碼登錄)
#設置爲你每臺虛擬機的ip地址,用戶名,密碼
address=(
"192.168.153.133,root,123456,free"
"192.168.153,134,root,123456,free"
"192.168.153.132,root,123456,free"
)

修改 start.sh 的運行權限

chmod u+x start.sh

運行

./start.sh

測試:

ssh ip地址 (測試是否能夠登陸)

安裝 ElasticSearch

下載地址: https://www.elastic.co/downlo...

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.tar.gz
cd /usr/local
tar -zxvf   elasticsearch-5.5.2.tar.gz

su tzs 切換到 tzs 用戶下 ( 默認不支持 root 用戶)

sh /usr/local/elasticsearch/bin/elasticsearch -d 其中 -d 表示後臺啓動

在 vmware 上測試是否成功:curl http://localhost:9200/

test

出現如上圖這樣的效果,就表明已經裝好了。

elasticsearch 默認 restful-api 的端口是 9200 不支持 IP 地址,也就是說沒法從主機訪問虛擬機中的服務,只能在本機用 http://localhost:9200 來訪問。若是須要改變,須要修改配置文件 /usr/local/elasticsearch/config/elasticsearch.yml 文件,加入如下兩行:

network.bind_host: 0.0.0.0
network.publish_host: _nonloopback:ipv4

或去除 network.host 和 http.port 以前的註釋,並將 network.host 的 IP 地址修改成本機外網 IP。而後重啓,Elasticsearch

關閉方法(輸入命令: ps -ef | grep elasticsearch ,找到進程,而後 kill 掉就好了。

若是外網仍是不能訪問,則有多是防火牆設置致使的 ( 關閉防火牆:service iptables stop )

修改配置文件:vim config/elasticsearch.yml

cluster.name : my-app (集羣的名字,名字相同的就是一個集羣)

node.name : es1 (節點的名字, 和前面配置的 hosts 中的 name 要一致)

path.data: /data/elasticsearch/data (數據的路徑。沒有要建立(mkdir -p /data/elasticsearch/{data,logs}),而且給執行用戶權限 chown tzs /data/elasticsearch/{data,logs} -R
path.logs: /data/elasticsearch/logs (數據 log 信息的路徑,同上)
network.host: 0.0.0.0 //容許外網訪問,也能夠是本身的ip地址
http.port: 9200 //訪問的端口
discovery.zen.ping.unicast.hosts: ["192.168.153.133", "192.168.153.134", "192.168.153.132"] //各個節點的ip地址

記得須要添加上:(這個是安裝 head 插件要用的, 目前不須要)
http.cors.enabled: true
http.cors.allow-origin: "*"

最後在外部瀏覽器的效果以下圖:

test-on-bro

安裝 IK 中文分詞

能夠本身下載源碼使用 maven 編譯,固然若是怕麻煩能夠直接下載編譯好的

https://github.com/medcl/elas...

注意下載對應的版本放在 plugins 目錄下

解壓

unzip elasticsearch-analysis-ik-5.5.2.zip

在 es 的 plugins 下新建 ik 目錄

mkdir ik

將剛纔解壓的複製到ik目錄下

cp -r elasticsearch/* ik

刪除剛纔解壓後的

rm -rf elasticsearch
rm -rf elasticsearch-analysis-ik-5.5.2.zip

IK 帶有兩個分詞器

ik_max_word :會將文本作最細粒度的拆分;儘量多的拆分出詞語

ik_smart:會作最粗粒度的拆分;已被分出的詞語將不會再次被其它詞語佔有

安裝完 IK 中文分詞器後(固然不止這種中文分詞器,還有其餘的,能夠參考個人文章 Elasticsearch 默認分詞器和中分分詞器之間的比較及使用方法),測試區別以下:

ik_max_word

curl -XGET 'http://192.168.153.134:9200/_...' -d '聯想是全球最大的筆記本廠商'

{
  "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" : 7,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "的",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "筆記本",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "筆記",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "本廠",
      "start_offset" : 10,
      "end_offset" : 12,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "廠商",
      "start_offset" : 11,
      "end_offset" : 13,
      "type" : "CN_WORD",
      "position" : 8
    }
  ]
}
ik_smart

curl -XGET 'http://localhost:9200/_analyz...' -d '聯想是全球最大的筆記本廠商'

{
  "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" : 7,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "的",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "筆記本",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "廠商",
      "start_offset" : 11,
      "end_offset" : 13,
      "type" : "CN_WORD",
      "position" : 6
    }
  ]
}

安裝 head 插件

elasticsearch-head 是一個 elasticsearch 的集羣管理工具,它是徹底由 html5 編寫的獨立網頁程序,你能夠經過插件把它集成到 es。

效果以下圖:(圖片來自網絡)

安裝 git

yum remove git
yum install git
git clone git://github.com/mobz/elasticsearch-head.git   拉取 head 插件到本地,或者直接在 GitHub 下載 壓縮包下來

安裝nodejs

先去官網下載 node-v8.4.0-linux-x64.tar.xz

tar -Jxv -f  node-v8.4.0-linux-x64.tar.xz
mv node-v8.4.0-linux-x64  node

環境變量設置:

vim  /etc/profile

新增:

export NODE_HOME=/opt/node
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules

使配置文件生效(這步很重要,本身要多注意這步)

source /etc/profile

測試是否全局可用了:

node -v

而後

mv elasticsearch-head head
cd head/
npm install -g grunt-cli
npm install
grunt server

再 es 的配置文件中加:

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

在瀏覽器打開 http://192.168.153.133:9100/ 就能夠看到效果了,

遇到問題

把坑都走了一遍,防止之後再次入坑,特此記錄下來

一、ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")

改變 elasticsearch 文件夾全部者到當前用戶

sudo chown -R noroot:noroot elasticsearch

這是由於 elasticsearch 須要讀寫配置文件,咱們須要給予 config 文件夾權限,上面新建了 elsearch 用戶,elsearch 用戶不具有讀寫權限,所以仍是會報錯,解決方法是切換到管理員帳戶,賦予權限便可:

sudo -i

chmod -R 775 config

二、WARN [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root

緣由是elasticsearch默認是不支持用root用戶來啓動的。

解決方案一:Des.insecure.allow.root=true

修改/usr/local/elasticsearch-2.4.0/bin/elasticsearch,

添加 ES_JAVA_OPTS="-Des.insecure.allow.root=true"

或執行時添加: sh /usr/local/elasticsearch-2.4.0/bin/elasticsearch -d -Des.insecure.allow.root=true

注意:正式環境用root運行可能會有安全風險,不建議用root來跑。

解決方案二:添加專門的用戶

useradd elastic
chown -R elastic:elastic  elasticsearch-2.4.0
su elastic
sh /usr/local/elasticsearch-2.4.0/bin/elasticsearch -d

三、UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in

只是警告,使用新的linux版本,就不會出現此類問題了。

四、ERROR: [4] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

緣由:沒法建立本地文件問題,用戶最大可建立文件數過小

解決方案:切換到 root 用戶,編輯 limits.conf 配置文件, 添加相似以下內容:

vim /etc/security/limits.conf

添加以下內容:

*  soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

[2]: max number of threads [1024] for user [tzs] is too low, increase to at least [2048]

緣由:沒法建立本地線程問題,用戶最大可建立線程數過小

解決方案:切換到root用戶,進入limits.d目錄下,修改90-nproc.conf 配置文件。

vim /etc/security/limits.d/90-nproc.conf

找到以下內容:

  • soft nproc 1024

修改成

  • soft nproc 2048

[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

緣由:最大虛擬內存過小

root用戶執行命令:

sysctl -w vm.max_map_count=262144

或者修改 /etc/sysctl.conf 文件,添加 「vm.max_map_count」設置
設置後,可使用
$ sysctl -p

[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk

緣由:Centos6不支持SecComp,而ES5.4.1默認bootstrap.system_call_filter爲true進行檢測,因此致使檢測失敗,失敗後直接致使ES不能啓動。
詳見 :https://github.com/elastic/el...

解決方法:在elasticsearch.yml中新增配置bootstrap.system_call_filter,設爲false,注意要在Memory下面:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false

五、 java.lang.IllegalArgumentException: property [elasticsearch.version] is missing for plugin [head]

再 es 的配置文件中加:

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

最後

整個搭建的過程全程本身手動安裝,不易,若是安裝不少臺機器,是否能夠寫個腳本之類的自動搭建呢?能夠去想一想的。首發於:http://www.54tianzhisheng.cn/...,轉載請註明出處,謝謝配合!

相關文章
相關標籤/搜索