使用curl操作es
curl与es交互的语法为:
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
# verb http方法有五种: GET, POST, PUT, HEAD, DELETE
# protocol是协议, 默认是http, 因此可以不写
# host主机地址
# port端口号
# QUERY_STRING查询设置
# body是json格式的请求细节
# 一般来说, 也需要使用"-H"指定header, 也就是要指定传递到服务器的自定义头
# 示例如下
curl -H 'Content-Type: application/json' -XPUT "node243:9200/megacorp/employee/1?pretty" -d'{
}'
异常
"Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n
# 常见异常, 一般是因为json中的字段, 忘了加逗号
增
curl -X PUT "node243:9200/megacorp/employee/5?pretty" -H 'Content-Type: application/json' -d'{
"first_name": "Carney",
"last_name": "Willson",
"age": 24,
"about": "I love java",
"try": "243",
"interests": [ "game", "music" ]
}'
格式解析
1. "?pretty"参数用于使查询结果显示为易读的json
2. megacorp是index, mysql中的database数据库
3. employee是type, 对应表
4. 1是id, 对应行
5. <BODY>中是json键值对, 对应每一列的相应字段
删
curl -X DELETE "node243:9200/megacorp/employee/5?pretty"
改
覆盖更新
和增的语法一样, 不过_version
属性会相应增加
curl -X PUT "node243:9200/megacorp/employee/5?pretty" -H 'Content-Type: application/json' -d'{
"first_name": "Carney",
"last_name": "Willson",
"age": 14,
"about": "I love java",
"try": "243",
"interests": [ "game", "music" ]
}'
通过API更新指定字段
# 外层的doc不可省略
curl -H 'Content-Type: application/json' -XPOST "node243:9200/megacorp/employee/3/_update?pretty" -d'{
"doc": {
"age": 10
}
}'
查
简单查询
# 默认显示前10条数据(size=10)
# 最简模式
curl -X GET "node243:9200/megacorp/employee/3"
# 增强可读性
curl -X GET "node243:9200/megacorp/employee/_search?pretty"
# 指定查询条件
curl -X GET "node243:9200/megacorp/employee/_search?q=last_name:Smith"
# 很多nosql都支持json查询, 但是规则各异
curl -H 'Content-Type: application/json' -X GET "node243:9200/megacorp/employee/_search" -d'{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}'
# 此外, 还支持如下字段
{
"query": {
# 显示多少行, 默认是10
"size": 1,
# 从哪个角标开始搜索, 默认是0
"from": 10,
# 查询所有字段
"match_all": {},
"sort": {
"name": {
"order": "desc"
}
},
# 指定要显示的字段
"_source": ["name", "age"]
}
}
多条件组合查询
{
"query": {
"match": {
# 名字中有carney 或 willson
"name": "carney willson"
}
}
}
{
"query": {
"match_phrase": {
# 名字中有短语: carney willson
"name": "carney willson"
}
}
}
{
"query": {
"bool": {
# must是且的逻辑, should是或的逻辑, must_not是否的逻辑
"must": [
{
"match": {
"name": "carney"
}
},
{
"match": {
"name": "willson"
}
}
],
# 逗号分隔的这两个关系之间, 是且的逻辑
"must_not": [
{
"match": {
"age": 18
}
}
]
}
}
}
# bool的值中, 还可以使用filter过滤
# range也是常用的一个键(方法)
{
"query": {
"bool": {
"mast": {
"match_all": {}
},
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 28
}
}
}
}
}
}
聚合
# 简单分组
curl -H 'Content-Type: application/json' -XGET "node243:9200/megacorp/employee/_search?pretty" -d'{
# 不显示非聚合的信息
"size": 0,
"aggs": {
# 指定聚合后的字段名, 可以任意指定, 但要符合逻辑
"group_by_last_name": {
"terms": {
# XX.keyword是固定语法, 显示count
"field": "last_name.keyword"
}
}
}
}'
# 排序和过滤
curl -H 'Content-Type: application/json' -XGET "node243:9200/megacorp/employee/_search?pretty" -d'{
"size": 0,
"query": {
"bool": {
"filter": {
"range": {
"age": {
# 大于等于, 和shell的语法一样
"gte": 0,
"lte": 25
}
}
}
}
},
"aggs": {
"group_by_last_name": {
"terms": {
"field": "last_name.keyword",
"order": { "max_age": "desc" }
},
# 注意aggs是和terms对齐的, 聚合查询都要严格嵌套
"aggs": {
# "max_age"的命名可变
"max_age": {
"max": {
"field": "age"
}
}
}
}
}
}'
# 范围分组和其它聚合函数
curl -H 'Content-Type: application/json' -XGET "node243:9200/megacorp/employee/_search?pretty" -d'{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
# [from, to), 包前不包后
"from": 0,
"to": 25
},
{
"from": 25,
"to": 100
}
]
},
"aggs": {
"min_age": {
"min": {
"field": "age"
}
},
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}'