请求体查询
# 空查询
GET /_search
{}
GET /index_2020*/type1,type2/_search
{}
# 分页查询
GET /_search
{
"from":5,
"size":5
}
# 由于GET带请求体不被广泛认同,因此ES支持POST
POST /_search
{
"from":5,
"size":5
}
# 查询表达式
# 空查询,匹配所有文档
GET /_search
{
"query": {
"match_all": {}
}
}
# 针对某一字段查询
GET /_search
{
"query": {
"match":{
"tweet":"elasticsearch"
}
}
}
# 合并查询语句
GET /_search
{
"query": {
"bool": {
"must": [{"match": {"tweet": "elasticsearch"}}],
"must_not": [{"match": {"name": "mary"}}],
"should": [{"match": {"tweet": "full text"}}],
"filter": {"range": {"age": {"gt": 30}}}
}
}
}
GET /_search
{
"query": {
"bool":{
"must": [{"match": {"email": "business opportunity"}}],
"should": [
{"match": {"FIELD": "TEXT"}},
{"bool":
{
"must":[{"match":{"FIELD":"TEXT"}}],
"should": [{"match": {"FIELD": "TEXT"}}]
}
}
],
"minimum_should_match": 1
}
}
}
# 最重要的几个查询
# 1.match_all, 得到评分值全为1的_score
# 2.match,可进行全文搜索和精确值查询
# 3.multi_match,在多个字段上进行相同的match查询
# 4.range,用于时间和数字区间查询,操作符:gt,gte,lt,lte
# 5.term,用于单个值对某个字段精确匹配
# 6.terms,用于多个值对某个字段精确匹配
# 7.exists和missing,用于查找文档中某个字段有值(IS NOT NULL)或无值(IS NULL)的文档
# 组合多查询
# 1.must, 必须匹配条件
# 2.must_not, 必须不匹配条件
# 3.should, 如果满足条件,则加分,否则无影响
# 4.filter, 只用于筛选文档,对评分不影响
# 只有一个filter,没有其他查询,可以使用constant_score使评分变成常量
# 验证查询
# _validate/query 验证查询语句
# explain参数,用于查看错误信息
# 以下为不合法查询语句
GET /gb/tweet/_validate/query?explain
{
"query": {
"tweet" : {
"match" : "query text"
}
}
}
# 以下为正确查询语句,每个index都会显示出自己对查询的分析结果
GET /_validate/query?explain
{
"query": {
"match" : {
"tweet" : "really powerful"
}
}
}
扫描二维码关注公众号,回复:
12449344 查看本文章
