弹性搜索日期范围交点 [英] Elasticsearch date range intersection

查看:108
本文介绍了弹性搜索日期范围交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {timeslot_start_at:2013-02- 01,timeslot_end_at:2013-02-03} 

鉴于我有另一个日期范围(例如从用户输入给出)我想要搜索相交的时间范围。类似于此:确定两个日期范围重叠其中概述了以下逻辑是我以后:

 (StartDate1< = EndDate2)和(StartDate2< = EndDate1)

但我不确定如何将其适合弹性搜索查询,是否使用范围过滤器,只能设置到值,从空白?还是有更有效的方法呢?

解决方案

更新:现在可以使用 date_range 在弹性搜索v5.2中添加的数据类型。对于较早版本的elasticsearch,以下解决方案仍然适用。



要测试交集,您应该使用bool查询将两个范围查询组合成单个查询:

  {
bool:{
must:[
{
range :{
timeslot_start_at:{
lte:2013-02-28
}
}
},
{
range:{
timeslot_end_at:{
gte:2013-02-03
}
}
}
]
}
}


I'm storing something like the following information in elastic search:

{ "timeslot_start_at" : "2013-02-01", "timeslot_end_at" : "2013-02-03" }

Given that I have another date range (given from user input for example) I am wanting to search for an intersecting time range. Similar to this: Determine Whether Two Date Ranges Overlap Which outlines that the following logic is what i'm after:

(StartDate1 <= EndDate2) and (StartDate2 <= EndDate1)

But I'm unsure of how to fit this into an elastic search query, would I use a range filter and only set the 'to' values, leaving the from blank? Or is there a more efficient way of doing this?

解决方案

Update: It is now possible to use date_range data type that was added in elasticsearch v5.2. For an earlier version of elasticsearch the following solution still applies.

To test for intersection, you should combine two range queries into a single query using bool query:

{
    "bool": {
        "must": [
            {
                "range": {
                    "timeslot_start_at": {
                        "lte": "2013-02-28"
                    }
                }
            },
            {
                "range": {
                    "timeslot_end_at": {
                        "gte": "2013-02-03"
                    }
                }
            }
        ]
    }
}

这篇关于弹性搜索日期范围交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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