过滤范围日期elasticsearch

问题描述:

这是我的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 

首先创建您的映射顶层的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