如何在Elasticsearch中进行此查询?

时间:2016-08-18 06:18:24

标签: java postgresql elasticsearch

这样的Postgresql查询:

select * from equipment where production_day < now() + interval '-1 years' * endurance_period + interval '1 month'

endurance_period是表中的列名。 如何在ElasticSearch中将此查询转换为查询DSL(JSON)?

1 个答案:

答案 0 :(得分:0)

您可以对日期字段中的字段进行日期时间操作

在范围过滤器

中使用日期数学/函数操作的小例子
{
    "range" : {
        "date" : {
            "gte" : "now-1d/d",
            "lt" :  "now/d"
        }
    }
}

所以你的查询可能看起来或多或少像这样

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "range": {
                            "created_at": {
                                "lte": "now-1*3d/d"
                            }
                        }
                    }]
                }
            }
        }
    }
}

您可以扩展gte方程以匹配您的搜索过滤条件。

Date Function elasticsearch