Elasticsearch使用script实现时间差值计算统计数据量

elasticsearch duery dsl做时间差值计算

一、SQL

<!--sql转为dsl查询-->
SELECT * FROM news where pubtime>='2020-03-23 00:00:00' and pubtime<'2020-03-24 00:00:00' and source="TRS"  and (crawle_time -pubtime) <= 5分钟;

二、QUERY DSL

将‘一’中的SQL转写为dsl语句。

http://localhost:9210/news_small/monitor_caiji_small/_search
{
    "aggs": {
        "it": {
            "terms": {
                "field": "it",
                "order": {
                    "_count": "DESC"
                },
                "shard_size": 100000,
                "size": 10
            }
        },
        "field_count": {
            "cardinality": {
                "precision_threshold": "100000",
                "field": "it"
            }
        }
    },
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "source_type": "TRS"
                    }
                },
                {
                    "script": {
                        "script": {
                            "inline": "(((doc['crawler_time'].value - doc['pubtime'].value)/(3600000.0/60)>= 0 && ((doc['crawler_time'].value - doc['pubtime'].value)/(3600000.0/60)) <=5))"
                        }
                    }
                }
            ],
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "pubtime": {
                                    "gte": "2020-03-23 00:00:00",
                                    "lt": "2020-03-24 00:00:00"
                                }
                            }
                        }
                    ]
                }
            }
        }
    },
    "size": 0
}

三、SCRIPT调试方法

Painless有一个实用工具方法Debug.explain,它会为你抛出异常。 例如,您可以使用_explain来探索script query可用的上下文。

POST /index/_explain/1
{
  "query": {
    "script": {
      "script": "Debug.explain(doc.goals)"
    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

马超的博客

谢谢大佬的赞赏 :)

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

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

打赏作者

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

抵扣说明:

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

余额充值