【发布时间】:2015-12-25 05:17:08
【问题描述】:
我正在尝试构建一个弹性搜索查询,以返回所有日期的午夜和当前时间之间的文档。例如,如果我在 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" }'
和一个示例过滤器:
curl -XPOST localhost:9200/test/dt/_search?pretty -d '{
"query": {
"filtered" : {
"filter" : {
"bool": {
"must": [
{
"script" : {
"script" : "doc[\"created_at\"].date.getMinuteOfDay() < 600"
}
}
]
}
}
}
}
}'
其中 600 是一个静态参数,我想根据查询运行的时间将其替换为动态分钟参数。
我最好的尝试是使用 getMinuteOfDay() 方法来过滤每个文档,但我的问题是如何获取当前时间的 getMinuteOfDay()。我已经尝试使用 time() 而不是上述查询中的硬编码参数 600 进行变体,但无法弄清楚。
有什么想法吗?
【问题讨论】:
标签: elasticsearch