Elasticsearch(四)---文档管理
【摘要】 新建文档:# 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...
新建文档:
# 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
}
推荐
华为开发者空间发布
让每位开发者拥有一台云主机
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)