到目前为止,您已经一次索引了一个文档。这对于玩乐来说很好,但它意味着至少从两个方向的性能损失:
您的应用程序必须等待 Elasticsearch 的回复,然后才能继续。
Elasticsearch 必须处理来自每个索引文档的请求中的所有数据。
如果您需要更高的索引速度,Elasticsearch 提供了一个批量 API,您可以使用它一次为多个文档编制索引
在之前我们的查询都是单条查询,如果查询多条就需要进行多次的IO,这样的效率不高,这时我们可以通过mget来进行批量查询,如下
Copy # 查询 index为megacorp id为 1,2的数据
GET /_mget
{
"docs": [
{
"_index": "megacorp",
"_id": "1"
},
{
"_index": "megacorp",
"_id": "2",
"_source" :[
"first_name" ,
"last_name"
]
}
]
}
Copy GET /megacorp/_mget
{
"ids": [
1,
2
]
}
可以说mget是很重要的,一般来说,在进行查询的时候,如果一次性要查询多条数据的话,那么一定要用batch批量操作的api 尽可能减少网络开销次数,可能可以将性能提升数倍,甚至数十倍,非常非常之重要
Copy header\n
body\n
header\n
body
Copy GET /_msearch
{"index":"megacorp"}
{"query":{"match":{"about":"I love"}}}
{"index":"megacorp"}
{"aggs":{"all_interests":{"terms":{"field": "interests.keyword"}}}}
Copy GET /megacorp/_msearch
{}
{"query":{"match":{"about":"I love"}}}
{}
{"aggs":{"all_interests":{"terms":{"field": "interests.keyword"}}}}
Copy {
"responses":[<body1Response>,<body2Response>, ...]
}
Copy POST _bulk
{"delete":{"_index":"megacorp","_id":30}}
{"create":{"_index":"megacorp","_id":5}}
{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}
{"create":{"_index":"megacorp","_id":6}}
{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}
{"index":{"_index":"megacorp","_id":7}}
{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}
{"index":{"_index":"megacorp","_id":8}}
{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}
{"update":{"_index":"megacorp","_id":1}}
{"doc":{"age":55}}
Copy {
"took" : 30,
"errors" : true,
"items" : [
{
"delete" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "30",
"_version" : 2,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 18,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "5",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[5]: version conflict, document already exists (current version [3])",
"index_uuid" : "HJSUaB6lSWKLKfY0treFXg",
"shard" : "0",
"index" : "megacorp"
}
}
},
{
"create" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "6",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[6]: version conflict, document already exists (current version [3])",
"index_uuid" : "HJSUaB6lSWKLKfY0treFXg",
"shard" : "0",
"index" : "megacorp"
}
}
},
{
"index" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "7",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 19,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "8",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 20,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "megacorp",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "noop",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1,
"status" : 200
}
}
]
}
Copy POST /megacorp/_bulk
{"delete":{"_id":30}}
{"delete":{"_id":31}}
{"delete":{"_id":32}}