ES基础之DSL

1、基础查询

基础查询也就是一些单体的查询,包括多字段查询,范围查询,精确查询,地理查询等

,下面的图片是我使用数据库所对应的字段名,可以结合查询语句来看。

#查询所有
GET /hotel/_search
{
  "query": {
    "match_all": {}
  }
}
#查询类型为keyword类型的所有字段
GET /hotel/_search
{
  "query": {
    "match": {
      "all": "上海"
    }
  }
}
#多字段查询
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "如家",
      "fields": ["name","brand"]
    }
  }
}
# 精确查询
GET /hotel/_search
{
  "query": {
    "term": {
      "city": "上海"
    }
  }
}

#范围查询
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 300
      }
    }
  }
}

#地理矩形范围查询
GET /hotel/_search
{
  "query": {
    "geo_bounding_box":{
      "location":{
        "top_left":{
          "lat":31.1,
          "lon":121.5
        },
        "bottom_right":{
          "lat":30.9,
          "lon":121.7
        }
      }
    }
  }
}
#查询到指定中心点小于某个距离值的所有文档
GET /hotel/_search
{
  "query": {
    "geo_distance":{
      "distance":"2km",
      "location":"31.21,121.5"
    }
  }
}
2、复合查询

复合查询也就是一个查询里面有多个条件的查询,比如排序、分页等

#复合查询
#function score 打分
GET /hotel/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "all": "外滩"
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "brand": "如家"
            }
          },
          "weight": 6 
        }
      ],
      "boost_mode": "sum"
    }
  }
}

#Boolean Query 布尔查询是一个或多个查询子句的组合
GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {"city": {"value": "上海"}}}
      ],
      "should": [
        {"term": {"brand": "如家" }},
        {"term":{"brand":"华美达"}}
          
      ],
      "must_not": [
        {"range": {"price": {"lte":100}
    }}
      ],
      "filter": [
        {"range": {"score": {"gte": 45}
        }}
    ]
    }
  }
}

#排序
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

#分页
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2,
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}
#搜索关键字高亮处理
GET /hotel/_search
{
  "query": {
    "term": {
      "brand": "如家"
    }
  },
  "from": 0,
  "size": 2,
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": "<em>",
        "post_tags": "</em>"
      }
    }
  }
}
3、聚合查询

1、bucket聚合

#聚合
GET /hotel_search
{
  "size": 0, //设置siaze为0,结果中不包含文档,只包含结果
  "aggs": {  //定义聚合
    "brandAgg": { //给聚合起名
      "terms": {  //聚合类型
        "field": "brand",  //参与聚合的字段
        "size": 10 //希望获取的结果数量
      }
    }
  }
}

还可以限定聚合范围比如

GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "lte": 200
      }
    }
  }, 
  "size": 0, 
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 20
      }
    }
  }
}

聚合三要素:

1、聚合名称  2、聚合类型  3、聚合字段

2、metrics聚合

GET /hotel/_search
{
  "size": 0, 
  "aggs": {
    "brandAgg": { 
      "terms": { 
        "field": "brand", 
        "size": 20
      },
      "aggs": { // 是brands聚合的子聚合,也就是分组后对每组分别计算
        "score_stats": { // 聚合名称
          "stats": { // 聚合类型,这里stats可以计算min、max、avg等
            "field": "score" // 聚合字段,这里是score
          }
        }
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值