Elasticsearch - 文档 CRUD

Caret Up

First

  • blogs 为我定义的索引名称

Create

POST blogs/_doc

PUT blogs/_create/1

PUT blogs/_doc/1?op_type=create

1 为文档id

1
2
3
4
{
  "title": "foo",
  "content": "bar"
}
  • 支持自动生成文档id和指定文档id两种方式
  • 通过调用 POST /blogs/_doc 系统会自动生成 document ID
  • 通过调用 PUT /blogs/_create/1 创建时,URI中显式指定 _create ,此时如果该 id 的文档已经存在,操作失败
  • 不存在的字段会自动创建

Get

GET blogs/_doc/1

1 为文档id

  • 成功找到文档 HTTP 200
  • 找不到文档返回 HTTP 404

Index

PUT blogs/_doc/1

1 为文档id

index 和 create 不一样的地方:

  • 如果文档不存在,就索引新的文档
  • 否则,现有文档会被删除,新的文档被索引。同时版本信息 +1

example

旧的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "_index": "blogs",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "title": "11",
        "content": "22"
    }
}

调用index后新的:

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

Update

POST blogs/_update/1

PUT blogs/_doc/1

1
2
3
4
5
6
{
    "doc": {
        "title": "11",
        "33": "33333"
    }
}
  • update 方法不会删除原来的文档,而是实现真正的数据更新
  • 可用于已有字段的更新或新增字段
  • Post 方法 / Payload 需要包含在 doc
  • 更新成功后 _version 会 +1

Bulk API

  • 支持在一次API调用中,对不同的索引进行操作

  • 支持四种类型操作

    • Index

    • Create

    • Update

    • Delete

  • 可以在 URI 中指定 Index,也可以在请求的 Payload 中指定

  • 操作中单条操作失败,不会影响到其他操作。会立即报错。

  • 返回结果包括了每一条操作执行的结果

  • json 中的最后一行必须空一行

example

POST _bulk

1
2
3
4
# 格式
# action: index create update delete
{action:{条件}}
{具体的字段,跟单独调用api一致}
1
2
3
4
5
6
7
{"index":{"_index":"test","_id":"1"}}
{"field1":"value1"}
{"delete":{"_index":"test","_id":"2"}}
{"create":{"_index":"test"}}
{"field1":"create"}
{"update":{"_index":"test","_id":"1"}}
{"doc":{"field1":"value2"}}
 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
68
69
70
71
72
73
74
{
	"took": 63,
	"errors": false,
	"items": [
		{
			"index": {
				"_index": "test",
				"_type": "_doc",
				"_id": "1",
				"_version": 4,
				"result": "updated",
				"_shards": {
					"total": 2,
					"successful": 2,
					"failed": 0
				},
				"_seq_no": 6,
				"_primary_term": 1,
				"status": 200
			}
		},
		{
			"delete": {
				"_index": "test",
				"_type": "_doc",
				"_id": "2",
				"_version": 1,
				"result": "not_found",
				"_shards": {
					"total": 2,
					"successful": 2,
					"failed": 0
				},
				"_seq_no": 7,
				"_primary_term": 1,
				"status": 404
			}
		},
		{
			"create": {
				"_index": "test",
				"_type": "_doc",
				"_id": "4ynq8H0BfpRI0KbW0ChT",
				"_version": 1,
				"result": "created",
				"_shards": {
					"total": 2,
					"successful": 2,
					"failed": 0
				},
				"_seq_no": 8,
				"_primary_term": 1,
				"status": 201
			}
		},
		{
			"update": {
				"_index": "test",
				"_type": "_doc",
				"_id": "1",
				"_version": 5,
				"result": "updated",
				"_shards": {
					"total": 2,
					"successful": 2,
					"failed": 0
				},
				"_seq_no": 9,
				"_primary_term": 1,
				"status": 200
			}
		}
	]
}

Mget

POST _mget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "docs": [
        {
            "_index": "test",
            "_id": "1"
        },
        {
            "_index": "test",
            "_id": "2"
        }
    ]
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
	"docs": [
		{
			"_index": "test",
			"_type": "_doc",
			"_id": "1",
			"_version": 5,
			"_seq_no": 9,
			"_primary_term": 1,
			"found": true,
			"_source": {
				"field1": "value2"
			}
		},
		{
			"_index": "test",
			"_type": "_doc",
			"_id": "2",
			"found": false
		}
	]
}

MSearch

POST blogs/_msearch

1
2
3
4
5
6
{}
{"query":{"match_all":{}},"from":0,"size":10} // 从第一条开始,返回10条
{}
{"query":{"match_all":{}}} // 返回所有
{"index":"twitter"} // 切换index
{"query":{"match_all":{}}} // 返回所有
  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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{
	"took": 16,
	"responses": [
		{
			"took": 16,
			"timed_out": false,
			"_shards": {
				"total": 3,
				"successful": 3,
				"skipped": 0,
				"failed": 0
			},
			"hits": {
				"total": {
					"value": 5,
					"relation": "eq"
				},
				"max_score": 1,
				"hits": [
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "Zyl97X0BfpRI0KbWBSiy",
						"_score": 1,
						"_source": {
							"33": "22",
							"title": "11"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "XSl37X0BfpRI0KbWnyje",
						"_score": 1,
						"_source": {
							"title": "11",
							"content": "22"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "2",
						"_score": 1,
						"_source": {
							"title": "11",
							"content": "22"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "3SnU8H0BfpRI0KbWeyiQ",
						"_score": 1,
						"_source": {
							"title": "ddd",
							"content": "ssss"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "1",
						"_score": 1,
						"_source": {
							"title": "ddd",
							"content": "ssss"
						}
					}
				]
			},
			"status": 200
		},
		{
			"took": 15,
			"timed_out": false,
			"_shards": {
				"total": 3,
				"successful": 3,
				"skipped": 0,
				"failed": 0
			},
			"hits": {
				"total": {
					"value": 5,
					"relation": "eq"
				},
				"max_score": 1,
				"hits": [
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "Zyl97X0BfpRI0KbWBSiy",
						"_score": 1,
						"_source": {
							"33": "22",
							"title": "11"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "XSl37X0BfpRI0KbWnyje",
						"_score": 1,
						"_source": {
							"title": "11",
							"content": "22"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "2",
						"_score": 1,
						"_source": {
							"title": "11",
							"content": "22"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "3SnU8H0BfpRI0KbWeyiQ",
						"_score": 1,
						"_source": {
							"title": "ddd",
							"content": "ssss"
						}
					},
					{
						"_index": "blogs",
						"_type": "_doc",
						"_id": "1",
						"_score": 1,
						"_source": {
							"title": "ddd",
							"content": "ssss"
						}
					}
				]
			},
			"status": 200
		},
		{
			"took": 7,
			"timed_out": false,
			"_shards": {
				"total": 1,
				"successful": 1,
				"skipped": 0,
				"failed": 0
			},
			"hits": {
				"total": {
					"value": 100,
					"relation": "eq"
				},
				"max_score": 1,
				"hits": [
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "dSnN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 69,
							"author_name": "Edgar Brush",
							"message": "Geology rocks, but Geography is where it's at!",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "dinN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 43,
							"author_name": "Elvira Rain",
							"message": "I really want to buy one of those supermarket checkout dividers, but the cashier keeps putting it back.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "dynN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 71,
							"author_name": "Junie Gerard",
							"message": "Why did the girl smear peanut butter on the road? To go with the traffic jam.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "eCnN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 97,
							"author_name": "Kayla Vanguilder",
							"message": "Why was the broom late for the meeting? He overswept.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "eSnN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 43,
							"author_name": "Claud Granderson",
							"message": "I went to the doctor today and he told me I had type A blood but it was a type O.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "einN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 25,
							"author_name": "Fleta Figgins",
							"message": "Why do you never see elephants hiding in trees? Because they're so good at it.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "eynN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 64,
							"author_name": "Glenna Pizarro",
							"message": "What’s the advantage of living in Switzerland? Well, the flag is a big plus.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "fCnN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 92,
							"author_name": "Jared Mcelravy",
							"message": "A book just fell on my head. I only have my shelf to blame.",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "fSnN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 38,
							"author_name": "Dong Rierson",
							"message": "I went to a Foo Fighters Concert once... It was Everlong...",
							"created_at": "2009-11-15T14:12:12"
						}
					},
					{
						"_index": "twitter",
						"_type": "tweet",
						"_id": "finN8H0BfpRI0KbWwygB",
						"_score": 1,
						"_source": {
							"author_id": 99,
							"author_name": "Jules Basch",
							"message": "Two peanuts were walking down the street. One was a salted.",
							"created_at": "2009-11-15T14:12:12"
						}
					}
				]
			},
			"status": 200
		}
	]
}

常见错误

200 请求成功

4xx 请求格式错误

429 集群过于繁忙 (重试请求或增加集群节点)

500 集群内部错误