如何使用ES做简单的时间条件过滤+模糊查询+精确匹配+关键字排除,查询 elasticsearch查询结果包含或排除某些字段、_source查询出需要的属性名称

目录

一、时间条件过滤+模糊查询+精确匹配+关键字排除

1. 查询出包含 log_geo 的数据 “wildcard”: { “message”: “log_geo” }

2. 查询某个时间段的数据

3. 条件查询与条件排除数据

4. from 表示起始的记录的ID

5. size 表示显示的记录数

6.sort排序 desc降序、asc升序 

7.should查询在mysql中就好比是or或

8. aggs:执行聚合

9.bool query布尔查询

二、_source查询结果包含或排除某些字段


一、时间条件过滤+模糊查询+精确匹配+关键字排除

1. 查询出包含 log_geo 的数据 “wildcard”: { “message”: “log_geo” }

log_geo 前面有*表示搜索以log_geo结尾的数据

log_geo后面有* 表示搜以log_geo开始的数据

log_geo前后都有*就是通用匹配包含log_geo的记录

“wildcard”:
{ 
   “message”: “log_geo” 
}

2. 查询某个时间段的数据

“range”: { 
    “@timestamp”: { 
        “gt”: “2018-02-08T07:00:00.056000000+00:00”,
        ”lt”: “2018-02-08T08:00:00.056000000+00:00” 
     } 
}

#注意 时区减去8小时

3. 条件查询与条件排除数据

3.1 match 包含provider 的数据

{ 
“match”: 
    {
     “message”: “type:provider” 
    }
}

3.2 must_not 类似于 must 做排除使用

“must_not”: { 
    “match”: { 
        “message”: “dateTime:2018-02-08 15:59”
     }
}

4. from 表示起始的记录的ID

5. size 表示显示的记录数

6.sort排序 desc降序、asc升序 

7.should查询在mysql中就好比是or或

但在es中使用should查询必须要和must一起使用才可以,相当于must[should A,should B],中文解释就好比是满足A或者是B条件的数据

8. aggs:执行聚合

9.bool query布尔查询

布尔查询是一个复合查询,由各个bool类型的子查询组成。

must :返回文档必须满足该条件,且提供分数
filter :返回文档必须满足该条件,但是不需要提供分数
should :返回文档可以满足0个或多个条件,且提供分数
must_not : 返回文档必须不满足条件,但是不需要提供分数


        查询语句 1,2,3可以组合,也可以单独使用,查询语句如果得不到想要的结果应该是ES的分词导致的,组合起来的语句如下:

{
  "query": {
    "bool": {
      "must": [
        { "range": { "@timestamp": { "gt": "2018-02-08T07:00:00.056000000+00:00","lt": "2018-02-08T08:00:00.056000000+00:00" } }  }
        ,   
        {  "wildcard": { "message": "*cp_geo*" }  }
        ,
        { "match": { "message": "*type:platform*" } }
      ],
      "must_not": { "match": { "message": "*deviceTypeCode:DTout00000000*" } },
      "should": []
    }
  },
  "from": 0,
  "size": 50,
  "sort": {  "@timestamp": "desc"  },
  "aggs": {}
}

参考原文地址:https://blog.51cto.com/u_8649447/4869008

二、_source查询结果包含或排除某些字段

        通过_source 字段中的 include 和 exclude 来指定返回结果包含哪些字段,排除哪些字段
举例:根据关系单号,查询 es,设置_source 的 include 和 exclude

实例查询语句:

{
  "_source":{
    "include":[
      "policyNo",
      "policyRelationNo",
      "policyStatus"
    ],
    "exclude":[
       "salesType"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "policyRelationNo": "KR01435021"
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  },
  "from": 0,
  "size": 10
}

查询效果:

{
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 19,
        "max_score": 11.391884,
        "hits": [
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407038",
                "_score": 11.391884,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120319",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407046",
                "_score": 10.713255,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120323",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407044",
                "_score": 10.713255,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120322",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407066",
                "_score": 10.713255,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120333",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407058",
                "_score": 10.713255,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120329",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407070",
                "_score": 10.713255,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120335",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407056",
                "_score": 10.294733,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120328",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407052",
                "_score": 10.294733,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120326",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407062",
                "_score": 10.294733,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120331",
                    "policyStatus": 11
                }
            },
            {
                "_index": "search4policy-msad-dev3_20200520000000",
                "_type": "policy-msad-dev3",
                "_id": "4407064",
                "_score": 10.294733,
                "_source": {
                    "policyRelationNo": "KR01435021",
                    "policyNo": "B609120332",
                    "policyStatus": 11
                }
            }
        ]
    },
    "took": 5,
    "timed_out": false
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等到鸡吃完米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值