本文介绍了如何获取当前时间作为脚本使用的unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个字段:
- date_start(类型:日期)
- date_end(类型:日期)
- 永久(类型:bool)
我想退回所有符合这些条件的文件:
I would like to return all documents with theses conditions:
date_start <= now AND date_end >= now
OR
date_start <= now AND permanent == true
最好的方法是什么?
我认为应该使用这样的脚本:
I thought it would be to use a script like this :
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "((doc['date_start'].value <= params.now) && (doc['date_end'].value >= params.now)) || ((doc['date_start'].value <= params.now) && (doc['permanent'].value == params.permanent))"
},
"lang": "painless",
"params": {
"now": "1594390526",
"permanent": true
}
}
}
]
}
}
}
但是日期类型比较存在问题,我不知道如何解决.谢谢
But there is an issue with date types comparison and I don't know how to solve this.Thank you
推荐答案
感谢 joe 的回答.我没有设法正确实现它,但我认为这是脚本解决方案的一个很好的起点.
Thanks joe for your answer. I did not manage to implement it correctly but I think this is a good starting point for a script solution.
但是,我找到了另一种方法来使用 range 和 bool/should 的组合来做我想做的事情.
However, I find another way to do what I wanted using combination of range and bool/should.
这里是:
{
"query": {
"bool": {
"filter": [
{
"range": {
"date_start": {
"lte": "now"
}
}
},
{
"bool": {
"should": [
{
"range": {
"date_end": {
"gte": "now"
}
}
},
{
"term": {
"permanent": true
}
}
]
}
}
]
}
}
}
这篇关于如何获取当前时间作为脚本使用的unix时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!