新建文档:
# curl -XPOST node3:9200/blog/article/1?pretty -d '{
"id":1,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}
如果不在url中指定ID的话,系统会自动生成:
# curl -XPOST node3:9200/blog/article?pretty -d '{
"id":1,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
"_index" : "blog",
"_type" : "article",
"_id" : "AW177VG-fklqH1cpWhsC",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}
获取文档:
# curl node2:9200/blog/article/1?pretty
{
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}
查找一个不存在的ID:
# curl node1:9200/blog/article/100?pretty
{
"_index" : "blog",
"_type" : "article",
"_id" : "100",
"found" : false
}
可以通过HEAD的提交方式快速查看是否有相关的记录:
# curl --head node2:9200/blog/article/100?pretty
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
# curl --head node2:9200/blog/article/1?pretty
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
新建两个文档:
# curl -XPOST node3:9200/blog/article/101?pretty -d '{
"id":101,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
"_index" : "blog",
"_type" : "article",
"_id" : "101",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}
# curl -XPOST node3:9200/blog/article/102?pretty -d '{
"id":102,
"title":"Git 简介",
"posttime":"2017-05-01",
"content":"Git是一款免费、开源的分布式版本控制系统"
}'
{
"_index" : "blog",
"_type" : "article",
"_id" : "102",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}
根据id获取多个文档:
# curl node2:9200/_mget?pretty -d '{
"docs":[
{
"_index":"blog",
"_type":"article",
"_id":"1"
},
{
"_index":"blog",
"_type":"article",
"_id":"101"
},
{
"_index":"blog",
"_type":"article",
"_id":"102"
}
]
}'
{
"docs" : [ {
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}, {
"_index" : "blog",
"_type" : "article",
"_id" : "101",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 101,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}, {
"_index" : "blog",
"_type" : "article",
"_id" : "102",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 102,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
} ]
}
同一个索引下的不同类型,可以简写为:
# curl node3:9200/blog/_mget?pretty -d '{
"docs":[
{
"_type":"article",
"_id":"1"
},
{
"_type":"article",
"_id":"102"
}
]
}'
{
"docs" : [ {
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}, {
"_index" : "blog",
"_type" : "article",
"_id" : "102",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 102,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
} ]
}
如果类型和索引都相同,则可以写为:
# curl node3:9200/blog/article/_mget?pretty -d '{
"docs":[
{"_id":"1"},
{"_id":"102"}
]
}'
{
"docs" : [ {
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}, {
"_index" : "blog",
"_type" : "article",
"_id" : "102",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 102,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
} ]
}
进一步简化:
# curl node3:9200/blog/article/_mget?pretty -d '{
"ids":["101","102"]
}'
{
"docs" : [ {
"_index" : "blog",
"_type" : "article",
"_id" : "101",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 101,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
}, {
"_index" : "blog",
"_type" : "article",
"_id" : "102",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 102,
"title" : "Git 简介",
"posttime" : "2017-05-01",
"content" : "Git是一款免费、开源的分布式版本控制系统"
}
} ]
}
更新文档
文档更新,在ES内部首先找到这个文档,删除旧文档执行更新,更新后再索引最新文档。
索引文档:
# curl -XPUT node3:9200/test?pretty
{
"acknowledged" : true
}
curl -XPOST node2:9200/test/mytype/1?pretty -d '
{
"id":1,
"name":"zhangsan",
"age":25
}'
curl -XPUT node2:9200/test/mytype/1?pretty -d '
{
"id":1,
"name":"lisi",
"age":35
}'
# curl -XPUT node3:9200/test/type1/1?pretty -d '{
"counter":1,
"tags":["red"]
}'
{
"_index" : "test",
"_type" : "type1",
"_id" : "1",
"_version" : 3,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}