文档基本操作

网友投稿 510 2022-05-29

文档的基本操作

新增文档

# 创建索引 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" } } '

Elasticsearch

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:什么是文档归档?——apipost
下一篇:接口测试--接口文档规范
相关文章