本文基於CentOS7.6,部署ElasticSearch7.6.1,詳細過程如下:
一、準備工作
1、準備機器
準備3臺機器,安裝CentOS7.6。ip計劃如下:
192.168.2.11 es-master
192.168.2.12 es-node1
192.168.2.13 es-node2
2、關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
3、安裝JDK
本處安裝jdk-8u231-linux-x64,並配置好環境變量。
4、禁用SELINUX
vim /etc/selinux/config
將SELINUX設置爲disabled:
SELINUX=disabled
5、修改主機名
#設置主機名
hostnamectl set-hostname es-node1
通過hostname命令查看主機名。
6、配置IP映射
vim /etc/hosts
最後增加以下內容:
192.168.2.11 es-master
192.168.2.12 es-node1
192.168.2.13 es-node2
7、創建用戶
在root用戶下添加es用戶:
#添加組
groupadd es
#添加用戶
adduser es -g es
#設置密碼
passwd es
給es用戶賦予權限:
#編輯/etc/sudoers
vi /etc/sudoers
#在root ALL=(ALL) ALL下增加es配置,最終如下
root ALL=(ALL) ALL
es ALL=(ALL) ALL
切換到es用戶,配置免密登錄:
#切換es用戶
su es
#切換到es用戶根目錄
cd ~
#查看根目錄
pwd
#生成rsa
ssh-keygen -t rsa
#生成公鑰
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
切換到root用戶,設置免密權限:
#文件夾權限
chmod 700 /home/es/.ssh
#文件權限
chmod 600 /home/es/.ssh/authorized_keys
把.ssh文件夾拷貝到其他節點上:
su es
cd ~
scp -r .ssh/ es@es-node2:/home/es/
在es-master、es-node1、es-node2上執行ssh相互驗證免密訪問。
二、部署es
1、部署es
在es用戶下,從https://www.elastic.co/cn/downloads/elasticsearch下載elasticsearch-7.6.1-linux-x86_64.tar.gz,上傳到服務器,並解壓縮。/data目錄爲大容量磁盤,創建數據文件和日誌文件目錄:
#創建數據目錄
mkdir -p /data/es/data
#創建日誌目錄
mkdir -p /data/es/logs
#修改目錄所有者
chown es:es -R /data/es
配置es-master,編輯elasticsearch-7.6.1/config/elasticsearch.yml,修改內容如下:
#集羣名稱
cluster.name: sdc-es-cluster
#節點名稱
node.name: es-master
#數據目錄
path.data: /data/es/data
#日誌目錄
path.logs: /data/es/logs
#本節點ip
network.host: 0.0.0.0
#端口
http.port: 9200
#集羣主節點候選列表
discovery.seed_hosts: ["es-master"]
#集羣初始主結點列表
cluster.initial_master_nodes: ["es-master"]
#集羣啓動到2個節點之前,阻止數據恢復
gateway.recover_after_nodes: 3
#跨域訪問設置
http.cors.enabled: true
http.cors.allow-origin: "*"
配置es-node1,編輯elasticsearch-7.6.1/config/elasticsearch.yml,修改內容如下:
#集羣名稱
cluster.name: sdc-es-cluster
#節點名稱
node.name: es-node1
#數據目錄
path.data: /data/es/data
#日誌目錄
path.logs: /data/es/logs
#本節點ip
network.host: 0.0.0.0
#端口
http.port: 9200
#集羣主節點候選列表
discovery.seed_hosts: ["es-master"]
#集羣初始主結點列表
cluster.initial_master_nodes: ["es-master"]
#集羣啓動到2個節點之前,阻止數據恢復
gateway.recover_after_nodes: 3
#跨域訪問設置
http.cors.enabled: true
http.cors.allow-origin: "*"
配置es-node2,編輯elasticsearch-7.6.1/config/elasticsearch.yml,修改內容如下:
#集羣名稱
cluster.name: sdc-es-cluster
#節點名稱
node.name: es-node2
#數據目錄
path.data: /data/es/data
#日誌目錄
path.logs: /data/es/logs
#本節點ip
network.host: 0.0.0.0
#端口
http.port: 9200
#集羣主節點候選列表
discovery.seed_hosts: ["es-master"]
#集羣初始主結點列表
cluster.initial_master_nodes: ["es-master"]
#集羣啓動到2個節點之前,阻止數據恢復
gateway.recover_after_nodes: 3
#跨域訪問設置
http.cors.enabled: true
http.cors.allow-origin: "*"
啓動es:
[es@es-master elasticsearch-7.6.1]$ ./bin/elasticsearch
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /data/es/logs/sdc-es-cluster.log
啓動失敗,顯示2個錯誤。
解決:max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535],在root用戶下,編輯/etc/security/limits.conf文件,在文件最後增加以下內容:
* soft nofile 65536
* hard nofile 65536
解決:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144],在root用戶下,編輯/etc/sysctl.conf文件,在文件最後增加以下內容:
vm.max_map_count=262144
命令行執行命令:sysctl -p,讓配置生效。
切換到es用戶,啓動es:
[root@es-master elasticsearch-7.6.1]# su es
[es@es-master elasticsearch-7.6.1]$ ./bin/elasticsearch
啓動成功,在瀏覽器輸入:http://192.168.2.11:9200,返回如下內容:
{
"name" : "es-master",
"cluster_name" : "sdc-es-cluster",
"cluster_uuid" : "kzGmp801R_CSLHdo9j0pHQ",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
通過配置es-node1,切換到es用戶,啓動es,在瀏覽器輸入:http://192.168.2.12:9200,返回如下內容:
{
"name" : "es-node1",
"cluster_name" : "sdc-es-cluster",
"cluster_uuid" : "YTOwCEiDToi2eitZaqCurA",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
通過配置es-node2,切換到es用戶,啓動es,在瀏覽器輸入:http://192.168.2.13:9200,返回如下內容:
{
"name" : "es-node2",
"cluster_name" : "sdc-es-cluster",
"cluster_uuid" : "YTOwCEiDToi2eitZaqCurA",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
es後臺啓動:
#後臺啓動
[es@es-master elasticsearch-7.6.1]$ ./bin/elasticsearch -d
#查看進程
[es@es-master elasticsearch-7.6.1]$ jps
13441 Elasticsearch
13477 Jps
2、部署elasticsearch-analysis-ik
es內置的分詞器對中文不友好,從https://github.com/medcl/elasticsearch-analysis-ik/releases下載es中文分詞插件elasticsearch-analysis-ik.zip。
解壓縮elasticsearch-analysis-ik.zip,複製到插件到es的plugins目錄下,如:
[es@es-master elasticsearch-7.6.1]$ pwd
/data/soft/elasticsearch-7.6.1
[es@es-master elasticsearch-7.6.1]$ ll plugins/analysis-ik/
總用量 1428
-rw-r--r-- 1 es es 263965 3月 30 13:47 commons-codec-1.9.jar
-rw-r--r-- 1 es es 61829 3月 30 13:47 commons-logging-1.2.jar
drwxr-xr-x 2 es es 299 3月 30 13:47 config
-rw-r--r-- 1 es es 54598 3月 30 13:47 elasticsearch-analysis-ik-7.6.1.jar
-rw-r--r-- 1 es es 736658 3月 30 13:47 httpclient-4.5.2.jar
-rw-r--r-- 1 es es 326724 3月 30 13:47 httpcore-4.4.4.jar
-rw-r--r-- 1 es es 1805 3月 30 13:47 plugin-descriptor.properties
-rw-r--r-- 1 es es 125 3月 30 13:47 plugin-security.policy
重啓啓動es。
分詞策略支持ik_max_word 和 ik_smart ,調用analyze接口測試, 如:
curl -XGET "http://192.168.2.11:9200/_analyze?pretty=true" -H 'Content-Type: application/json' -d'
{
"text":"陝西省人民醫院","tokenizer": "ik_smart"
}'
返回結果如下:
{
"tokens" : [
{
"token" : "陝西省",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "人民醫院",
"start_offset" : 3,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 1
}
]
}
ik_max_word:會將文本做最細粒度的拆分,比如會將“陝西省人民醫院”拆分爲“陝西省,陝西,省人民醫院,人民醫院,人民,民醫院,醫院”,會窮盡各種可能的組合,適合 Term Query;
ik_smart: 會做最粗粒度的拆分,比如會將“陝西省人民醫院”拆分爲“陝西省,人民醫院”,適合 Phrase 查詢。
3、集羣可視化訪問
從http://nodejs.cn/download/下載node-v12.16.1-linux-x64.tar.xz,上傳es-master並解壓縮。配置nodejs環境變量:
#配置環境變量
vim /etc/profile
#增加以下內容
#nodejs
export NODE_HOME=/data/soft/nodejs
export PATH=$PATH:$NODE_HOME/bin
#讓配置生效
source /etc/profile
#查看node版本
node -v
v12.16.1
#查看npm版本
npm -v
6.13.4
從https://github.com/mobz/elasticsearch-head下載elasticsearch-head-master.zip,上傳es-master並解壓縮,在elasticsearch-head根目錄下命令行執行:npm install下載相關依賴。
#編輯./_site/app.js
vim ./_site/app.js
#替換http://localhost:9200爲http://192.168.2.11:9200
在命令行執行:npm run start(後臺啓動命令:sudo nohup npm run start &),啓動elasticsearch-head。在瀏覽器輸入:http://192.168.2.11:9100,如下所示:
三、簡單使用
es空間幾何字段包括geo_point和geo_shape。空間幾何座標最好採用WGS-84座標系,即經度[-180,180],緯度:[-90,90],涉及到空間索引。
1、創建索引點
1.1、創建索引
curl -XPUT http://192.168.2.11:9200/poi
參數如下:
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"type": {
"type": "keyword"
},
"geom": {
"type": "geo_point"
}
}
}
}
1.2、插入數據
curl -XPOST http://192.168.2.11:9200/poi/_doc/1
參數如下:
{
"name": "頤和園",
"type": "公園",
"geom": {"lon": 116.272362,"lat": 39.99299}
}
curl -XPOST http://192.168.2.11:9200/poi/_doc/2
參數如下:
{
"name": "陝西省人民醫院",
"type": "醫院",
"geom": [108.930479,34.239695]
}
1.3、查詢
空間查詢包括:box查詢、distance查詢、polygon查詢:
box查詢:
"filter": {
"geo_bounding_box": {
"geom": {
"top": 45,"left": 100,"bottom": 30,"right": 120
}
}
}
distance查詢:
"filter": {
"geo_distance": {
"distance": "5km",
"geom": [108.947028,34.259424]
}
}
polygon查詢:
"filter": {
"geo_polygon": {
"geom": {
"points": [[100,30],[120,30],[120,45],[100,45]]
}
}
}
curl -XPOST http://192.168.2.11:9200/poi/_search
參數如下:
{
"query": {
"bool": {
"must": {
"match": {
"name": "人民醫院"
}
},
"filter": {
"geo_polygon": {
"geom": {
"points": [[100,30],[120,30],[120,45],[100,45]]
}
}
}
}
}
}
2、創建索引面
2.1、創建索引
curl -XPUT http://192.168.2.11:9200/image
參數如下:
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"geom": {
"type": "geo_shape"
}
}
}
}
2.2、插入數據
支持常用的wkt和geojson格式插入空間幾何圖形,要求點個數大於3,且首尾相同:
geojson:
{
"name": "GF1_PMS2",
"geom": {
"type" : "polygon",
"coordinates" : [
[[100.0, 30.0], [101.0, 30.0], [101.0, 31.0], [100.0, 31.0], [100.0, 30.0]]
]
}
}
gwt:
{
"name": "GF4_PMS",
"geom": "POLYGON ((116.0 39.0, 117.0 39.0, 117.0 40.0, 116.0 40.0, 116.0 39.0))"
}
2.3、查詢
常用空間查詢,空間圖形由geojson、wkt構建,空間關係支持:intersects(默認)、disjoint、within、contains。
envelope查詢:
"filter": {
"geo_shape": {
"geom": {
"shape": {
"type": "envelope",
"coordinates": [[100.0,50.0],[120.0,30.0]]
},
"relation": "within"
}
}
}
geojson查詢:
"filter": {
"geo_shape": {
"geom": {
"shape": {
"type": "polygon",
"coordinates": [[[100.0,30.0], [120.0,30.0], [120.0, 50.0], [100.0, 50.0], [100.0,30.0]]]
},
"relation": "intersects"
}
}
}
wkt查詢:
"filter": {
"geo_shape": {
"geom": {
"shape":"POLYGON ((100.0 30.0, 120.0 30.0, 120.0 50.0, 100.0 50.0, 100.0 30.0))"
}
}
}