2014-09-04 131 views
3

這是我的DATAS看怎麼樣過濾範圍日期elasticsearch

{ 
    "name": "thename", 
    "openingTimes": { 
    "monday": [ 
     { 
     "start": "10:00", 
     "end": "14:00" 
     }, 
     { 
     "start": "19:00", 
     "end": "02:30" 
     } 
    ] 
    } 
} 

我想查詢該文件說,opened on monday between 13:00 and 14:00
我想這個過濾器,但它並沒有給我回文件:

{ 
    "filter": { 
    "range": { 
     "openingTimes.monday.start": { 
     "lte": "13:00" 
     }, 
     "openingTimes.monday.end": { 
     "gte": "14:00" 
     } 
    } 
    } 
} 

如果我簡單說opened on monday at 13:00,它的工作原理:

{ 
    "filter": { 
    "range": { 
     "openingTimes.monday.start": { 
     "lte": "13:00" 
     } 
    } 
    } 
} 

甚至closing on monday from 14:00,工程太:

{ 
    "filter": { 
    "range": { 
     "openingTimes.monday.start": { 
     "gte": "14:00" 
     } 
    } 
    } 
} 

但他們兩個都沒有給我任何東西。我如何設法創建一個含義爲opened on monday between 13:00 and 14:00的過濾器?

編輯

這是我的映射openingTime

{ 
    "properties": { 
    "monday": { 
     "type": "nested", 
     "properties": { 
     "start": {"type": "date","format": "hour_minute"}, 
     "end": {"type": "date","format": "hour_minute"} 
     } 
    } 
    } 
} 

SOLUTION(@DanTuffery)

基於@DanTuffery答案,我改變了我的過濾器,以他的(這是可以正常使用)並添加了我的openingTime屬性的類型定義。

對於我使用elasticsearch作爲我的主DB通過的Ruby-on-Rails的使用下面的寶石記錄:

gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git' 
gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git' 
gem 'elasticsearch-persistence', git: 'git://github.com/elasticsearch/elasticsearch-rails.git', require: 'elasticsearch/persistence/model' 

這裏是我的openingTime屬性的映射的樣子:

attribute :openingTimes, Hash, mapping: { 
            type: :object, 
            properties: { 
             monday:  { 
             type: :nested, 
             properties: { 
              start:{type: :date, format: 'hour_minute'}, 
              end: {type: :date, format: 'hour_minute'} 
             } 
             }, 
             tuesday:  { 
             type: :nested, 
             properties: { 
              start:{type: :date, format: 'hour_minute'}, 
              end: {type: :date, format: 'hour_minute'} 
             } 
             }, 
             ... 
             ... 
            } 
            } 

這裏是我如何實現他的過濾器:

def self.openedBetween startTime, endTime, day 
    self.search filter: { 
       nested: { 
        path: "openingTimes.#{day}", 
        filter: { 
        bool: { 
         must: [ 
         {range: {"openingTimes.#{day}.start"=> {lte: startTime}}}, 
         {range: {"openingTimes.#{day}.end" => {gte: endTime}}} 
         ] 
        } 
        } 
       } 
       } 
end 

回答

5

首先創建您的映射頂層的openingTimes對象。

/PUT http://localhost:9200/demo/test/_mapping 
{ 
    "test": { 
    "properties": { 
     "openingTimes": { 
     "type": "object", 
     "properties": { 
      "monday": { 
      "type": "nested", 
      "properties": { 
       "start": { 
       "type": "date", 
       "format": "hour_minute" 
       }, 
       "end": { 
       "type": "date", 
       "format": "hour_minute" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

指數文檔

/POST http://localhost:9200/demo/test/1 
{ 
    "name": "thename", 
    "openingTimes": { 
    "monday": [ 
     { 
     "start": "10:00", 
     "end": "14:00" 
     }, 
     { 
     "start": "19:00", 
     "end": "02:30" 
     } 
    ] 
    } 
} 

隨着嵌套過濾查詢您可以將文檔與布爾範圍查詢內startend字段進行搜索:

/POST http://localhost:9200/demo/test/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "nested": { 
      "path": "openingTimes.monday", 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "range": { 
        "openingTimes.monday.start": { 
         "lte": "13:00" 
        } 
        } 
       }, 
       { 
        "range": { 
        "openingTimes.monday.end": { 
         "gte": "14:00" 
        } 
        } 
       } 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

你的過濾器完美的作品,我永遠不會自己找到它。我在編輯時根據您的答案添加了對代碼所做的更改。 – Micka 2014-09-06 15:42:58

+0

很高興你的工作:) – 2014-09-06 17:02:09