Elasticsearch常用API使用

以下API的使用均为在Kinbana的DevTools上面的运行方式,如果你使用的的CURL命令请自行转换为对应格式。

Index相关API

ES 有专门的index API ,用于创建,更新,删除索引配置等,创建API如下:

1
put /test_index

查看现有索引

1
GET _cat/indices

删除索引

1
DELETE /test_index

Document API

Elasticsearch也有丰富的Document API用于操作文档。

创建文档

创建文档时,如果索引不存在,es会自动创建对应的,创建文档时可以指定文档id,也可以由es来自动生成文档id。

指定id创建文档

index 和type
ES6.1以后版本默认type使用doc 类型
_version记录了文档的变化操作,操作文档_version会 +1,也是一种锁的机制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PUT /test_index/_doc/1
{
"username": "yangzhiheng",
"age": 26
}
结果输出
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}

不指定id创建文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
POST /test_index/_doc
{
"username":"zachary",
"age":27
}
结果输出
ES会生成一个id acOPvWwBDmk_0NAxXsMk
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "acOPvWwBDmk_0NAxXsMk",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}

查询文档

查询文档

指定要查询的文档ID

_source存储了文档的完整原始数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
GET /test_index/_doc/1

结果输出
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"username" : "yangzhiheng",
"age" : 26
}
}
1
2
3
4
5
6
7
8
9
查询的文档不存在时http_code为404,返回值found为false
GET /test_index/_doc/2
结果输出:
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "2",
"found" : false
}

搜索所有文档,用到_search API, 简单用法如下

查询语句,json格式,放在http body中发送到ES。
以下是返回的字段含义:

  • took: 查询耗时,ms
  • hits: 命中文档信息
  • hits[“total”]: 符合条件的总文档数
  • hits[“hits”] :返回的文档详情数据list,默认为前10个文档
  • _index:索引名
  • _id:文档ID
  • _score:文档得分
  • _source: 文档详情
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
GET /test_index/_search
{
"query":{
"term":{
"_id":1
}
}
}
结果输出:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "yangzhiheng",
"age" : 26
}
}
]
}
}

批量创建文档API

es允许一次创建多个文档,从而减少网络传输开销,提升写入速率
endpoint为_bulk 如下

  • action_type: index update , create, delete
    index和create的区别
  • create只创建,如果文档存在,报错
  • index也为创建,如果文档存在,覆盖
    1
    2
    3
    4
    5
    6
    POST _bulk
    {"index":{"_index":"test_index","_id":"3"}}
    {"username":"yangzhiheng","age":26}
    {"delete":{"_index":"test_index","_id":"1"}}
    {"update":{"_id":"3","_index":"test_index"}}
    {"doc":{"age":27}}

批量查询文档API

es允许一次查询多个文档
endpoint为_mget
docs为数组list,可以获取不同index的文档。

1
2
3
4
5
6
7
8
9
10
11
12
13
GET /_mget
{
"docs":[
{
"_index":"test_index",
"_id":"1"
},
{
"_index":"test_index",
"_id":"3"
}
]
}

更新文档

更新文档使用PUT方法,指定对应的文档id
文档更新成功后result为updated,_version加1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PUT /test_index/_doc/1
{
"username":"yangzhiheng"
}
结果输出
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 3
}

删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
test_index/_doc/1
结果输出
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 15,
"_primary_term" : 3
}