gpt4 book ai didi

date - 过滤范围日期elasticsearch

转载 作者:行者123 更新时间:2023-11-29 02:54:45 25 4
gpt4 key购买 nike

这是我的数据的样子

{
"name": "thename",
"openingTimes": {
"monday": [
{
"start": "10:00",
"end": "14:00"
},
{
"start": "19:00",
"end": "02:30"
}
]
}
}

我想查询这个文档说,opened on monday between 13:00 and 14:00
我试过这个过滤器,但它没有返回我的文档:

{
"filter": {
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
},
"openingTimes.monday.end": {
"gte": "14:00"
}
}
}
}

如果我简单地说周一 13:00 开放,它会起作用:

{
"filter": {
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
}
}
}
}

甚至 周一 14:00 关闭,也可以:

{
"filter": {
"range": {
"openingTimes.monday.start": {
"gte": "14:00"
}
}
}
}

但是将它们结合起来并没有给我任何东西。我如何设法创建一个过滤器,意思是 在星期一 13:00 到 14:00 之间打开

编辑

这就是我映射 openingTime 字段的方式

{
"properties": {
"monday": {
"type": "nested",
"properties": {
"start": {"type": "date","format": "hour_minute"},
"end": {"type": "date","format": "hour_minute"}
}
}
}
}

解决方案(@DanTuffery)

根据@DanTuffery 的回答,我将我的过滤器更改为他的过滤器(工作正常)并添加了我的 openingTime 属性的类型定义。

郑重声明,我使用以下 gems 通过 Ruby-on-Rails 使用 elasticsearch 作为我的主要数据库:

gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-persistence', git: 'git://github.com/elasticsearch/elasticsearch-rails.git', require: 'elasticsearch/persistence/model'

这是我的 openingTime 属性映射的样子:

attribute :openingTimes, Hash,    mapping: {
type: :object,
properties: {
monday: {
type: :nested,
properties: {
start:{type: :date, format: 'hour_minute'},
end: {type: :date, format: 'hour_minute'}
}
},
tuesday: {
type: :nested,
properties: {
start:{type: :date, format: 'hour_minute'},
end: {type: :date, format: 'hour_minute'}
}
},
...
...
}
}

下面是我如何实现他的过滤器:

def self.openedBetween startTime, endTime, day
self.search filter: {
nested: {
path: "openingTimes.#{day}",
filter: {
bool: {
must: [
{range: {"openingTimes.#{day}.start"=> {lte: startTime}}},
{range: {"openingTimes.#{day}.end" => {gte: endTime}}}
]
}
}
}
}
end

最佳答案

首先使用顶层的 openingTimes 对象创建映射。

/PUT http://localhost:9200/demo/test/_mapping
{
"test": {
"properties": {
"openingTimes": {
"type": "object",
"properties": {
"monday": {
"type": "nested",
"properties": {
"start": {
"type": "date",
"format": "hour_minute"
},
"end": {
"type": "date",
"format": "hour_minute"
}
}
}
}
}
}
}
}

索引你的文档

/POST http://localhost:9200/demo/test/1
{
"name": "thename",
"openingTimes": {
"monday": [
{
"start": "10:00",
"end": "14:00"
},
{
"start": "19:00",
"end": "02:30"
}
]
}
}

使用嵌套过滤器查询,您可以在 bool 范围查询中使用 startend 字段搜索文档:

/POST http://localhost:9200/demo/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "openingTimes.monday",
"filter": {
"bool": {
"must": [
{
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
}
}
},
{
"range": {
"openingTimes.monday.end": {
"gte": "14:00"
}
}
}
]
}
}
}
}
}
}
}

关于date - 过滤范围日期elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671150/

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