2016-08-18 34 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