过滤范围日期弹性搜索

Mic*_*cka 3 date range filter elasticsearch

这就是我的数据的样子

{
  "name": "thename",
  "openingTimes": {
    "monday": [
      {
        "start": "10:00",
        "end": "14:00"
      },
      {
        "start": "19:00",
        "end": "02:30"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我想查询这个文件说,opened on monday between 13:00 and 14:00.
我尝试过这个过滤器,但它没有返回我的文档:

{
  "filter": {
    "range": {
      "openingTimes.monday.start": {
        "lte": "13:00"
      },
      "openingTimes.monday.end": {
        "gte": "14:00"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我只是说opened on monday at 13:00,它有效:

{
  "filter": {
    "range": {
      "openingTimes.monday.start": {
        "lte": "13:00"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

甚至closing on monday from 14:00,也是有效的:

{
  "filter": {
    "range": {
      "openingTimes.monday.start": {
        "gte": "14:00"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但结合他们两个并没有给我任何东西.如何设置创建过滤器含义 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"}
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

解决方案(@DanTuffery)

根据@DanTuffery的答案,我将我的过滤器更改为他的(这完美地工作)并添加了我的openingTime属性的类型定义.

为了记录我通过Ruby-on-Rails使用elasticsearch作为我的主要数据库使用以下gem:

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'
Run Code Online (Sandbox Code Playgroud)

以下是我的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'}
                                        }
                                      },
                                      ...
                                      ...
                                    }
                                  }
Run Code Online (Sandbox Code Playgroud)

这是我实现他的过滤器的方式:

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
Run Code Online (Sandbox Code Playgroud)

Dan*_*ery 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"
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

索引您的文档

/POST http://localhost:9200/demo/test/1
{
  "name": "thename",
  "openingTimes": {
    "monday": [
      {
        "start": "10:00",
        "end": "14:00"
      },
      {
        "start": "19:00",
        "end": "02:30"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

使用嵌套过滤器查询,您可以使用布尔范围查询中的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"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)