使用elasticsearch按营业时间过滤搜索结果
我正在使用elasticsearch来索引和搜索位置,并且我遇到了一个按营业时间过滤的特定问题,我不知道如何解决
基本上,每个位置都有营业时间(一周中的每一天) )并且每天可能有超过1“组”的运行时间(我们现在使用2组)。
例如: 周一: 上午 9 点开门 / 中午 12 点关门 下午 1 点开放/晚上 9 点关闭
给定当前时间和星期几,我需要搜索“开放”位置。
我不知道应该如何将这些营业时间与位置详细信息一起索引,以及如何使用它们来过滤结果,任何帮助、建议将不胜
感激
I'm using elasticsearch to index and search locations, and I'm running into 1 particular issue with filtering by operating hour which I don't know how to work out
Basically, each location will have operating hour (for every day of the week) and each day may have more than 1 "sets" of operating hour (we use 2 for now).
For example:
Monday:
open 9am / close 12pm
open 1pm / close 9pm
Given the current time and the current day of the week, I need to search for the "open" locations.
I don't know how should I index these operating hour together with the location details, and how to use them to filter out the results yet, any help, suggestion would be really appreciated
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更好的方法是使用嵌套文档。
首先:设置映射以指定
hours
文档应被视为嵌套:添加一些数据:(注意开放时间的多个值)
然后运行查询,按当前日期和时间进行过滤:
这应该有效。
不幸的是,在 0.17.5 中,它抛出了一个 NPE - 这可能是一个简单的错误,很快就会被修复。我在这里为此提出了一个问题: https://github.com/elasticsearch/elasticsearch/issues/ 1263
更新 奇怪的是,我现在无法复制 NPE - 此查询似乎在 0.17.5 及更高版本上都能正常工作。一定是一些暂时的故障。
克林特
A better way to do this would be to use
nested
documents.First: set up your mapping to specify that the
hours
document should be treated as nested:Add some data: (note the multiple values for opening hours)
Then run your query, filtering by the current day and time:
This should work.
Unfortunately, in 0.17.5, it throws an NPE - it is likely to be a simple bug which will be fixed shortly. I have opened an issue for this here: https://github.com/elasticsearch/elasticsearch/issues/1263
UPDATE Bizarrely, I now can't replicate the NPE - this query seems to work correctly both on version 0.17.5 and above. Must have been some temporary glitch.
clint
上述解决方案不起作用,因为如果您有一些内容在周一 2-4 点开放,周二 6-8 点开放,那么在周一 6 点进行过滤将返回该文档。下面是一些伪json来说明应该如何完成。
所有时间均应采用标准时区 (GMT) 的 24 小时格式
The above solution doesn't work because if you have something that is open 2-4 on monday and 6-8 on tuesday then doing a filter on monday at 6 will return the document. Below is some pseudojson to illustrate how it should be done.
All times should be in 24 hour format using a standard timezone (GMT)
最简单的方法是在某个位置开放时对时段进行命名和索引。首先,您需要提出一个模式,为每个位置可以开放的时间段分配一个名称。例如,thu17 可能代表星期四下午 5 点。然后,应使用包含以下值的多个“open”字段对示例中的位置进行索引:mon09、mon10、mon11、mon13、mon14、mon15、mon16、mon17、mon18、mon19、mon20、tue09、tue10 等。要仅显示星期四上午 7 点营业的地点,您只需将此过滤器添加到您的查询中:open:thu07。
您不必使用这个特定的命名模式。例如,您可以只计算从一周开始的小时数。在这种情况下,周一上午 9 点将是 9 点,周一晚上 11 点 - 23 点,周二凌晨 2 点 - 26 点,依此类推。
The simplest way to do it is by naming and indexing time slots when a location is open. First, you need to come up with a schema that assigns a name to each time slot when location can be open. For example, thu17 may represent 5PM on Thursday. The location in your example should then be indexed with several fields "open" containing the following values: mon09, mon10, mon11, mon13, mon14, mon15, mon16, mon17, mon18, mon19, mon20, tue09, tue10 and so on. To show only locations that are open on Thursday 7AM, you just need add this filter to your query: open:thu07.
You don't have to use this particular naming schema. You can, for example, just count the number of hours from the beginning of the week. In this case, 9 AM on Monday would be 9, 11PM on Monday - 23, 2AM on Tuesday - 26 and so on.