Elasticsearch 文档

本文最后更新于:2024年3月18日 凌晨

Elasticsearch 文档

  • Elasticsearch 提供单文档API和多文档API,其中API调用分别针对单个文档和多个文档。

创建文档

  • 当使用特定映射对相应索引发出请求时,它有助于在索引中添加或更新 JSON 文档。
  • type 在 Elasticsearch 中逐渐被弃用,使用 _doc 代替指定 type
1
2
3
4
5
6
7
8
9
PUT /users/_doc/1

{
"username": "Tim",
"password": "123456",
"age": 20,
"date": "2000/1/1",
"desc": "I am Tim"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
  • 自动生成 ID:当在索引操作中未指定 ID 时, Elasticsearch 自动为文档生成 ID

自动索引创建

  • 当请求将 JSON 对象添加到特定索引时,如果该索引不存在,那么此 API 会自动创建该索引以及该特定 JSON 对象的基础映射。
  • 可以通过将以下参数的值更改为 false 来禁用此功能,这个值是存在于 elasticsearch.yml 文件中。
1
2
action.auto_create_index: false
index.mapper.dynamic: false
  • 还可以限制自动创建索引,其中通过更改以下参数的值只允许指定模式的索引名称。
1
action.auto_create_index: +acc*,-bank*
  • 其中 + 表示允许, - 表示不允许。

获取文档

  • 通过对特定文档执行 get 请求来帮助提取 JSON 对象。
1
GET user/_doc/1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_version": 3,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"username": "Tim",
"password": "123456",
"age": 20,
"date": "2000/1/1",
"desc": "A person"
}
}
  • 指定版本,然后 Elasticsearch 将仅提取该版本的文档。
1
GET /users/_doc/1?version=3
  • 从该特定文档的结果中指定所需的字段。
1
GET /users/_doc/1?_source=username,password
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_version": 3,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"password": "123456",
"username": "Tim"
}
}
  • 通过在 get 请求中添加 _source 字段来获取结果中的源部分。
1
GET /users/_doc/1/_source
1
2
3
4
5
6
7
{
"username": "Tim",
"password": "123456",
"age": 20,
"date": "2000/1/1",
"desc": "A person"
}

删除文档

  • 删除指定的索引,映射或文档。
1
DELETE /users/_doc/1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_version": 6,
"result": "deleted",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}
  • 可以指定文档的版本以删除指定的版本。
  • 可以指定路由参数以删除指定用户的文档,如果文档不属于该特定用户,则操作将失败。
  • 在此操作中,可以像 GET 那样指定刷新(refresh)和超时(timeout)选项。

更新文档

  • 更新指定的文档。
1
2
3
4
5
6
7
POST /users/_update/1

{
"doc":{
"username": "Mike"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_version": 5,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}

多文档 API

  • 通过在单个请求中进行多个索引/删除操作来批量上传或删除 JSON 对象。

获取多文档

  • 它具有相同的功能,如 GET API,但此 get 请求可以返回多个文档。
  • 使用 docs 数组来指定需要提取的所有文档的索引,类型和 ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
POST /users/_mget

{
"docs":[
{
"_index": "schools",
"_type": "school",
"_id": "1"
},

{
"_index":"schools_gev",
"_type":"school",
"_id": "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
{
"docs":[
{
"_index":"schools", "_type":"school", "_id":"1",
"_version":1, "found":true, "_source":{
"name":"Central School", "description":"CBSE Afiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location":[31.8955385,76.8380405], "fees":2000,
"tags":["Senior Secondary", "beatiful campus"], "rating":"3.5"
}
},

{
"_index":"schools_gev", "_type":"school", "_id":"2", "error":{

"root_cause":[{
"type":"index_not_found_exception", "reason":"no such index",
"index":"schools_gev"
}],

"type":"index_not_found_exception", "reason":"no such index",
"index":"schools_gev"
}
}
]
}

创建

  • 需要添加"_bulk”关键字来调用此 API
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
POST /users/_bulk

{
"index":{
"_index":"schools",
"_type":"school",
"_id":"1"
}
}
{
"name":"Central School",
"description":"CBSE Affiliation",
"street":"Nagan",
"city":"paprola",
"state":"HP",
"zip":"176115",
"location":[31.8955385, 76.8380405],
"fees":2000,
"tags":["Senior Secondary", "beautiful campus"],
"rating":"3.5"
}
{
"index":{
"_index":"schools",
"_type":"school",
"_id":"2"
}
}
{
"name":"Saint Paul School", "description":"ICSE Afiliation",
"street":"Dawarka",
"city":"Delhi",
"state":"Delhi",
"zip":"110075",
"location":[28.5733056, 77.0122136],
"fees":5000,
"tags":["Good Faculty", "Great Sports"],
"rating":"4.5"
}
{
"index":{
"_index":"schools",
"_type":"school",
"_id":"3"}
}
{
"name":"Crescent School",
"description":"State Board Affiliation",
"street":"Tonk Road",
"city":"Jaipur",
"state":"RJ",
"zip":"176114",
"location":[26.8535922, 75.7923988],
"fees":2500,
"tags":["Well equipped labs"],
"rating":"4.5"
}
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
{
"took":328, "errors":false,"items":[
{
"index":{
"_index":"schools",
"_type":"school",
"_id":"1",
"_version":1,
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"status":201
}
},

{
"index":{
"_index": "schools",
"_type": "school",
"_id": "2",
"_version": 1,
"_shards":{
"total": 2,
"successful": 1,
"failed":0
},
"status":201
}
},

{
"index":{
"_index": "schools",
"_type": "school",
"_id": "3",
"_version": 1,
"_shards":{
"total": 2,
"successful": 1,
"failed":0
},
"status":201
}
}
]
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!