首页 > 编程知识 正文

elasticsearch实战讲解,elasticsearch实战教学

时间:2023-05-04 09:00:56 阅读:261945 作者:1410

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。

PUT my_index{ "mappings": { "_doc": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } }}

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

GET _search{ "query": { "range" : { "timestamp" : { "gte": "2015-01-01 00:00:00", "lte": "now", "time_zone": "+01:00" } } }}

参考:
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中的脚步语言。
应用举例:

GET /_search{ "query": { "bool" : { "must" : { "script" : { "script" : { "source": "doc['num1'].value > 1", "lang": "painless" } } } } }}

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

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

5、如何实现呢?

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

具体实现:

POST index_*/_search{ "size":100, "_source": { "includes": [ "title", "create_time" ] }, "query": { "bool" : { "must" : [{ "script" : { "script" : { "inline": "doc['create_time'].getDate().getMonthOfYear() == 12", "lang": "painless" } } }, { "script" : { "script" : { "inline": "doc['create_time'].getDate().getDayOfMonth() == 23", "lang": "painless" } } } ] } }}

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

{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 8536, "max_score": 1, "hits": [ { "_index": "bak_index_v3", "_type": "bke_type", "_id": "AWCEDIuUYGOEa9MAzRvk", "_score": 1, "_source": { "create_time": "2017-12-23 23:45:02", "title": "世界道学网-道学文摘-唱道真言" } }, { "_index": "bak_index_v3", "_type": "bke_type", "_id": "AWCD2bs4YGOEa9MAzRta", "_score": 1, "_source": { "create_time": "2017-12-23 22:49:32", "title": "新浪网-航天知识-飞向月球" } }, { "_index": "bak_index_v3", "_type": "baike_type", "_id": "AWB_uk1jYGOEa9MAzRs4", "_score": 1, "_source": { "create_time": "2017-12-23 03:36:44", "title": "3DXXX需求" } }, { "_index": "bak_index_v3", "_type": "bke_type", "_id": "AWCD2bhbYGOEa9MAzRtZ", "_score": 1, "_source": { "create_time": "2017-12-23 22:49:31", "title": "新浪网-知识-什么是全球定位系统" } } ] }} 6、小结 对于不熟悉的业务场景,可以通过检索英文关键词,通过Google、stackoverflow等最快找到解决方案。此处,切记:不要盲目用中文检索。使用完以后,一定要形成知识点总结,便于以后的触类旁通。

参考:脚本检索——

http://t.cn/R3nxhwW

7、这就完了吗?

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

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

doc['create_time'].getDate().getMonthOfYear() == 11 && doc['create_time'].getDate(). getDayOfMonth () == 11

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


加入知识星球,更短时间更快习得更多干货!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。