更好的方法是使用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