『ElasticSearch』- Introduction & QuickStart

简介:

Elasticsearch是一个高度可扩展的开源全文搜索和分析引擎。它允许您快速、实时地存储、搜索和分析大量数据。它通常被用作支持具有复杂搜索特性和需求的应用的底层引擎/技术。

官网:https://www.elastic.co/

文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

版本:

截止目前的最新版是 elasticsearch-6.2.4

安装:

下载地址:https://www.elastic.co/downloads
在官网的下载页面有提供安装方式:

CLI参数:

Option Description
-E 配置设置
-V, –version 打印ES版本信息
-d, –daemonize 以守护进程运行ES
-h, –help 查看帮助
-p, –pidfile 在指定的路径上在开始时创建一个PID文件
-q, –quiet 关闭控制台中的标准输出/错误流日志
-s, –silent 显示最小输出
-v, –verbose 显示冗长输出

单实例安装:

1
2
3
➜  ~ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
➜ ~ tar -xzf elasticsearch-6.2.4.tar.gz
➜ ~ cd elasticsearch-6.2.4/

ES可以从命令行启动,如下所示:

1
➜  elasticsearch ./bin/elasticsearch

集群安装:

创建三个文件夹,依次将elasticsearch-6.2.4.tar.gz解压到三个文件夹中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  ~ mkdir {es-master,es-slave1,es-slave2}
➜ ~ tar -vxf elasticsearch-6.2.4.tar.gz -C ~/elasticsearch/es-master
➜ ~ tar -vxf elasticsearch-6.2.4.tar.gz -C ~/elasticsearch/es-slave1
➜ ~ tar -vxf elasticsearch-6.2.4.tar.gz -C ~/elasticsearch/es-slave2
➜ ~ cd elasticsearch

# 其它如法炮制
➜ elasticsearch cd es-slave1
➜ es-slave1 mv elasticsearch-6.2.4/* .
➜ es-slave1 ls
LICENSE.txt bin lib plugins
NOTICE.txt config logs
README.textile elasticsearch-6.2.4 modules
➜ es-slave1 rm -rf elasticsearch-6.2.4

修改es-master/config/elasticsearch.yml文件, 在结尾追加以下内容:

1
2
3
4
5
6
7
8
9
10
➜  es-master vim es-master/config/elasticsearch.yml

# 追加以下内容
http.cors.enabled: true
http.cors.allow-origin: "*"

cluster.name: Yang
node.name: master
node.master: true
network.host: 127.0.0.1

修改es-slave1/config/elasticsearch.yml文件,在结尾追加以下内容:

1
2
3
4
5
6
7
8
9
10
➜  es-slave1 vim es-slave1/config/elasticsearch.yml

# 追加以下内容
cluster.name: Yang
node.name: slave1

network.host: 127.0.0.1
http.port: 9201

discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

修改es-slave2/config/elasticsearch.yml文件,在结尾追加以下内容:

1
2
3
4
5
6
7
8
9
10
➜  es-slave2 vim config/elasticsearch.yml

# 追加以下内容
cluster.name: Yang
node.name: slave2

network.host: 127.0.0.1
http.port: 9202

discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

接下来分别按照下面步骤验证是否安装成功。

验证安装:

默认情况下,Elasticsearch在前台运行,会将其日志打印到Console输出,并可通过按停止Ctrl-C

需要ES在后台运行,可以再启动时候加上-d参数:

1
➜  ~ ./bin/elasticsearch -d

查看是否正常运行可以访问http://localhost:9200,或者在控制台查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  ~ curl 'http://localhost:9200/?pretty'
{
"name" : "mF61KCm",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "MmELAlGuQVGLgLDjE4bZ2Q",
"version" : {
"number" : "6.2.4",
"build_hash" : "ccec39f",
"build_date" : "2018-04-12T20:37:28.497551Z",
"build_snapshot" : false,
"lucene_version" : "7.2.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

如果输出以上内容 则说明安装成功。

插件安装:

elasticsearch-head:

下载地址:https://github.com/mobz/elasticsearch-head

安装步骤:

1
2
3
4
➜  ~ git clone git://github.com/mobz/elasticsearch-head.git
➜ ~ cd elasticsearch-head
➜ ~ npm install
➜ ~ npm run start

启动成功之后访问:http://localhost:9100

kibana:

下载地址:https://www.elastic.co/downloads/kibana

安装步骤:

1
2
3
➜  ~  tar -zxvf kibana-6.2.4-darwin-x86_64.tar.gz -C elasticsearch/
➜ ~ cd elasticsearch && mv kibana-6.2.4-darwin-x86_64 kibana
➜ kibana ./bin/kibana # 启动Kibana

启动成功之后 访问:http://localhost:5601

官方的安装步骤上讲在启动前,要修改./config/kibana.yml文件中的elasticsearch.url,但是elasticsearch.url的默认值是http://localhost:9200,所以此处不做修改。

值得注意的是,kibana在下载的时候,一定要选择与自己当前系统相对应的版本,不然……

集群管理:

集群健康状态:

可以再kibana中使用GET /_cat/health?v来查看集群的健康状态:

1
2
3
4
5
GET /_cat/health?v

# 返回结果:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1527521915 23:38:35 Yang green 3 3 0 0 0 0 0 0 - 100.0%

集群的健康状态status有三种状态:

green:每个索引的primary shard和replica shard都是active状态的
yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了

API操作:

API 说明
GET /_cat/health?v 检查集群健康状态
GET /_cat/indices?v 查看集群中有哪些索引
GET _cluster/state 检查集群状态信息
GET /_cat/master?v 查看master的节点ID,绑定IP地址,节点名称
GET /_cat/nodes?v 输出当前集群的拓扑结构

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/cat.html

索引操作:

创建索引:

1
2
3
4
5
6
7
8
PUT /test_index?pretty

# 返回结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}

查看索引:

1
2
3
4
5
GET /_cat/indices?v

# 返回结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open test_index oDZBadoQQAuxMVb7ekUl5g 5 1 0 0 2.2kb 1.1kb

删除索引:

1
2
3
4
5
6
DELETE /test_index?pretty

# 返回结果:
{
"acknowledged": true
}

CRUD:

新增文档:

新增文档使用PUTAPI来操作。

语法:PUT /index/type/id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
PUT /shop/product/1
{
"name":"Thinking in Java",
"desc":"Java Conding API Book",
"price":120,
"from":"Machinery Industry Press",
"tags":["Java","Coding"]
}

# 返回结果:
{
"_index": "shop",
"_type": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}

替换文档:

替换文档同样可以使用PUTAPI来操作,注意每次完成后返回结果的_version会发生变化。

语法:PUT /index/type/id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
PUT /shop/product/1
{
"name":"Thinking in Java",
"desc":"Java Conding API Book",
"price":119,
"from":"Machinery Industry Press",
"tags":["Java","Coding"]
}

# 返回结果:
{
"_index": "shop",
"_type": "product",
"_id": "1",
"_version": 2, # 由1变为2
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}

修改文档:

修改文档使用POSTAPI来操作。

语法:POST /index/type/id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
POST /shop/product/1/_update
{
"doc": {
"price":108
}
}

# 返回结果:
{
"_index": "shop",
"_type": "product",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 1
}

检索文档:

检索文档使用GETAPI来操作。

语法:GET /index/type/id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
GET /shop/product/1

# 返回结果:
{
"_index": "shop",
"_type": "product",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "Thinking in Java",
"desc": "Java Conding API Book",
"price": 119,
"from": "Machinery Industry Press",
"tags": [
"Java",
"Coding"
]
}
}

删除文档:

删除文档使用 DELETEAPI来操作:

语法:DELETE /index/type/id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DELETE /shop/product/1

# 返回结果:
{
"_index": "shop",
"_type": "product",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}

es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索。