title: filter range date elasticsearch
body: <p>This is how my datas look like</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "name": "thename",
    "openingTimes": {
      "monday": [
        {
          "start": "10:00",
          "end": "14:00"
        },
        {
          "start": "19:00",
          "end": "02:30"
        }
      ]
    }
  }
  </code></pre>
  
  <p>I want to query this document saying, <code>opened on monday between 13:00 and 14:00</code>.<br>
  I tried this filter but it doesn't return my document:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "filter": {
      "range": {
        "openingTimes.monday.start": {
          "lte": "13:00"
        },
        "openingTimes.monday.end": {
          "gte": "14:00"
        }
      }
    }
  }
  </code></pre>
  
  <p>If I simply say <code>opened on monday at 13:00</code>, it works:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "filter": {
      "range": {
        "openingTimes.monday.start": {
          "lte": "13:00"
        }
      }
    }
  }
  </code></pre>
  
  <p>Or even <code>closing on monday from 14:00</code>, works too:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "filter": {
      "range": {
        "openingTimes.monday.start": {
          "gte": "14:00"
        }
      }
    }
  }
  </code></pre>
  
  <p>but combining both of them doens't give me anything. How can I manage to create a filter meaning  <code>opened on monday between 13:00 and 14:00</code> ?</p>
  
  <h1>EDIT</h1>
  
  <p>This is how I mapped the <code>openingTime</code> field</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "properties": {
      "monday": {
        "type": "nested",
        "properties": {
          "start": {"type": "date","format": "hour_minute"},
          "end": {"type": "date","format": "hour_minute"}
        }
      }
    }
  }
  </code></pre>
  
  <h1>SOLUTION (@DanTuffery)</h1>
  
  <p>Based on @DanTuffery answer I changed my filter to his (which is working perfectly) and added the type definition of my <code>openingTime</code> attribute.</p>
  
  <p>For the record I am using elasticsearch as my primary db through Ruby-on-Rails using the following gems:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>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'
  </code></pre>
  
  <p>Here is how my openingTime attribute's mapping looks like:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>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'}
                                          }
                                        },
                                        ...
                                        ...
                                      }
                                    }
  </code></pre>
  
  <p>And here is how I implemented his filter:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>def self.openedBetween startTime, endTime, day
    self.search filter: {
                  nested: {
                    path: "openingTimes.#{day}",
                    filter: {
                      bool: {
                        must: [
                          {range: {"openingTimes.#{day}.start"=&gt; {lte: startTime}}},
                          {range: {"openingTimes.#{day}.end"  =&gt; {gte: endTime}}}
                        ]
                      }
                    }
                  }
                }
  end
  </code></pre>