社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
curl -XGET "192.168.149.129:9200/_cat/health?v&pretty"
curl -XGET "192.168.149.129:9200/_cat/indices?v&pretty"
curl -H "Content-Type: application/json" -XPOST '192.168.149.129:9200/bank/account/_bulk?pretty&refresh' --data-binary "@account.json"
account.json文本如下:
说明:bank为索引名,account为类型,_bulk表示批量导入,refresh为立即生效,@account.json为安装目录下的json数据文件(即将该数据文件添加到bank索引中)
注:-H这部分不加的话,可能会报错,加上之后报错消失
注意:accounts.json每行必须以n换行。如果提示The bulk request must be terminated by a newline [n],请检查最后一行是否以n换行。
下图表示数据添加成功:
查询索引,发现bank已添加:
1)REST请求URI发送搜索参数
curl -XGET '192.168.149.129:9200/bank/_search?q=*&sort=account_number:asc&pretty'
注:bank为索引名,q=*表示查询所有的,sort=account_number表示按照account_number进行排序,asc表示升序
2)REST请求主体发送搜索参数
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{
"query":{"match_all":{}}, #匹配索引下面所有的文档
"sort":[{"account_number":"asc"}] #查询结果按照account_number字段进行升序排列
}'
注:查询语句可以不用换行,换行是为了显示清晰,也可以直接写成:
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{ "match_all":{} },
"sort":[{"account_number":"asc"}] }'
查询示例:
a. 指定显示结果数:"size":n
#显示20条
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}},
"sort":[{"account_number":"asc"}],
"size":20 }'
b. 指定显示范围:"from":n #从第n个开始
#从第10个开始,显示20条(10~29)
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}},
"sort":[{"account_number":"asc"}],
"size":20,
"from":10 }'
c. 排序:按照balance降序排
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}},
"sort":[{"balance":"desc"}]}'
或者:
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}},
"sort":{"balance":{"order":"desc"}} }'
d. 指定显示的字段:"_source":["field1","field2"]
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}},
"sort":[{"balance":"desc"}],
"_source":["balance","firstname","account_number"] }'
e. match和match_phrase
#匹配account_number为20的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"match":{"account_number":20}}
}'
#匹配地址中包含mill(不区分大小写)的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"match":{"address":"mill"}},
"_source":["address"] }'
#匹配地址中包含mill或者lane(不区分大小写)的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{ "match":{"address":"mill lane"} },
"_source":["address"] }'
#匹配地址中包含"mill lane" 的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{ "match_phrase":{"address":"mill lane"} },
"_source":["address"] }'
f. bool查询 -must #与的关系,同时匹配
#匹配地址中包含"mill lane" 的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"bool":
{"must":
[ {"match":{"address":"mill"}},
{"match":{"address":"lane"}}
]
}
},
"_source":["address"] }'
g. bool查询 -should #或的关系,匹配任意一个
#匹配地址包含mill或者lane的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"bool":
{"should":
[ {"match":{"address":"mill"}},
{"match":{"address":"lane"}}
]
}
},
"_source":["address"] }'
h. bool查询 - must_not #既不包含A也不包含B
#匹配地址既不包含mill,也不包含lane
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"bool":
{"must_not":
[ {"match":{"address":"mill"}},
{"match":{"address":"lane"}}
]
}
},
"_source":["address"] }'
i. bool查询- 组合使用
#匹配地址包含mill,但是年龄不是38的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":
{"bool":
{"must":[{"match":{"address":"mill"}}],
"must_not":[{"match":{"age":38}}]
}
},
"_source":["address","age"] }'
j. 过滤器和聚合
#匹配地址包含mill,且年龄在20~30之间的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' {"query":
{"bool":
{ "must":{"match":{"address":"mill"}},
"filter":{"range":{"age":{"gte":20,"lte":30}}}
}
},
"_source":["address","age"] }'
k. 聚合演示 - 按照state分组,count递减排序
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "size":0,
"aggs":
{"group_by_state":
{"terms":{"field":"state.keyword"}}
}
}' #默认递减
l. 聚合演示 - 按state计算平均账户余额
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{
"size":0,
"aggs":{
"group_by_state":
{ "terms": {"field":"state.keyword"},
"aggs":{"average_balance":{"avg":{"field":"balance"}}}
}
}
}'
curl -XDELETE 127.0.0.1:9200/index_name1,index_name2,index_name3…
删除多个索引,索引之间用逗号
也支持通配符,如:
curl -XDELETE 127.0.0.1:9200/index_*
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!