更好的方法是使用nested
文件。
第一:設置你的映射指定爲嵌套在hours
文件應被視爲:
curl -XPUT 'http://127.0.0.1:9200/foo/?pretty=1' -d '
{
"mappings" : {
"location" : {
"properties" : {
"hours" : {
"include_in_root" : 1,
"type" : "nested",
"properties" : {
"open" : {
"type" : "short"
},
"close" : {
"type" : "short"
},
"day" : {
"index" : "not_analyzed",
"type" : "string"
}
}
},
"name" : {
"type" : "string"
}
}
}
}
}
'
添加一些數據:(注意,營業時間多值)
curl -XPOST 'http://127.0.0.1:9200/foo/location?pretty=1' -d '
{
"name" : "Test",
"hours" : [
{
"open" : 9,
"close" : 12,
"day" : "monday"
},
{
"open" : 13,
"close" : 17,
"day" : "monday"
}
]
}
'
然後運行您的查詢,按當前日期和時間篩選:
curl -XGET 'http://127.0.0.1:9200/foo/location/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"query" : {
"text" : {
"name" : "test"
}
},
"filter" : {
"nested" : {
"path" : "hours",
"filter" : {
"and" : [
{
"term" : {
"hours.day" : "monday"
}
},
{
"range" : {
"hours.close" : {
"gte" : 10
}
}
},
{
"range" : {
"hours.open" : {
"lte" : 10
}
}
}
]
}
}
}
}
}
}
'
這應該工作。
不幸的是,在0.17.5中,它引發了一個NPE - 它很可能是一個簡單的bug,它很快就會被修復。我已經打開了一個問題,這在這裏:https://github.com/elasticsearch/elasticsearch/issues/1263
UPDATE奇怪的是,我現在不能複製NPE - 這個查詢似乎正常工作都在0.17.5和以上版本。一定是暫時的小故障。
克林特
感謝克林特,這看起來非常整齊,我是跟蹤您在ElasticSearch上發佈的問題。 – mr1031011
感謝您的更新 – mr1031011