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
你的過濾器完美的作品,我永遠不會自己找到它。我在編輯時根據您的答案添加了對代碼所做的更改。 – Micka 2014-09-06 15:42:58
很高興你的工作:) – 2014-09-06 17:02:09