使用Elasticsearch按工作时间过滤搜索结果 [英] Filtering search results by operating hour with elasticsearch

查看:312
本文介绍了使用Elasticsearch按工作时间过滤搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Elasticsearch对位置进行索引和搜索,但我遇到了一个特别的问题,即按工作时间进行过滤,但我不知道该如何解决

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

基本上,每个位置都会有一个营业时间(一周中的每一天),并且每天可能会有超过1套设定"的营业时间(我们现在使用2个).

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).

例如: 周一: 上午9点开放/下午12点关闭 打开1pm/关闭9pm

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

致谢

推荐答案

一种更好的方法是使用nested文档.

A better way to do this would be to use nested documents.

首先:设置映射以指定将hours文档视为嵌套文档:

First: set up your mapping to specify that the hours document should be treated as nested:

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"
            }
         }
      }
   }
}
'

添加一些数据:(请注意营业时间的多个值)

Add some data: (note the multiple values for opening hours)

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"
      }
   ]
}
'

然后运行查询,并按当前日期和时间进行过滤:

Then run your query, filtering by the current day and time:

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

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

更新奇怪的是,我现在无法复制NPE-此查询似乎在0.17.5及更高版本上均能正常工作.一定是暂时的故障.

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.

克林特

这篇关于使用Elasticsearch按工作时间过滤搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆