elasticsearch:getMinuteOfDay()应用于time()在日期范围过滤器 [英] elasticsearch: getMinuteOfDay() applied to time() in date range filter

查看:363
本文介绍了elasticsearch:getMinuteOfDay()应用于time()在日期范围过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个弹性搜索查询,以便在所有日期之间的午夜和当前时间之间返回文档。例如,如果我在09:00:00运行查询,则查询将返回任何文档,时间戳记在午夜和09:00:00之间,无论日期如何。



这是一个示例数据集:

  curl -XPUT localhost:9200 / test / dt / _mapping -d'{dt :{properties:{created_at:{type:date,format:YYYY-MM-DD HH:mm:ss}}}}'

curl -XPOST localhost:9200 / test / dt / 1 -d'{created_at:2014-10-09 07:00:00}'
curl -XPOST localhost:9200 / test / dt / 2 -d'{created_at:2014-10-09 14:00:00}'
curl -XPOST localhost:9200 / test / dt / 3 -d'{created_at:2014-10 -08 08:00:00}'
curl -XPOST localhost:9200 / test / dt / 4 -d'{created_at:2014-10-08 15:00:00}'
curl -XPOST localhost:9200 / test / dt / 5 -d'{created_at:2014-10-07 09:00:00}'
curl -XPOST localhost:9200 / test / dt / 6 -d'{created_at:2014-10-07 16:00:00}'

和示例过滤器:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
filter:{
bool:{
must:[
{
script:{
script:doc [\created_at\]。date.getMinuteOfDay()< 600
}
}
]
}
}
}
}
}'

其中600是一个静态参数,我想用动态分钟参数替换,具体取决于运行查询的时间。



我最好的尝试是使用getMinuteOfDay()方法来过滤每个文档,但我的问题是如何获取当前时间的getMinuteOfDay()



任何想法?

解决方案

这是很晚,但可以使用 DateTime.now()将您的查询更改为

  GET test / dt / _search 
{
query:{
filtered:{
filter:{
bool:{
must:[
{
script:{
script:doc [\created_at\]。date.getMinuteOfDay()< DateTime.now()。getMinuteOfDay()
}
}
]
}
}
}
}
}

希望这有帮助!


I'm trying to build an elasticsearch query to return documents for times between midnight and the current time of day, for all dates. For example, if I run the query at 09:00:00, then the query will return any document with a timestamp between midnight and 09:00:00 regardless of the date.

Here's an example dataset:

curl -XPUT  localhost:9200/test/dt/_mapping -d '{"dt" : {"properties" : {"created_at" : {"type" : "date", "format": "YYYY-MM-DD HH:mm:ss" }}}}'

curl -XPOST localhost:9200/test/dt/1 -d '{ "created_at": "2014-10-09 07:00:00" }'
curl -XPOST localhost:9200/test/dt/2 -d '{ "created_at": "2014-10-09 14:00:00" }'
curl -XPOST localhost:9200/test/dt/3 -d '{ "created_at": "2014-10-08 08:00:00" }'
curl -XPOST localhost:9200/test/dt/4 -d '{ "created_at": "2014-10-08 15:00:00" }'
curl -XPOST localhost:9200/test/dt/5 -d '{ "created_at": "2014-10-07 09:00:00" }'
curl -XPOST localhost:9200/test/dt/6 -d '{ "created_at": "2014-10-07 16:00:00" }'

and an example filter:

curl -XPOST localhost:9200/test/dt/_search?pretty -d '{
  "query": {
    "filtered" : {
       "filter" : {
        "bool": {
          "must": [
            {
              "script" : {
            "script" : "doc[\"created_at\"].date.getMinuteOfDay() < 600"
              }
            }
          ]
        }
      }
    }
  }
}'

where 600 is a static parameter that I want to replace with a dynamic minutes parameter depending on the time of day when the query is run.

My best attempt is to use the getMinuteOfDay() method to filter each doc, but my problem is how to get getMinuteOfDay() for the current time. I've tried variations using time() instead of the hard-coded parameter 600 in the above query, but can't figure it out.

Any ideas?

解决方案

This is pretty late but this can be done with DateTime.now(), you should change your query to

GET test/dt/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "script": {
                "script": "doc[\"created_at\"].date.getMinuteOfDay() < DateTime.now().getMinuteOfDay()"
              }
            }
          ]
        }
      }
    }
  }
}

Hope this helps!

这篇关于elasticsearch:getMinuteOfDay()应用于time()在日期范围过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆