# Es查询表达式(Query DSL)

Es查询表达式(Query DSL)


精确值查找

  1. terms查询(包含查询,类似与sql的in)
{
  "query":{
    "terms":{
      "id":["1001","1002"]
    } 
  }
}
  1. term查询
select * from test where name ='test'
{
    "query":{
        "term":{
            "naem":{
                "value":"test"
            }
        }
    }
}

bool过滤器

  1. bool过滤器组成部分
{
    "bool":{
    	//与And等价
        "must":[]
        //与Or等价
        "should":[]
        //与not等价
       	"must_not":[]
       	//必须匹配
       	"filter":[]
    }
}

范围检索

  1. range query
gt:>大于(greather than)
lt:<小于(less than)
gte:>大于或者等于(greather than or equal to)
lte:<小于或者等于(less than or equal to)

相当与sql

select * from test where 1=1 and age>=10 and age<=20 
{
    "query":{
        "bool":{
            "must":{
                "range":{
                    "age":{
                        "gte":10,
                        "lte":20
                    }
                }
            }
        }
    }
}
  1. 匹配查询
select * from test where 1=1 and age>=10 and age<=20  and name like'%test%'
select * from test where 1=1 and age>=10 and age<=20  and name in'test1','test2')
{
  "query":{
    "bool":{
        "must":{
        "range":{
          "es_time":{
            "gte": "2019-07-03 00:00:00",
            "lte": "2019-07-03 23:59:59"
          }
        }
      },
      "filter":{
        "terms":{
          //匹配查询
          "name":["test"]
          //精确查询
          "name.keyword":["test1","test2"]
        }
      }
    }
  }
}

前缀检索

  1. prefix query匹配前缀:类似与sql的like ‘%test’
{
    "query":{
        "prefix":{
            "name":{
                "value":"玲"
            }
        }
    }
}
  1. 通配符匹配
{
    "query":{
        "wildcard":{
            "name":{
                "value":"玲*"
            }
        }
    }
}

模糊检索

{
    "query":{
        "fuzzy":{
            "name":{
                "value":"阿"
            }
        }
    }
}

聚合功能

  1. 类似于sql中的group by可以进行聚合统计
{
    "aggs":{
        "all_name":{
            "terms":{"filed":"name"}
        }
    }
}
  1. 加上where 条件:统计姓名中含有勇,并且根据姓名Group by
{
    "query":{
        "match":{
            "name":"勇"
        }
    },
    "aggs":{
        "all_names":{
            "terms":{"files":"name"}
        }
    }
}
发布了123 篇原创文章 · 获赞 9 · 访问量 3981

猜你喜欢

转载自blog.csdn.net/qq_37248504/article/details/103774307
今日推荐