通常我们在操作es的时候,都是一个一个http命令发送来实现的,如果想一次执行多个操作,可以使用bulk api。
_bulk api支持4中常见的操作:
index 写入文档
create 创建索引
delete 删除索引
update 更新文档
我们提前将需要执行的操作写入到一个文本文件中,每个操作后通过换行结束。其中create和index可以在该行的下面加入一个数据行,作为该操作传入的数据。
{ "index" : { "_index" : "students", "_type" : "students", "_id" : "1" } }
{ "name" : "pengpeng" }
{ "delete" : { "_index" : "students", "_type" : "students", "_id" : "1" } }
{ "create" : { "_index" : "teachers", "_type" : "teachers", "_id" : "3" } }
{ "name" : "liming" }
{ "update" : {"_id" : "1", "_type" : "teachers", "_index" : "teachers"} }
{ "doc" : {"name" : "liming2"} }
如上将要执行的所有操作写入到一个txt文档中,这里命名test,然后使用如下命令执行test中的操作:
curl -XGET 127.0.0.1:9200/_bulk?v --data-binary "@test"
不过我在使用create创建索引的时候,通过如下方式指定分片数,出现错误:
{ "create" : { "_index" : "teachers", "_type" : "teachers", "_id" : "3", "settings":{ "number_of_shards":5, "number_of_replicas":1} } }
{ "name" : "liming" }
出现如下错误:
{"error":"IllegalArgumentException[Malformed action/metadata line [1], expected a simple value for field [settings] but found [START_OBJECT]]","status":500}
好像在命令行中,不能用对象作为参数,但是没有找到其他指定分片数的方法。