一、查询
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