一、查询
1.简单查询
// post、get方式都行
{
"query":{
"match_all":{
}}
}
// 对应的SQL语句
select * from student
可以将student表中的所有数据查询出来。
2.精确查询
// post方式
{
"query":{
"term":{
"name.keyword":"张三"}
}
}
// 对应的SQL语句
select * from student where name='张三'
可以将student表中名为张三的数据查询出来。
其中term是用于精确匹配的,类似于sql语句中的“=”,因为“name”字段用的是standard默认分词器,其会将“张三”分成“张”和“三”,并不会匹配姓名为“张三”的人,而name.keyword可以让其不会进行分词。也可以是terms,这个可以用多个值去匹配一个字段,例如:
// post方式
{
"query": {
"terms": {
"name.keyword": ["张三", "李四"]
}
}
}
// 对应的SQL语句
select * from student where name in ('张三','李四')
3.模糊查询
// post方式
{
"query": {
"match": {
"name": "张"
}
}
}
// 对应的SQL语句
select * from student where name like '%张%'
可以将student表中名字带有“张”的查询出来。
4.分页查询
// post方式
{
"from":0,
"size":10
}
// 对应的SQL语句
select * from student limit 0,10
5.范围查询
// post方式
{
"query":{
"range":{
"age":{
"gte":20,
"lte":30
}
}
}
// 对应的SQL语句
select * from student where age>=20 and age<=30
gt: 大于
gte: 大于或等于
lt: 小于
lte: 小于或等于
6.多字段匹配查询
// post方式
{
"query":{
"multi_match":{
"query":"男",
"fields":["name","sex"]
}
}
// 对应的SQL语句
select * from stud

本文详细介绍了Elasticsearch的查询和聚合操作,包括简单查询、精确查询、模糊查询、分页、范围查询、多字段匹配、过滤、通配符、前缀和空值查询。在聚合部分,讲解了指标聚合如最大值、最小值、平均值的计算,以及桶聚合如terms分组统计、范围聚合和空值统计的应用。
最低0.47元/天 解锁文章
2568

被折叠的 条评论
为什么被折叠?



