elasticsearch - 6.3.2 API

本文演示了如何使用curl命令通过RESTfulAPI对Elasticsearch进行索引创建、文档添加、更新、删除及批量处理等基本操作,包括指定ID和自动创建索引、文档检索以及使用脚本更新文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

索引操作

//创建一个索引,pretty表示打印添加状态

curl -X PUT "localhost:9200/customer?pretty"

//获取索引列表
curl -X GET "localhost:9200/_cat/indices?v"

//向索引中添加一个文档,指定ID为1,如果索引不存在es会自动创建
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}'

//向索引中添加一个文档,没有指定ID,es将自动创建
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}'

//检索编入索引的文档
curl -X GET "localhost:9200/customer/_doc/1?pretty"

//删除索引
curl -X DELETE "localhost:9200/customer?pretty"


更新文档

//更新ID为1的文档
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe" }
}'

//更新ID为1的文档,并且增加属性
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'

//使用脚本更新文档,ctx._source代表当前要更新的文档(此处表示年龄在原来的基础上再加5)
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}'


删除文档

//删除文档指定ID为2
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"

批量处理:

//批量插入两条数据,ID分别为3和4
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"3"}}
{"name": "John Doe" }
{"index":{"_id":"4"}}
{"name": "Jane Doe" }
'

//此示例更新第一个文档(ID为1),然后在一个批量操作中删除第二个文档(ID为2):
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值