elasticsearch常用DSL查询、统计语句大全(珍藏版)

本文详细介绍了Elasticsearch的DSL查询语句,包括通过ID查询、时间范围查询、数字范围查询、多条件与关系查询、分页、模糊查询、短语查询、bool联合查询、排序、范围查询等。同时,还涵盖了统计方法,如分组、求和、求最小值、分组统计、不同时间段和数值范围的统计,以及按时间单位统计标签数量等实用技巧。
摘要由CSDN通过智能技术生成

一 常用DSL语句大全 

1.1  通过id查询

1.通过id且设置显示的字段查询
{ "_source" : {
    "includes" : [
      "id",
      "title"
    ],
    "excludes" : [ "content"]
  },
  "query": {
    "term": {
      "id": "282f85c59020c903e8adff91802815f0"
    }
  }
}

 1.2  时间范围查询

2.通过时间范围查询://注意日期加引号
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createtime": {
         "format": "yyyy-MM-dd HH:mm:ss",
              "from": "2017-12-14 16:25:26",
              "to": "2017-12-14 17:25:26",
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}
或者
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createtime": {
              "gt": "2017-12-14 16:25:26",
              "lte": "2017-12-14 17:25:26",
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}

1.3 数字范围查询

3.通过数字区间范围查询
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "similarValue": {
              "from": 0.1,
              "to": 1,
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  }
}

1.4 多条件与关系查询

4.通过多条件查询,并且是“与”关系
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "similarValue": {
              "from": 0.1,
              "to": 1,
              "include_lower": true,
              "include_upper": true
            }
          }
        },
        {
          "term": {
            "dataSource": "财新网"
          }
        }
      ]
    }
  }
}

1.5 带分页查询

5.带分页的模糊查询
title字段:分词
{
  "from": 10,
  "size": 20,
  "query": {
    "match": {
      "title": "中国杭州"
    }
  }
}
contentType:不分词,必须全词条匹配
{
  "query": {
    "match": {
      "contentType": "企业新闻" 
    }
  }
}

1.6 带操作符的模糊查询

6.带操作符的模糊查询,该参数的默认值是"or",变为"and"来要求所有的词条都需要被匹配,提高精确度。
{
  "query": {
    &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值