有时候可以通过批量操作来减少网络请求。如:批量查询、批量插入数据。
- # 当查询的数据都存在时
- POST /test/user/_mget
-
- # 请求数据
- {
- "ids": [
- "1002",
- "1003"
- ]
- }
-
- # 响应结果
- {
- "docs": [
- {
- "_index": "test",
- "_type": "user",
- "_id": "1002",
- "_version": 5,
- "_seq_no": 10,
- "_primary_term": 1,
- "found": true,
- "_source": {
- "id": 1002,
- "name": "李四",
- "age": 23,
- "sex": "女"
- }
- },
- {
- "_index": "test",
- "_type": "user",
- "_id": "1003",
- "_version": 1,
- "_seq_no": 11,
- "_primary_term": 1,
- "found": true,
- "_source": {
- "id": 1003,
- "name": "王五",
- "age": 27,
- "sex": "男"
- }
- }
- ]
- }
当某一条数据不存在,不影响整体响应,需要通过found的值进行判断是否查询到数据。
- # 当查询的数据有不存在的时候
- POST /test/user/_mget
- # 请求数据
- {
- "ids": [
- "1002",
- "1007"
- ]
- }
-
-
- # 响应结果
- {
- "docs": [
- {
- "_index": "test",
- "_type": "user",
- "_id": "1002",
- "_version": 5,
- "_seq_no": 10,
- "_primary_term": 1,
- "found": true,
- "_source": {
- "id": 1002,
- "name": "李四",
- "age": 23,
- "sex": "女"
- }
- },
- {
- "_index": "test",
- "_type": "user",
- "_id": "1007",
- "found": false
- }
- ]
- }
在Elasticsearch中,支持批量的插入、修改、删除操作,都是通过_bulk的api完成的。 请求格式如下:(请求格式不同寻常)
- { action: { metadata }}\n
- { request body }\n
- { action: { metadata }}\n
- { request body }\n
- ...
批量插入数据:
- {"create":{"_index":"test","_type":"user","_id":2001}}
- {"id":2001,"name":"name1","age": 20,"sex": "男"}
- {"create":{"_index":"test","_type":"user","_id":2002}}
- {"id":2002,"name":"name2","age": 20,"sex": "男"}
- {"create":{"_index":"test","_type":"user","_id":2003}}
- {"id":2003,"name":"name3","age": 20,"sex": "男"}
注意:最后一行要有一个回车。
- POST /test/user/_bulk
-
- #请求数据
- {"create":{"_index":"test","_type":"user","_id":2001}}
- {"id":2001,"name":"name1","age": 20,"sex": "男"}
- {"create":{"_index":"test","_type":"user","_id":2002}}
- {"id":2002,"name":"name2","age": 20,"sex": "男"}
- {"create":{"_index":"test","_type":"user","_id":2003}}
- {"id":2003,"name":"name3","age": 20,"sex": "男"}
-
-
- # 响应结果
- {
- "took": 12,
- "errors": false,
- "items": [
- {
- "create": {
- "_index": "test",
- "_type": "user",
- "_id": "2001",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 13,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "create": {
- "_index": "test",
- "_type": "user",
- "_id": "2002",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 14,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "create": {
- "_index": "test",
- "_type": "user",
- "_id": "2003",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 15,
- "_primary_term": 1,
- "status": 201
- }
- }
- ]
- }
批量删除
- {"delete":{"_index":"test","_type":"user","_id":2001}}
- {"delete":{"_index":"test","_type":"user","_id":2002}}
- {"delete":{"_index":"test","_type":"user","_id":2003}}
- POST /test/user/_bulk
-
- # 请求数据
- {"delete":{"_index":"test","_type":"user","_id":2001}}
- {"delete":{"_index":"test","_type":"user","_id":2002}}
- {"delete":{"_index":"test","_type":"user","_id":2003}}
-
-
- # 响应数据
- {
- "took": 11,
- "errors": false,
- "items": [
- {
- "delete": {
- "_index": "test",
- "_type": "user",
- "_id": "2001",
- "_version": 2,
- "result": "deleted",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 16,
- "_primary_term": 1,
- "status": 200
- }
- },
- {
- "delete": {
- "_index": "test",
- "_type": "user",
- "_id": "2002",
- "_version": 2,
- "result": "deleted",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 17,
- "_primary_term": 1,
- "status": 200
- }
- },
- {
- "delete": {
- "_index": "test",
- "_type": "user",
- "_id": "2003",
- "_version": 2,
- "result": "deleted",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 18,
- "_primary_term": 1,
- "status": 200
- }
- }
- ]
- }
ElasticSearch一次请求多少性能最高?