前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实战 | 一步步排查基于业务场景的Elasticsearch难题!

实战 | 一步步排查基于业务场景的Elasticsearch难题!

作者头像
铭毅天下
发布2018-07-26 15:16:17
8020
发布2018-07-26 15:16:17
举报
文章被收录于专栏:铭毅天下铭毅天下

1、题记

我们在实际的业务场景中做Elasticsearch开发时,免不了遇到这样、那样的问题。 《死磕Elasticsearch方法论》中,已经告诉大家相关问题的排查方法。 这一节,我们以具体的示例,解决基于业务场景的Elasticsearch难题的方法?

2、上问题

请问下es可以在date字段上查询月日吗 下单时间字段类型是date,想查询每年11月11日下单的记录?有什么办法可以zh直接查? 原文链接:https://elasticsearch.cn/question/3649

3、搜索已有知识储备

步骤1:脑海中显现:ES中有哪些数据类型?ES支持哪些数据类型。 此处,我个人认为,没必要记住。 但是,要知道,从哪里查? 记住,查询方法如下: 在Google或者Bing国际版输入:“elasticsearch data type” 相关链接:http://t.cn/REjyfxa

步骤2:ES中date字段时如何定义的? 在核心的数据类型部分,就介绍了ES中的Date类型: 数据类型分为三种: 1)字符串类型如: "2015-01-01" or "2015/01/01 12:10:30". 2)长整型,以毫秒度量:1520256456000 3)整型,以秒度量:1520256456。

代码语言:javascript
复制
 1PUT my_index
 2{
 3  "mappings": {
 4    "_doc": {
 5      "properties": {
 6        "date": {
 7          "type":   "date",
 8          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
 9        }
10      }
11    }
12  }
13}

步骤3:常见日期类型的检索方式: range时间区间检索:

代码语言:javascript
复制
 1GET _search
 2{
 3    "query": {
 4        "range" : {
 5            "timestamp" : {
 6                "gte": "2015-01-01 00:00:00", 
 7                "lte": "now", 
 8                "time_zone": "+01:00"
 9            }
10        }
11    }
12}

参考: http://t.cn/RcWM2Py

步骤4:如何获取日期中指定的年、月、日检索? 能想到的是什么呢——的确,我也没这么用过。 注意:此时一定要用英文去检索。 在Google或者Bing国际版或者Stackoverflow中输入:“elasticsearch date get month“。

看看网友的思路: 返回如下: http://t.cn/REjVkGW http://t.cn/REjfh8z

初步,确定解决方案。 结合:script检索+ getDate().getMonthOfYear()接口实现条件检索。

4、敲黑板——知识点

1、script检索 一种基于 Painless脚本语言的检索, Painless 脚本语言是简单、安全应用于Elasticsearch中的脚步语言。 应用举例:

代码语言:javascript
复制
 1GET /_search
 2{
 3    "query": {
 4        "bool" : {
 5            "must" : {
 6                "script" : {
 7                    "script" : {
 8                        "source": "doc['num1'].value > 1",
 9                        "lang": "painless"
10                     }
11                }
12            }
13        }
14    }
15}

2、时间的返回接口 getDate().getMonthOfYear()

注意: date.getMonthOfYear() == 11 返回11月; date.getDayOfMonth() == 11 返回11日。

5、如何实现呢?

基于之前的分析,我的思路: 第一步:获取月; 第二步:获取日; 第三步:二者与。

具体实现:

代码语言:javascript
复制
 1POST index_*/_search
 2{
 3  "size":100,
 4  "_source": {
 5  "includes": [
 6  "title",
 7  "create_time"
 8  ]
 9  },
10  "query": {
11  "bool" : {
12  "must" : [{
13  "script" : {
14  "script" : {
15  "inline": "doc['create_time'].getDate().getMonthOfYear() == 12",
16  "lang": "painless"
17  }
18  }
19  },
20  {
21  "script" : {
22  "script" : {
23  "inline": "doc['create_time'].getDate().getDayOfMonth() == 23",
24  "lang": "painless"
25  }
26  }
27  }
28  ]
29  }
30  }
31}

返回结果: 实际业务中,我以12月,23日作为检索条件验证的,我用的系统中没有11月11日的数据。

代码语言:javascript
复制
 1{
 2  "took": 5,
 3  "timed_out": false,
 4  "_shards": {
 5  "total": 5,
 6  "successful": 5,
 7  "failed": 0
 8  },
 9  "hits": {
10  "total": 8536,
11  "max_score": 1,
12  "hits": [
13  {
14  "_index": "bak_index_v3",
15  "_type": "bke_type",
16  "_id": "AWCEDIuUYGOEa9MAzRvk",
17  "_score": 1,
18  "_source": {
19  "create_time": "2017-12-23 23:45:02",
20  "title": "世界道学网-道学文摘-唱道真言"
21  }
22  },
23  {
24  "_index": "bak_index_v3",
25  "_type": "bke_type",
26  "_id": "AWCD2bs4YGOEa9MAzRta",
27  "_score": 1,
28  "_source": {
29  "create_time": "2017-12-23 22:49:32",
30  "title": "新浪网-航天知识-飞向月球"
31  }
32  },
33  {
34  "_index": "bak_index_v3",
35  "_type": "baike_type",
36  "_id": "AWB_uk1jYGOEa9MAzRs4",
37  "_score": 1,
38  "_source": {
39  "create_time": "2017-12-23 03:36:44",
40  "title": "3DXXX需求"
41  }
42  },
43  {
44  "_index": "bak_index_v3",
45  "_type": "bke_type",
46  "_id": "AWCD2bhbYGOEa9MAzRtZ",
47  "_score": 1,
48  "_source": {
49  "create_time": "2017-12-23 22:49:31",
50  "title": "新浪网-知识-什么是全球定位系统"
51  }
52  }
53  ]
54  }
55}

6、小结

  1. 对于不熟悉的业务场景,可以通过检索英文关键词,通过Google、stackoverflow等最快找到解决方案。此处,切记:不要盲目用中文检索。
  2. 使用完以后,一定要形成知识点总结,便于以后的触类旁通。

参考:脚本检索——

http://t.cn/R3nxhwW

7、这就完了吗?

有没有更便捷的方法。 这里,后期检索的时候,发现如下不需要bool检索的更高效的解决方案: https://goo.gl/EhtJA2

核心的一行脚本: "script": "doc.ActivityDate.date.getMonthOfYear() == 12 && doc.ActivityDate.date.getDayOfMonth() == 9" 对应于我们的问题的解决方案:

代码语言:javascript
复制
1doc['create_time'].getDate().getMonthOfYear() == 11 
2&&  doc['create_time'].getDate(). getDayOfMonth () == 11

同时,这里告诉我们正确的英文关键词的搜索应该为: Elasticsearch Filtering part of date ignoring Year like SQL DATEPART function 你我共深思。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 铭毅天下Elasticsearch 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、题记
  • 2、上问题
  • 3、搜索已有知识储备
  • 4、敲黑板——知识点
  • 5、如何实现呢?
  • 6、小结
  • 7、这就完了吗?
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档