#检查群集运行状况
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" } }
}
'
以上都是基本操作,更复杂的操作有待更新...