elasticsearch - 6.3.2 API
创始人
2024-06-03 14:40:42

索引操作

//创建一个索引,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"}}
'
 

相关内容

热门资讯

尚纬股份跌0.45%,成交额2... 1月12日,尚纬股份跌0.45%,成交额2.68亿元,换手率4.88%,总市值55.19亿元。异动分...
光洋股份涨2.01%,成交额3... 1月12日,光洋股份涨2.01%,成交额3.52亿元,换手率5.04%,总市值77.18亿元。异动分...
恒华科技涨2.97%,成交额1... 1月12日,恒华科技涨2.97%,成交额1.92亿元,换手率5.47%,总市值41.57亿元。异动分...
鲍斯股份涨0.38%,成交额1... 1月12日,鲍斯股份涨0.38%,成交额1.30亿元,换手率2.66%,总市值50.52亿元。异动分...
江化微涨停,成交额10.02亿... 1月12日,江化微涨停,成交额10.02亿元,换手率12.40%,总市值82.60亿元。异动分析光刻...