2016-08-18 32 views
3

PostgreSQL的查询是这样的:我如何使这个查询在Elasticsearch?

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

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

回答

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

+0

'*'操作符做什么? – Roger

+0

它应该倍增。在发布之前,我尝试了几个虚拟查询,对我来说工作得很好。 – user3775217

+0

你能告诉我一些示例查询吗? – star4689