gpt4 book ai didi

time - 使用 elasticsearch 按营业时间过滤搜索结果

转载 作者:行者123 更新时间:2023-11-29 02:51:50 24 4
gpt4 key购买 nike

我正在使用 elasticsearch 来索引和搜索位置,但我遇到了 1 个关于按营业时间过滤的特殊问题,我不知道如何解决

基本上,每个地点都有营业时间(一周中的每一天),并且每天可能有超过 1“组”的营业时间(我们现在使用 2)。

例如:周一:上午 9 点开门/中午 12 点关门下午 1 点开门/晚上 9 点关门

鉴于当前时间和星期几,我需要搜索“开放”位置。

我还不知道如何将这些营业时间与位置详细信息一起编入索引,以及如何使用它们来过滤结果,非常感谢任何帮助和建议

问候

最佳答案

更好的方法是使用嵌套 文档。

首先:设置映射以指定 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 - 这可能是一个简单的错误,很快就会被修复。我在这里为此开了一个问题:https://github.com/elasticsearch/elasticsearch/issues/1263

更新 奇怪的是,我现在无法复制 NPE - 这个查询似乎在 0.17.5 及更高版本上都能正常工作。一定是一些临时故障。

克林特

关于time - 使用 elasticsearch 按营业时间过滤搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com