文档的基本操作
【摘要】 文档的基本操作 新增文档# 创建索引curl -X PUT "http://localhost:9200/student"# 创建mappingcurl -X PUT "localhost:9200/student/_mapping" -H 'Content-Type: application/json' -d'{ "properties": { "name": { "t...
文档的基本操作
新增文档
# 创建索引
curl -X PUT "http://localhost:9200/student"
# 创建mapping
curl -X PUT "localhost:9200/student/_mapping" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"years":{
"type": "integer"
}
}
}
'
# 指定ID新增文档
curl -X PUT "localhost:9200/student/_doc/1" -H "Content-Type: application/json" -d'
{
"name": "Nick",
"age": 19
}
'
# 不指定ID新增文档
curl -X POST "localhost:9200/student/_doc" -H "Content-Type: application/json" -d'
{
"name": "Nick",
"age": 19
}
'
指定操作类型
# 创建文档,如果该文档已经存在则会UPDATE
curl -X PUT "localhost:9200/student/_doc/1" -H "Content-Type: application/json" -d'
{
"name": "Nick",
"age": 35
}
'
# 指定创建操作,如果该文档已经存在则会报错,该操作可以避免错误
curl -X PUT "localhost:9200/student/_doc/1?op_type=create" -H "Content-Type: application/json" -d'
{
"name": "Nick",
"age": 35
}
'
查看文档
# 通过ID查看文档
curl -X GET "localhost:9200/student/_doc/1"
curl -X POST "localhost:9200/_mget?pretty" -H "Content-Type: application/json" -d '
{
"docs":[
{
"_index": "student",
"_type": "_doc",
"_id": "1"
},
{
"_index": "school",
"_type": "_doc",
"_id": "1"
}
]
}
'
# 指定索引,然后获取多个ID值的文档
curl -X POST "localhost:9200/student/_mget?pretty" -H "Content-Type: application/json" -d '
{
"docs":[
{
"_type": "_doc",
"_id": "1"
},
{
"_type": "_doc",
"_id": "2"
}
]
}
'
# 指定索引,文档,然后获取多个ID值的文档
curl -X POST "localhost:9200/student/_doc/_mget?pretty" -H "Content-Type: application/json" -d '
{
"docs":[
{
"_id": "1"
},
{
"_id": "2"
}
]
}
'
curl -X POST "localhost:9200/student/_doc/_mget?pretty" -H "Content-Type: application/json" -d '
{
"ids": [1,2]
}
'
修改文档
# 指定id修改
curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d '
{
"doc": {
"name": "Elaine"
}
}
'
curl -X GET "localhost:9200/student/_doc/1?pretty"
# 新增字段,ctx上下文
curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d '
{
"script": "ctx._source.age1 = 19"
}
'
# 删除字段,ctx上下文
curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d '
{
"script": "ctx._source.remove(\"age1\")"
}
'
# 更新, upsert当文档不存在时,upsert内的内容将会插入到索引中,作为一个新文档
curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d '
{
"script": {
"source": "ctx._source.age += params.age",
"params": {
"age": 4
}
},
"upsert":{
"age": 1
}
}
'
删除
# 删除指定文档
curl -X DELETE "localhost:9200/student/_doc/1" -H "Content-Type: application/json"
自动创建索引
- 当索引不存在,并且auto_create_index为true的时,新增文档会自动创建索引
# 查看方法
curl http://localhost:9200/_cluster/settings
# 配置方法
curl -X PUT "localhost:9200/_cluster/settings" -H "Content-Type: application/json" -d'
{
"persistent": {
"action.auto_create_index": "true"
}
}
'
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)