shell窗口运行curl命令执行DSL查询语句

本文介绍了Elasticsearch的基本操作,包括检查群集健康状态、管理索引、文档的增删改查及批量操作等。此外还展示了如何通过查询获取数据、更新文档以及使用脚本进行复杂的数据修改。

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

#检查群集运行状况
curl -X GET "localhost:9200/_cat/health?v"

#获得群集中的节点列表
curl -X GET "localhost:9200/_cat/nodes?v"

#列出所有索引
curl -X GET "localhost:9200/_cat/indices?v"

#创建索引并查看
curl -X PUT "localhost:9200/customer?pretty"
curl -X GET "localhost:9200/_cat/indices?v"

#指定ID插入数据并查看(如果没有该索引则默认自动创建)
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
#删除索引并查看
curl -X DELETE "localhost:9200/customer?pretty"
curl -X GET "localhost:9200/_cat/indices?v"

#不指定ID插入数据会自动分配ID
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'

#替换文档版本号会递增,若该ID尚未使用则相当于插入数据
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X PUT "localhost:9200/customer/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'

#通过将名称字段更改为“Jane Doe”来更新以前的文档(ID为1)
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe" }
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
#通过将名称字段更改为“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 }
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
#使用脚本将年龄增加5
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"

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

#批处理
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X GET "localhost:9200/customer/_doc/2?pretty"
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X GET "localhost:9200/customer/_doc/2?pretty"
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"}}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty"
curl -X GET "localhost:9200/customer/_doc/2?pretty"

#q=*匹配索引中的全部文档
curl -X GET "localhost:9200/customer/_search?q=*&pretty"
#JSON查询体匹配索引中的全部文档
curl -X GET "localhost:9200/customer/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} }
}
'
#size指定查询文档大小,若未指定,则默认为10
curl -X GET "localhost:9200/customer/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "size": 1
}
'
#from参数(基于0)指定从哪个文档索引开始,size参数指定从from参数开始返回多少文档
#这个特性在实现搜索结果分页时非常有用!注意,如果未指定from,则默认为0
curl -X GET "localhost:9200/customer/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}
'
#此示例执行match_all并按帐户余额降序对结果进行排序,并返回前10个(默认大小)文档
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}
'

以上都是基本操作,更复杂的操作有待更新...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值