ElasticSearch入门

简介及使用方式~

官方文档 中文社区

什么是ElasticSearch

  • 基于Apache Lucene构建的开源搜索引擎
  • 采用Java编写,提供简单易用的RESTFul API
  • 轻松的横向扩展,支持PB级别的结构化或非结构化数据

常见术语

文档 Document

用户存储在es中的数据

Json Object,由字段(Field)组成,常见数据类型如下

  • 字符串:text,keyword
  • 数值类型:long,integer,short,byte,double,float,half_float,scaled_float
  • 布尔:boolean
  • 日期:date
  • 二进制:binary
  • 范围类型:integer_range,float_range,long_range,double_range,date_range

每个文档有唯一的id标识

  • 自行生成
  • es自动生成

Document MetaData 元数据,用于标注文档的相关信息

  • _index:文档所在的索引名
  • _type:文档所在的类型名
  • _id:文档唯一id
  • _uid:组合id,由 _type和 _id组成(6.x _type不再起作用,同 _id一样)
  • _source:文档的原始Json数据,可以从这里获取每个字段的内容
  • _all:整合所有字段内容到该字段,默认禁用

索引 Index

由具有相同字段的文档列表组成

索引中存储具有相同结构的文档

  • 每个索引都有自己的mapping定义,用于定义字段名和类型

一个集群可以有多个索引

节点 Node

一个Elasticsearch的运行实例

集群 Cluster

由一个或多个节点组成,对外服务

Rest API

  • REST - REpresentational State Transfer
  • URI指定资源,如Index,Document等
  • Http Method 指明资源操作类型,如GET,POST,PUT,DELETE等
  • API基本格式 http://:/<索引>/<类型>/<文档id>
  • 常用HTTP动词GET/PUT/POST/DELETE

常见的交互方式

  • Curl命令行
  • Kibana DevTools

索引 API

es有专门的Index API,用于创建,更新,删除索引配置等

创建索引
1
2
3
4
5
6
7
8
9
//request
PUT /test_index

//response
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}
查看现有索引
1
2
3
4
5
//request
GET _cat/indices

//response
green open test_index qnsE8tAgTFW6oJlYfRKh9A 5 1 0 0 2.2kb 1.1kb
删除索引
1
2
3
4
5
6
7
//request
DELETE /test_index

//response
{
"acknowledged": true
}

文档 API

创建文档

如果索引不存在则会自动创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//request
PUT /test_index/doc/1 //PUT/index/type/id
{
"username":"liupeng",
"age":1
}

//response
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询文档
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
//request
GET /test_index/doc/_search
{
"query": {
"term": {
"_id": "1"
}
}
}

//response
{
"took": 33, //查询耗时
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1, //符合条件总数
"max_score": 1,
"hits": [ //返回的文档详情数据数组,默认前10个文档
{
"_index": "test_index", //索引名
"_type": "doc",
"_id": "1", //文档id
"_score": 1, //文档得分
"_source": { //文档详情
"username": "liupeng",
"age": 1
}
}
]
}
}
批量创建文档

es一次创建多个文档,从而减少网络传输开销,提升写入速率

endpoint 为_bulk

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
//request
POST _bulk
{"index":{"_index":"test_index","_type":"doc","_id":"3"}}
{"username":"alfred","age":"10"}
{"delete":{"_index":"test_index","_type":"doc","_id":"1"}}
{"update":{"_id":"2","_index":"test_index","_type":"doc"}}
{"doc":{"age":"20"}}

//response
{
"took": 441,
"errors": true, //结果
"items": [
{
"index": {
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"delete": {
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 404
}
},
{
"update": {
"_index": "test_index",
"_type": "doc",
"_id": "2",
"status": 404,
"error": {
"type": "document_missing_exception",
"reason": "[doc][2]: document missing",
"index_uuid": "yfEfCn2cSrKb0qCX8Ide4g",
"shard": "2",
"index": "test_index"
}
}
}
]
}
批量查询文档

es允许一次查询多个文档

endpoint为 _maget

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
//request
GET /_mget
{
"docs":[
{
"_index":"test_index",
"_type":"doc",
"_id":"1"
},
{
"_index":"test_index",
"_type":"doc",
"_id":"2"
}
]
}

//response
{
"docs": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"username": "liupeng",
"age": 1
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"found": false
}
]
}