【发布时间】:2014-09-07 00:46:30
【问题描述】:
这就是我的数据的样子
{
"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"}
}
}
}
}
解决方案 (@DanTuffery)
根据@DanTuffery 的回答,我将过滤器更改为他的(效果很好),并添加了openingTime 属性的类型定义。
作为记录,我使用以下 gems 通过 Ruby-on-Rails 使用 elasticsearch 作为我的主数据库:
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
【问题讨论】:
标签: date filter elasticsearch range