跳到主要内容

索引写入 bulk

创建索引

创建一个索引,包含2个分片1份副本(settings),指定了2个字段类型(mappings) 。

PUT greeting
{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 1
},
"mappings" : {
"_doc" : {
"properties" : {
"email" : { "type" : "keyword" },
"message" : { "type" : "text" }
}
}
}
}

写入索引(单条)

POST greeting/_doc
{
"email" : "greeting@nasuyun.com",
"message" : "hello world"
}

写入索引(指定ID)

POST greeting/_doc/1
{
"email" : "greeting@nasuyun.com",
"message" : "hello world"
}

Bulk 批量写入

POST greeting/_bulk
{ "index" : { "_type" : "_doc"} }
{ "email" : "g1@nasuyun.com","message":"hello1" }
{ "index" : { "_type" : "_doc"} }
{ "email" : "g2@nasuyun.com","message":"hello2" }
{ "index" : { "_type" : "_doc"} }
{ "email" : "g3@nasuyun.com","message":"hello3" }
{ "index" : { "_type" : "_doc"} }
{ "email" : "g4@nasuyun.com","message":"hello4" }

更复杂的一个例子,在body内指定 _index, _type , _id , 并增加了删除更新等操作。

POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }