如题

安装

以 utomcat 的身份配置和启动 ElasticSearch

建议不要以 root 启动,存在不安全因素。

以下,以 utomcat 用户操作,最终会以 utomcat 的身份来启动 ElasticSearch。

下载

下载页面,该页面可下载的是最新版
以 utomcat 的身份操作

1
2
3
4
5
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.tar.gz
tar -xzvf elasticsearch-6.4.1.tar.gz

mkdir -p /home/utomcat/apps/es/data
mkdir -p /home/utomcat/apps/es/logs

安装、配置

以 root 用户操作

1
mv /home/utomcat/soft/elasticsearch-6.4.1 /opt/

以 utomcat 操作

1
2
cd /opt/elasticsearch-6.4.1/
vim config/elasticsearch.yml

增加以下项

1
2
3
4
5
6
7
8
9
10
11
12
cluster.name: hjz-es-cluster
node.name: hjz-es
path.data: /home/utomcat/apps/es/data
path.logs: /home/utomcat/apps/es/logs

# 以下参数,建议在生产环境设置成局域网 IP,不建议在公网上可访问
network.host: 192.168.1.91

# 以下参数是为了让 head 插件可以远程访问 ElasticSearch。
# 为了安全性,不建议在生产环境设置这两个参数。
http.cors.enabled: true
http.cors.allow-origin: "*"

普通启动

1
/opt/elasticsearch-6.4.1/bin/elasticsearch

后台启动

1
/opt/elasticsearch-6.4.1/bin/elasticsearch -d

重启

  1. 重启之前先用 jps 查看 Elasticsearch 进程号。

    1
    2
    3
    4
    5
    6
    7
    8
    [root@centOS7BasicForTest analysis-ik]# jps
    3904 Bootstrap
    3969 Bootstrap
    1190 jenkins.war
    1912 Bootstrap
    4745 Jps
    1467 Bootstrap
    1375 Elasticsearch
  2. 杀死进程,再重新启动

    1
    2
    [root@centOS7BasicForTest analysis-ik]# kill -9 1375
    [root@centOS7BasicForTest analysis-ik]# /opt/elasticsearch-6.4.1/bin/elasticsearch -d

启动报错

1
2
3
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 身份操作

1
2
3
4
vim /etc/security/limits.conf
# 添加下面两行,其中 utomcat 是启动 es 的用户名
utomcat soft nofile 65536
utomcat hard nofile 65536

utomcat 账户退出客户端之后,再次登录,ulimit -Hn 查看硬限制,将会看到修改过的数字:65536,之前是 4096

1
2
[utomcat@centOS7BasicForTest ~]$ ulimit -Hn
65536

接着以 root 身份修改 sysctl.conf

1
2
3
vi /etc/sysctl.conf
# 添加如下配置
vm.max_map_count=262144

用命令 sysctl -p 使修改生效

再次启动

1
2
3
4
5
6
7
8
9
/opt/elasticsearch-6.4.1/bin/elasticsearch
...
T19:09:42,482][INFO ][o.e.t.TransportService ] [hjz-es] publish_address {192.168.1.91:9300}, bound_addresses {[::]:9300}
[2018-09-13T19:09:42,490][INFO ][o.e.b.BootstrapChecks ] [hjz-es] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2018-09-13T19:09:45,528][INFO ][o.e.c.s.MasterService ] [hjz-es] zen-disco-elected-as-master ([0] nodes joined)[, ], reason: new_master {hjz-es}{wJZDvKN9Q7WuzLjIHF6gXg}{I5y067JPS0iJC2c65UxnrA}{192.168.1.91}{192.168.1.91:9300}{ml.machine_memory=8067002368, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}
[2018-09-13T19:09:45,531][INFO ][o.e.c.s.ClusterApplierService] [hjz-es] new_master {hjz-es}{wJZDvKN9Q7WuzLjIHF6gXg}{I5y067JPS0iJC2c65UxnrA}{192.168.1.91}{192.168.1.91:9300}{ml.machine_memory=8067002368, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}, reason: apply cluster state (from master [master {hjz-es}{wJZDvKN9Q7WuzLjIHF6gXg}{I5y067JPS0iJC2c65UxnrA}{192.168.1.91}{192.168.1.91:9300}{ml.machine_memory=8067002368, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)[, ]]])
[2018-09-13T19:09:45,542][INFO ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [hjz-es] publish_address {192.168.1.91:9200}, bound_addresses {[::]:9200}
[2018-09-13T19:09:45,542][INFO ][o.e.n.Node ] [hjz-es] started
...

访问:192.168.1.91:9200

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "hjz-es",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "En8MsMWqRx-KEuL20eqN0A",
"version" : {
"number" : "6.4.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "595516e",
"build_date" : "2018-08-17T23:18:47.308994Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

这种启动方式,客户端关闭之后,就会停止,或者 Ctrl+C 也会停止
可以通过参数 -d 让 Elasticsearch 在后台运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/opt/elasticsearch-6.4.1/bin/elasticsearch -d

[utomcat@centOS7BasicForTest bin]$ jps
4673 Jps
1186 jenkins.war
1426 Bootstrap
4597 Elasticsearch
1318 Bootstrap
1400 Bootstrap
1356 Bootstrap
1277 Bootstrap
[utomcat@centOS7BasicForTest bin]$ ps -ef|grep Elasticsearch
utomcat 4597 1 38 19:17 pts/0 00:00:18 /usr/jdk1.8.0_171/bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch.w8x505kO -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Des.path.home=/opt/elasticsearch-6.4.1 -Des.path.conf=/opt/elasticsearch-6.4.1/config -Des.distribution.flavor=default -Des.distribution.type=tar -cp /opt/elasticsearch-6.4.1/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
utomcat 4690 4397 0 19:17 pts/0 00:00:00 grep --color=auto Elasticsearch
[utomcat@centOS7BasicForTest bin]$

启动异常?

1
2
3
[root@centOS7BasicForTest analysis-ik]# jps
...
1375 -- process information unavailable

因为以 utomcat 的身份启动,又以 root 的身份 kill,就会这样,过一会儿就好了。

设置开机启动

创建脚本文件

1
vim /etc/init.d/elasticsearch

文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
#chkconfig: 2345 80 05
#description: es

source /etc/profile

case $1 in
start)
#下面的“<<!”是切换用户后,待执行的命令,执行完后使用“!”来进行结束
su utomcat<<!
cd /opt/elasticsearch-6.4.1/bin
./elasticsearch -d
!
#上面的“!”与上面的 "!" 对应起来,并且顶格,这是语法
# 或者,换成这种语法:su - utomcat -lc "sh /opt/elasticsearch-6.4.0/bin/elasticsearch -d"
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
#“grep -v”过滤掉本身的执行命令,获取准确的pid
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "es stopped"
su utomcat<<!
cd /opt/elasticsearch-6.4.1/bin
./elasticsearch -d
!
echo "es startup"
;;
*)
echo "parameter error, usage:(start|stop|restart)"
;;
esac

设置文件执行权限以及开机启动

1
2
3
chmod +x /etc/init.d/elasticsearch
chkconfig --add /etc/init.d/elasticsearch
chkconfig elasticsearch on

Kibana

Kibana 用户手册
Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作。您可以使用 Kibana 对 Elasticsearch 索引中的数据进行搜索、查看、交互操作。您可以很方便的利用图表、表格及地图对数据进行多元化的分析和呈现。
Kibana 可以使大数据通俗易懂。它很简单,基于浏览器的界面便于您快速创建和分享动态数据仪表板来追踪 Elasticsearch 的实时数据变化。
搭建 Kibana 非常简单。您可以分分钟完成 Kibana 的安装并开始探索 Elasticsearch 的索引数据 — 没有代码、不需要额外的基础设施。

下载安装

下载页面
以 utomcat 身份操作

1
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.1-linux-x86_64.tar.gz

以 root 身份操作

1
mv /home/utomcat/soft/kibana-6.4.1-linux-x86_64 /opt/

以 utomcat 身份操作

1
2
3
4
5
6
cd /opt/kibana-6.4.1-linux-x86_64/ 
vim config/kibana.yml

# 添加项
server.host: "192.168.1.91"
elasticsearch.url: http://192.168.1.91:9200

在后台启动

1
nohup /opt/kibana-6.4.1-linux-x86_64/bin/kibana &

关闭 shell 客户端之后,进程会被终止。

查看进程 ID

1
2
3
netstat -anltp|grep 5601
ps -ef|grep kibana
ps -ef|grep node

打开 web 端

http://192.168.1.91:5601

Head 插件

official github page
Running with built in server
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
cnpm install
npm start
open http://localhost:9100/

1
2
3
4
5
6
7
8
9
[utomcat@centOS7BasicForTest elasticsearch-head]$ npm start

> elasticsearch-head@0.0.0 start /home/utomcat/soft/elasticsearch-head
> grunt server

(node:5204) ExperimentalWarning: The http2 module is an experimental API.
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

将 localhost 换成服务器的 IP:192.168.1.91 访问(http://192.168.1.91:9100)

修改连接 elasticsearch 服务 url 默认值

1
2
3
vim /home/utomcat/soft/elasticsearch-head/_site/app.js
# 找到:this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200",将 localhost 改为服务器 IP
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.1.91:9200"

开机启动该插件

开机启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vi /etc/init.d/eshead

# 内容如下
#!/bin/sh
source /etc/profile

case $1 in
start)
sh /home/utomcat/shell/es-head-start.sh;;
stop)
echo "please manual stop...";;
*)
echo "parameter error, usage:(start|stop)";;
esac

添加启动脚本

1
2
3
4
vim /home/utomcat/shell/es-head-start.sh
# 内容如下
cd /home/utomcat/soft/elasticsearch-head/
nohup npm run start >/home/utomcat/soft/elasticsearch-head/nohup.out 2>&1 &

赋执行权限

1
2
3
4
5
chmod +x /home/utomcat/shell/es-head-start.sh
chmod +x /etc/init.d/eshead
# 设置开机启动
chkconfig --add /etc/init.d/eshead
chkconfig eshead on

经验,踩坑记

chkconfig --add 报错

1
service eshead does not support chkconfig

需要在脚本中添加:

1
2
#chkconfig: 2345 80 05
#description: es-head

启动时仍然出错

1
2
3
4
5
6
7
8
9
10
[root@centOS7BasicForTest init.d]# systemctl start eshead
Job for eshead.service failed because the control process exited with error code. See "systemctl status eshead.service" and "journalctl -xe" for details.

[root@centOS7BasicForTest init.d]# journalctl -xe
...
Nov 26 18:14:53 centOS7BasicForTest systemd[1]: Starting SYSV: es-head...
...
Nov 26 18:14:53 centOS7BasicForTest eshead[2235]: /etc/rc.d/init.d/eshead: line 12: syntax error near unexpected token `)'
Nov 26 18:14:53 centOS7BasicForTest eshead[2235]: /etc/rc.d/init.d/eshead: line 12: `*)'
Nov 26 18:14:53 centOS7BasicForTest systemd[1]: eshead.service: control process exited, code=exited status=2

或者通过 systemctl status eshead 也可以查看记得情况

1
2
3
Nov 26 18:22:49 centOS7BasicForTest systemd[1]: Starting SYSV: es-head...
Nov 26 18:22:49 centOS7BasicForTest eshead[1213]: /etc/rc.d/init.d/eshead: line 10: syntax error near unexpected token `)'
Nov 26 18:22:49 centOS7BasicForTest eshead[1213]: /etc/rc.d/init.d/eshead: line 10: `stop)'

原来是语法错误
下面的脚本后少了两个分号,也就是说,在 case 的每一个语句后面,都需要两个分号

1
echo "please manual stop..."

添加两个分号之后再次执行

1
2
3
4
[root@centOS7BasicForTest init.d]# systemctl start eshead
Warning: eshead.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@centOS7BasicForTest init.d]# systemctl daemon-reload
[root@centOS7BasicForTest init.d]# systemctl start eshead

IK 分词器插件

releases
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.1/elasticsearch-analysis-ik-6.4.1.zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[utomcat@centOS7BasicForTest soft]$ /opt/elasticsearch-6.4.1/bin/elasticsearch-plugin install /home/utomcat/soft/elasticsearch-analysis-ik-6.4.1.zip
A tool for managing installed elasticsearch plugins

Commands
--------
list - Lists installed elasticsearch plugins
install - Install a plugin
remove - removes a plugin from Elasticsearch

Non-option arguments:
command

Option Description
------ -----------
-h, --help show help
-s, --silent show minimal output
-v, --verbose show verbose output
ERROR: Unknown plugin /home/utomcat/soft/elasticsearch-analysis-ik-6.4.1.zip

可以直接解压至 /opt/elasticsearch-6.4.1/plugins/ik 目录,然后重启 es。

换在线安装方式

1
2
3
4
5
6
7
8
9
10
11
12
[utomcat@centOS7BasicForTest soft]$ /opt/elasticsearch-6.4.1/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.1/elasticsearch-analysis-ik-6.4.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.1/elasticsearch-analysis-ik-6.4.1.zip
[=================================================] 100%  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik

5.6.1 的在线安装

1
2
3
4
[utomcat@AndyCentOS7Basic ~]$ /opt/elasticsearch-5.6.1/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.1/elasticsearch-analysis-ik-5.6.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.1/elasticsearch-analysis-ik-5.6.1.zip
[=================================================] 100%  
-> Installed analysis-ik

查看字典

1
2
3
4
5
6
7
8
9
10
11
12
13
ll /opt/elasticsearch-6.4.1/config/analysis-ik/
-rw-rw---- 1 utomcat gtomcat 5225922 Sep 14 14:59 extra_main.dic
-rw-rw---- 1 utomcat gtomcat 63188 Sep 14 14:59 extra_single_word.dic
-rw-rw---- 1 utomcat gtomcat 63188 Sep 14 14:59 extra_single_word_full.dic
-rw-rw---- 1 utomcat gtomcat 10855 Sep 14 14:59 extra_single_word_low_freq.dic
-rw-rw---- 1 utomcat gtomcat 156 Sep 14 14:59 extra_stopword.dic
-rw-rw---- 1 utomcat gtomcat 625 Sep 14 14:59 IKAnalyzer.cfg.xml
-rw-rw---- 1 utomcat gtomcat 3058510 Sep 14 14:59 main.dic
-rw-rw---- 1 utomcat gtomcat 123 Sep 14 14:59 preposition.dic
-rw-rw---- 1 utomcat gtomcat 1824 Sep 14 14:59 quantifier.dic
-rw-rw---- 1 utomcat gtomcat 164 Sep 14 14:59 stopword.dic
-rw-rw---- 1 utomcat gtomcat 192 Sep 14 14:59 suffix.dic
-rw-rw---- 1 utomcat gtomcat 752 Sep 14 14:59 surname.dic

自定义词典,这个很重要

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
cd /opt/elasticsearch-6.4.1/config/analysis-ik/
mkdir custom
vi custom/custom_word.dic
# 根据业务添加自定义词组,在文件中添加词语即可, 每一个词语一行
四中
七中
九中
四系
七系
九系
嘉祥
师大
德瑞
嘉祥系
师大系
德瑞系
成都
锦江
青羊
金牛
武侯
成华
龙泉驿
青白江
新都
温江
双流
郫都
郫县
高新
高新西
天府新
简阳
锦江区
青羊区
金牛区
武侯区
成华区
龙泉驿区
青白江区
新都区
温江区
双流县
郫都区
高新区
高新西区
天府新区
简阳市

更新配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd /opt/elasticsearch-6.4.1/config/analysis-ik/
vim IKAnalyzer.cfg.xml

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">custom/custom_word.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

手动重启,会在日志里看到这样一行

1
... [Dict Loading] /opt/elasticsearch-6.4.1/config/analysis-ik/custom/custom_word.dic

说明已经加载自定义分词词组

热更新 IK 分词使用方法

参数:ik_max_word

会将文本做最细粒度的拆分,会根据已有词典,穷尽各种可能的组合。
http://192.168.1.91:5601

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
GET _analyze?pretty
{
"analyzer": "ik_max_word",
"text":"中华人民共和国国歌"
}

# 结果
{
"tokens": [
{
"token": "中华人民共和国",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
},
{
"token": "中华人民",
"start_offset": 0,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
},
{
"token": "中华",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 2
},
{
"token": "华人",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 3
},
{
"token": "人民共和国",
"start_offset": 2,
"end_offset": 7,
"type": "CN_WORD",
"position": 4
},
{
"token": "人民",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 5
},
{
"token": "共和国",
"start_offset": 4,
"end_offset": 7,
"type": "CN_WORD",
"position": 6
},
{
"token": "共和",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 7
},
{
"token": "国",
"start_offset": 6,
"end_offset": 7,
"type": "CN_CHAR",
"position": 8
},
{
"token": "国歌",
"start_offset": 7,
"end_offset": 9,
"type": "CN_WORD",
"position": 9
}
]
}

参数:ik_smart

会做最粗粒度的拆分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text":"中华人民共和国国歌"
}
# 结果
{
"tokens": [
{
"token": "中华人民共和国",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
},
{
"token": "国歌",
"start_offset": 7,
"end_offset": 9,
"type": "CN_WORD",
"position": 1
}
]
}

核心概念

ElasticSearch RDBMS 说明
索引(index) 数据库(database) 索引(index)是ElasticSearch存放具体数据的地方,是一类具有相似特征的文档的集合。ElasticSearch中索引的概念具有不同意思,这里的索引相当于关系数据库中的一个数据库实例。在ElasticSearch中索引还可以作为动词,表示对数据进行索引操作。
类型(type) 表(table) 在6.0之前的版本,一个ElasticSearch索引中,可以有多个类型;从6.0版本开始,一个ElasticSearch索引中,只有1个类型,警告!Type在6.0.0版本中已经不赞成使用。而且,7.0版本以后就完全抛弃type了。一个类型是索引的一个逻辑上的分类,通常具有一组相同字段的文档组成。ElasticSearch的类型概念相当于关系数据库的数据表。
文档(document) 行(row) 文档是ElasticSearch可被索引的基础逻辑单元,相当于关系数据库中数据表的一行数据。ElasticSearch的文档具有JSON格式,由多个字段组成,字段相当于关系数据库中列的概念。
Shard 当数据量较大时,索引的存储空间需求超出单个节点磁盘容量的限制,或者出现单个节点处理速度较慢。为了解决这些问题,ElasticSearch将索引中的数据进行切分成多个分片(shard),每个分片存储这个索引的一部分数据,分布在不同节点上。当需要查询索引时,ElasticSearch将查询发送到每个相关分片,之后将查询结果合并,这个过程对ElasticSearch应用来说是透明的,用户感知不到分片的存在。一个索引的分片一定指定,不再修改。
分片与备份 在ES中,索引会备份成分片,每个分片是独立的lucene索引,可以完成搜索分析存储等工作。
副本 其实,分片全称是主分片,简称为分片。主分片是相对于副本来说的,副本是对主分片的一个或多个复制版本(或称拷贝),这些复制版本(拷贝)可以称为复制分片,可以直接称之为副本。当主分片丢失时,集群可以将一个副本升级为新的主分片。
Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。
字段(field) 列(column)
映射(mapping) 表结构(schema)
全文索引 索引
查询DSL SQL
GET select
PUT/POST update
DELETE delete

关于 Type 将会在 7.X 中弃用的原因

official document–> Removal of mapping types
文末有移除 type 的计划,以及每个版本中的用法,很有参考价值:Schedule for removal of mapping types
Elasticsearch 6.x

  • Indices created in 5.x will continue to function in 6.x as they did in 5.x.
  • Indices created in 6.x only allow a single-type per index. Any name can be used for the type, but there can be only one. The preferred type name is _doc, so that index APIs have the same path as they will have in 7.0: PUT {index}/_doc/{id} and POST {index}/_doc
  • The _type name can no longer be combined with the _id to form the _uid field. The _uid field has become an alias for the _id field.
  • New indices no longer support the old-style of parent/child and should use the join field instead.
  • The default mapping type is deprecated.

移除原因,翻译:

你知道的,为了搜索······

我们一直认为ES中的“index”类似于关系型数据库的“database”,而“type”相当于一个数据表。ES的开发者们认为这是一个糟糕的认识。例如:关系型数据库中两个数据表示是独立的,即使他们里面有相同名称的列也不影响使用,但ES中不是这样的。

我们都知道elasticsearch是基于Lucene开发的搜索引擎,而ES中不同type下名称相同的filed最终在Lucene中的处理方式是一样的。举个例子,两个不同type下的两个user_name,在ES同一个索引下其实被认为是同一个filed,你必须在两个不同的type中定义相同的filed映射。否则,不同type中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降。

去掉type能够使数据存储在独立的index中,这样即使有相同的字段名称也不会出现冲突,就像ElasticSearch出现的第一句话一样“你知道的,为了搜索····”,去掉type就是为了提高ES处理数据的效率。

除此之外,在同一个索引的不同type下存储字段数不一样的实体会导致存储中出现稀疏数据,影响Lucene压缩文档的能力,导致ES查询效率的降低。

创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@localhost ~]# curl -XPUT "localhost:9210/test" -d '
{
"settings": {
"analysis": {
"analyzer": { #配置分析器
"csh_analyzer": { #分析器的名字是csh_analyer,这个是系统没有的,我自己定义的一个,可以取一个不和已有的分析器重名的名字
"type": "standard", #这个分析器的类型是基于系统自带的标准的standard分析器
"stopwords": "_english_" #禁用词,或者说无效词范围定义来自_english_列表
}
}
},
index.mapping.single_type: true
},
"mappings": {
"user": { #定义test索引下的一个type为user,注意,6.0 及以后,一个索引下只能有一个 type,7.0以后不会有 type
"properties": { #开始定义这个type的属性值(也可以用fields)
"first_name": { #字段名为first_name
"type": "string", #数据类型为string
"analyzer": "standard" #分析器用系统默认的standard
},
"last_name": { #字段名字为last_name
"type": "string", #字段类型为string
"analyzer": "csh_analyzer" #分析器为自定义的csh_analyzer
},
"job": { #字段名字为job
"type": "string", #字段类型
"analyzer": "csh_analyzer" #分析器为自定义的csh_analyzer
}
}
}
}
}'

查询所有索引列表

1
2
[utomcat@hjweb01 esDemo]$ curl localhost:9200/_cat/indices 
green open pre-es-index uGcKbpOIT7e0l_TS5ShKsw 3 0 4308 15 4mb 4mb

检测集群是否健康

1
2
[utomcat@hjweb01 esDemo]$ curl localhost:9200/_cat/health
1546583744 06:35:44 hjz-es-cluster green 1 1 3 3 0 0 0 0 - 100.0%

获取集群的节点列表

1
2
[utomcat@hjweb01 esDemo]$ curl localhost:9200/_cat/nodes
127.0.0.1 23 89 3 0.57 0.14 0.09 mdi * hjz-es

ElasticSearch 通过 kibana 简单操作

  1. GET _cat/indices 列出所有索引
  2. GET _cat/health 检测集群是否健康
  3. GET _cat/nodes 获取集群的节点列表

查询

1
GET xunwu/_search
1
2
3
4
5
6
7
8
9
10
GET xunwu/_search
{
"query": {
"match": {
"title": {
"query": "富力城"
}
}
}
}

结果为空,因为还没有写入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

ElasticSearch 默认不允许以 root 身份启动,建议创建一个专门的用户来运行

以 root 身份启动,会报错

1
Exception in thread "main" Java.lang.RuntimeException: don't run elasticsearch as root.

解决办法,增加启动参数

1
./elasticsearch -Des.insecure.allow.root=true

或者修改启动文件 ElasticSearch,在 ES_JAVA_OPTS="${JVM_OPTIONS//\$\{ES_TMPDIR\}/$ES_TMPDIR} $ES_JAVA_OPTS" 前添加一行

1
ES_JAVA_OPTS="-Des.insecure.allow.root=true"

如果某个字段内容非常多(比如一篇小说),或者查询业务只需要对该字段进行搜索,返回文档id,然后通过其他途径查看文档原文,则不需要保留_source元字段。可以通过禁用_source元字段,在ElasticSearch 中只存储倒排索引,不保留字段原始值。

通过 Kibana 的 Dev Tools 搜索

1
2
3
4
5
6
7
8
GET hjz-es-index-test/_search
{
"query": {
"term": {
"_id": "20-62"
}
}
}

completion 类型的字段保存问题

1
Caused by: java.lang.IllegalArgumentException: unknown field name [customKey], must be one of [input, weight, contexts]

Completion Suggester

1
2
3
4
5
The following parameters are supported:
input
The input to store, this can be an array of strings or just a string. This field is mandatory.
weight
A positive integer or a string containing a positive integer, which defines a weight and allows you to rank your suggestions. This field is optional.

todo

[x] Completion Suggester 可以被普通搜索吗?
可以

查看内存、磁盘占用情况

1
2
3
4
http://192.168.1.91:9200/_cat/nodes?v&h=http,version,jdk,disk.total,disk.used,disk.avail,disk.used_percent,heap.current,heap.percent,heap.max,ram.current,ram.percent,ram.max,master
---
http version jdk disk.total disk.used disk.avail disk.used_percent heap.current heap.percent heap.max ram.current ram.percent ram.max master
192.168.1.91:9200 6.4.1 1.8.0_171 9.9gb 8.8gb 1.1gb 88.50 475.1mb 47 990.7mb 7.1gb 95 7.5gb *

官网参考链接:cat nodes

1
2
3
http://192.168.1.91:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
13 1.5mb 8.8gb 1.1gb 9.9gb 88 192.168.1.91 192.168.1.91 hjz-es

控制分析器 search_analyzer、analyzer

  • 分析器主要有两种情况会被使用,一种是插入文档时,将text类型的字段做分词然后插入倒排索引,第二种就是在查询时,先对要查询的text类型的输入做分词,再去倒排索引搜索
  • 如果想要让 索引 和 查询 时使用不同的分词器,ElasticSearch也是能支持的,只需要在字段上加上search_analyzer参数
  • 在索引时,只会去看字段有没有定义analyzer,有定义的话就用定义的,没定义就用ES预设的
  • 在查询时,会先去看字段有没有定义search_analyzer,如果没有定义,就去看有没有analyzer,再没有定义,才会去使用ES预设的

数据类型,定义 search_analyzer 为 ik_smart 而 analyzer 为 ik_max_word,这样不至于输入“郫都区”,去匹配“郫”、“都”和“区”,而是直接匹配“郫都区”,这三个字已经添加到自定义分词词库。

数据类型

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html