ElasticSearch基本操作

es对文档的基本RestFul操作汇总

操作类型 请求方式 URL 格式 说明 示例
创建文档 PUT /{index}/_doc/{id} 指定 ID 创建文档,若 ID 已存在则覆盖 PUT /users/_doc/1携带 JSON 文档:{"name":"张三","age":30}
创建文档(自动生成 ID) POST /{index}/_doc 不指定 ID,ES 自动生成唯一 ID POST /users/_doc携带 JSON 文档:{"name":"李四","age":25}
查看文档 GET /{index}/_doc/{id} 通过 ID 查询单个文档 GET /users/_doc/1
更新文档(全量替换) PUT /{index}/_doc/{id} 同 “指定 ID 创建”,若文档存在则全量替换 PUT /users/_doc/1携带新 JSON 文档
更新文档(部分字段) POST /{index}/_doc/{id}/_update 仅更新指定字段 POST /users/_update/1携带:{"doc":{"age":31}}
删除文档 DELETE /{index}/_doc/{id} 通过 ID 删除文档 DELETE /users/_doc/1
批量操作 POST /_bulk 批量执行创建、更新、删除(需按特定格式) 格式示例:{"index":{"_index":"users","_id":"3"}}``{"name":"王五","age":28}

测试

  1. 创建或全量替换文档
1
2
3
4
5
PUT /index/_doc/1
{
"name": "zhangsan",
"age": 23
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}

  1. 创建索引并定义映射
1
2
3
4
5
6
7
8
9
10
11
12
13
PUT /index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
}
}
}
}
  1. 查询索引信息
1
GET /index
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
{
"index" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1758529598685",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "t1PmwRFQRUSe0ixcmWUMbg",
"version" : {
"created" : "7060199"
},
"provided_name" : "index"
}
}
}
}
  1. 部分更新文档
1
2
3
4
5
6
POST /index/_doc/1/_update
{
"doc": {
"name": "lisi"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
  1. 删除索引
1
DELETE /index
  1. ID查询文档
1
GET /index/_doc/1
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "lisi",
"age" : 23
}
}

带条件的复杂查询

  1. 关键字查询
1
GET /index/_doc/_search?q=name:lisi
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
{
"took" : 27,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "lisi",
"age" : 23
}
}
]
}
}
  1. 结构化的关键字查询
1
2
3
4
5
6
7
8
GET /index/_doc/_search
{
"query": {
"match": {
"name": "lisi"
}
}
}
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
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "lisi",
"age" : 23
}
}
]
}
}
  1. 指定返回字段的查询
1
2
3
4
5
6
7
8
9
10
11
12
GET /index/_doc/_search
{
"query": {
"match": {
"name": "狂神"
}
},
"_source": [
"name",
"desc"
]
}
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
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.86891425,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.86891425,
"_source" : {
"name" : "狂神说java",
"desc" : "一顿操作猛如虎,一看工资2500"
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.78038335,
"_source" : {
"name" : "狂神说前端",
"desc" : "青涩大男孩"
}
}
]
}
}
  1. 对结果排序
1
2
3
4
5
6
7
8
9
10
11
12
13
GET /index/_doc/_search
{
"query": {
"match": {
"name": "狂神"
}
},
"sort": {
"age": {
"order": "asc"
}
}
}
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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "狂神说java",
"age" : 25,
"desc" : "一顿操作猛如虎,一看工资2500"
},
"sort" : [
25
]
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "狂神说前端",
"age" : 27,
"desc" : "阳光开朗大男孩"
},
"sort" : [
27
]
}
]
}
}
  1. 分页
1
2
3
4
5
6
7
8
GET /index/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
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
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "lisi",
"age" : 23
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "狂神说java",
"age" : 25,
"desc" : "一顿操作猛如虎,一看工资2500"
}
}
]
}
}
  1. 与条件查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

GET /index/_doc/_search
{
"query": {
"match": {
"bool": {
"must": [
{
"match": {
"name": "狂神"
}
},
{
"match": {
"age": 23
}
}
]
}
}
}
}
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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.6944115,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.6944115,
"_source" : {
"name" : "狂神说java",
"age" : 25,
"desc" : "一顿操作猛如虎,一看工资2500"
}
}
]
}
}
  1. 或条件查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GET /index/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "前端"
}
},
{
"match": {
"name": "lisi"
}
}
]
}
}
}
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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.7199613,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.7199613,
"_source" : {
"name" : "lisi",
"age" : 23
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.2199391,
"_source" : {
"name" : "狂神说前端",
"age" : 27,
"desc" : "阳光开朗大男孩"
}
}
]
}
}
  1. 非条件查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
GET /index/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "狂神"
}
}
]
}
}
}
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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"name" : "lisi",
"age" : 23
}
}
]
}
}
  1. 结果过滤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
GET /index/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "狂神"
}
}
],
"filter": {
"range": {
"age": {
"gt": 20,
"lt": 26
}
}
}
}
}
}
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
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.69441146,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.69441146,
"_source" : {
"name" : "狂神说java",
"age" : 25,
"desc" : "一顿操作猛如虎,一看工资2500"
}
}
]
}
}
  1. 数组或条件匹配
1
2
3
4
5
6
7
8
GET /index/_doc/_search
{
"query": {
"match": {
"name": "狂神 lisi"
}
}
}
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
{
"took" : 320,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.8132977,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.8132977,
"_source" : {
"name" : "张三",
"age" : 23,
"desc" : "法外狂徒",
"tags" : [
"刑法",
"国家饭碗",
"惯犯"
]
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.49005115,
"_source" : {
"name" : "狂神说前端",
"age" : 25,
"desc" : "青涩大男孩",
"tags" : [
"暖男",
"技术",
"摄影"
]
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.49005115,
"_source" : {
"name" : "狂神说java",
"age" : 27,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"渣男",
"海王",
"交友"
]
}
}
]
}
}
  1. 精确查询
1
2
3
4
5
6
7
8
GET /index/_doc/_search
{
"query": {
"term": {
"name": "狂神说java" //要求name为keyword或使用name的.keyword子字段
}
}
}
  1. 关键词高亮
1
2
3
4
5
6
7
8
9
10
11
12
13
GET /index/_doc/_search
{
"query": {
"match": {
"name": "狂神"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
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
{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.90630186,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.90630186,
"_source" : {
"name" : "狂神说java",
"age" : 27,
"desc" : "一顿操作猛如虎,一看工资2500",
"tags" : [
"渣男",
"海王",
"交友"
]
},
"highlight" : {
"name" : [
"<em>狂</em><em>神</em>说java"
]
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.8182796,
"_source" : {
"name" : "狂神说前端",
"age" : 25,
"desc" : "青涩大男孩",
"tags" : [
"暖男",
"技术",
"摄影"
]
},
"highlight" : {
"name" : [
"<em>狂</em><em>神</em>说前端"
]
}
}
]
}
}