2014-10-09 52 views
4

我試圖構建一個elasticsearch查詢來返回午夜和當天的所有日期之間的時間文檔。例如,如果我在09:00:00運行查詢,那麼無論日期如何,查詢都會返回任何在午夜和09:00:00之間帶有時間戳的文檔。elasticsearch:getMinuteOfDay()適用於日期範圍中的time()過濾器

下面是一個例子數據集:

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的變體,但無法弄清楚。

任何想法?

回答

0

這是非常晚了,但是這可以通過DateTime.now()做,你應該將查詢更改爲

GET test/dt/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "script": { 
       "script": "doc[\"created_at\"].date.getMinuteOfDay() < DateTime.now().getMinuteOfDay()" 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

希望這有助於!