常用mysql语句转elasticsearch查询语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zfy1355/article/details/82968118
  1. 多个查询条件 bool(must、should、must_not)
select * from index_person where name = 'jeck' or sex = 'male' order by age,sex
{
	"query":{
		"bool":{
			"should":{
				{"match":{"name": "jeck"}},
				{"match": {"sex": "male"}},
			}
		}		
	},
    "sort":[{"age":{"order":"asc"},
			{"sex":{"order":"desc"}}]
}
  1. like查询 wildcard
select * from index_person where name like '%jeck%'

{
	"query":{
		"bool":{
			"must":{
				"wildcard":{
					"name":"*jeck*"
				}
			}
		}		
	}
}
  1. 区间查询 range
select * from index_person where name = 'jeck' and sex = 'male' and (age<30 and age>40)

{
 "query": {
    "bool": {
		"must": [
			{"match":{"name": "jeck"}},
			{"match": {"sex": "male"}},
			{"range" : {"age" : {"gt" : 40,"lt":30}}}
		]
    }
  }
}
  1. 根据bucket提取汇总数,如果对汇总数继续分组,可以在group_by_sex后继续添加aggs结构
select count(sex) from index_person where age>30 group by sex;

{
  "size":10,
  "query":{
    "bool":{
      "must":[{"range" : {"age" : {"gt" : 30}}}]
    }
  },
  "aggs":{
    "group_by_sex":{
      "terms":{
        "field":"sex"
      }
    }
  }
}
  1. 分组调用内置函数,(avg,sum)
select average(age) from index_person  group by sex 
{
  "size":10,
  "aggs":{
    "avg_by_age":{
      "avg":{
        "field":"sex"
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/zfy1355/article/details/82968118