1. 程式人生 > >和我一起打造個簡單搜索之ElasticSearch集群搭建

和我一起打造個簡單搜索之ElasticSearch集群搭建

停止 連接 gda onf -xmx elastic 分享 查看 關閉

我們所常見的電商搜索如京東,搜索頁面都會提供各種各樣的篩選條件,比如品牌、尺寸、適用季節、價格區間等,同時提供排序,比如價格排序,信譽排序,銷量排序等,方便了用戶去找到自己心裏理想的商品。

站內搜索對於一個網站幾乎是標配,只是搜索的強大與否的區別,有的網站只支持關鍵詞模糊搜索,而淘寶,京東提供了精細的篩選條件,同時支持拼音搜索等更方便的搜索方式。

由於筆者在一家做網絡文學的公司工作,所以實現就是以小說為商品的搜索,具體可以參考起點網小說的搜索。

技術分享圖片

如圖所示,起點網的搜索提供了關鍵詞搜索和排序條件以及篩選條件,接下來,我們一起來實現這個吧~

環境

本文以及後續 es 系列文章都基於 5.5.3

這個版本的 elasticsearch ,這個版本比較穩定,可以用於生產環境。

系列文章

  • 一、和我一起打造個簡單搜索之ElasticSearch集群搭建
  • 二、和我一起打造個簡單搜索之ElasticSearch入門
  • 三、和我一起打造個簡單搜索之IK分詞以及拼音分詞
  • 四、和我一起打造個簡單搜索之Logstash實時同步建立索引
  • 五、和我一起打造個簡單搜索之SpringDataElasticSearch入門
  • 六、和我一起打造個簡單搜索之SpringDataElasticSearch關鍵詞高亮
  • ...

環境準備之 ES 集群搭建

master 配置

## 下載 elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.3.tar.gz

## 創建目錄
mkdir /usr/local/es

## 解壓文件到 es 目錄
tar -xvf elasticsearch-5.5.3.tar.gz -C /usr/local/es

## 修改 es 的配置文件
cd /usr/local/es/elasticsearch-5.5.3/config

vim elasticsearch.yml

## 在文件末尾增加

http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: es-search
node.name: slave1
node.master: true
network.host: 0.0.0.0

## 修改 es 的 jvm 設置,如果不調節,可能啟動一個 master, slave 就沒足夠內存來啟動了

vim jvm.options

修改
-Xms2g 
-Xmx2g
為
-Xms512m
-Xmx512m

註意,如果是線上,這個內存就不要修改了,使用默認的內存 2G 即可。

slave 配置

## 準備搭建一個偽集群 1個master + 2個slave
cd /usr/local/es

## 將目錄重命名為 master
[root@localhost es]# mv elasticsearch-5.5.3/ master

##拷貝兩份為 slave
[root@localhost es]# cp -r master/ slave1
[root@localhost es]# cp -r master/ slave2


## 修改兩個 slave 的配置

### 修改 slave1 的配置
[root@localhost es]# vim slave1/config/elasticsearch.yml

http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: es-search
node.name: slave1
## 註意 http 端口不要設置一樣,以免沖突
http.port: 8200
#node.master: true
network.host: 0.0.0.0

### 修改 slave2 的配置
[root@localhost es]# vim slave2/config/elasticsearch.yml

http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: es-search
node.name: slave2
## 註意 http 端口不要設置一樣,以免沖突
http.port: 7200
#node.master: true
network.host: 0.0.0.0

添加用戶

## 啟動 es 不能使用 root 用戶,所以先需要增加新的用戶

[root@localhost es]# adduser esuser
[root@localhost es]# chown -R esuser /usr/local/es/

## 切換到 esuser 用戶
[root@localhost es]# su esuser
[esuser@localhost es]$ chmod 777 /usr/local/es/

啟動集群中的 master

# 先測試能否正常啟動
[esuser@localhost es]$ /usr/local/es/master/bin/elasticsearch

# 查看打印的日誌信息
[2018-09-02T01:45:21,125][INFO ][o.e.g.GatewayService     ] [master] recovered [0] indices into cluster_state
[2018-09-02T01:45:21,138][INFO ][o.e.h.n.Netty4HttpServerTransport] [master] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}
[2018-09-02T01:45:21,138][INFO ][o.e.n.Node               ] [master] started


## 啟動失敗提示
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

## 切換到 root 用戶,修改系統配置
su root
# 輸入登錄密碼

vim /etc/security/limits.conf

## 在文件末尾增加,不要去掉前面的 * 號

* soft nofile 300000
* hard nofile 300000
* soft nproc 102400
* soft memlock unlimited
* hard memlock unlimited

## 對 sysctl.conf 文件 進行修改
echo "vm.max_map_count=262144" > /etc/sysctl.conf
sysctl -p

## 修改完畢,切換回 esuser 用戶身份
su esuser

## 嘗試啟動
[esuser@localhost es]$ /usr/local/es/master/bin/elasticsearch

# 提示已經啟動成功了
[2018-09-02T02:10:14,285][INFO ][o.e.h.n.Netty4HttpServerTransport] [master] publish_address {192.168.199.192:9200}, bound_addresses {[::]:9200}
[2018-09-02T02:10:14,285][INFO ][o.e.n.Node               ] [master] started
[2018-09-02T02:10:14,289][INFO ][o.e.g.GatewayService     ] [master] recovered [0] indices into cluster_state

驗證啟動

使用瀏覽器訪問 http://ip:9200 ip 替換你的 ip 地址,我的是 http://192.168.199.192:9200

瀏覽器響應內容

{
    name: "master",
    cluster_name: "es-search",
    cluster_uuid: "JoNUMEKFS06NHNS7p3bdWg",
    version: {
        number: "5.5.3",
        build_hash: "9305a5e",
        build_date: "2017-09-07T15:56:59.599Z",
        build_snapshot: false,
        lucene_version: "6.6.0"
    },
    tagline: "You Know, for Search"
}

註意:如果無法訪問,請關閉防火墻

後臺守護進程啟動 es 集群

前文是直接啟動,如果按下 ctrl + c 或者結束 ssh 會話,es 會立即停止退出,因此需要通過守護進程後臺啟動


[esuser@localhost es]$ /usr/local/es/master/bin/elasticsearch -d

## 查看是否啟動成功
ps -ef | grep elasticsearch

## 正常可以看到一個 elasticsearch 進程

## 如前文一樣,分別測試兩個 slave 是否可以正常啟動

### 測試slave1
[esuser@localhost es]$ /usr/local/es/slave1/bin/elasticsearch -d

### 瀏覽器訪問 http://ip:8200,響應為:
{
    "name":"slave1",
    "cluster_name":"es-search",
    "cluster_uuid":"JoNUMEKFS06NHNS7p3bdWg",
    "version":{
        "number":"5.5.3",
        "build_hash":"9305a5e",
        "build_date":"2017-09-07T15:56:59.599Z",
        "build_snapshot":false,
        "lucene_version":"6.6.0"
    },
    "tagline":"You Know, for Search"
}

### 測試slave2
[esuser@localhost es]$ /usr/local/es/slave2/bin/elasticsearch -d
### 瀏覽器訪問 http://ip:8200,響應為:

{
    name: "slave2",
    cluster_name: "es-search",
    cluster_uuid: "JoNUMEKFS06NHNS7p3bdWg",
    version: {
        number: "5.5.3",
        build_hash: "9305a5e",
        build_date: "2017-09-07T15:56:59.599Z",
        build_snapshot: false,
        lucene_version: "6.6.0"
    },
    tagline: "You Know, for Search"
}

至此完成了 es 集群(偽)的搭建。

環境準備之 elasticsearch head 安裝

為了方便我們觀察調試,安裝這個 es 插件。

安裝步驟參考官方 github

# 把插件安裝到 es 目錄下
cd /usr/local/es/

git clone git://github.com/mobz/elasticsearch-head.git  
cd elasticsearch-head  
npm install
npm run start

npm install 這一步,由於國內網絡環境的原因,可能會失敗,可以npm 換源重試。

啟動完成後,瀏覽器訪問 http://ip:9100,我的是 http://192.168.199.192:9100/

技術分享圖片

這裏需要修改連接地址,為你的 es 所在的 ip:9200。後面集群健康為綠色為正常。

到這裏,插件也就安好了。

分詞

商品搜索,分詞是必不可少的,開源的中文分詞最有名的莫過於 IK 分詞了,同時為了給用戶提供更好的體驗,同時配置 pinyin 分詞,即輸入拼音也可以進行搜索,網上也有對應的分詞器,在下文中我們一起來配置分詞器。

有疑問?

歡迎來信,給我寫信

和我一起打造個簡單搜索之ElasticSearch集群搭建