Elasticsearch日期范围查询,使用两个字段 [英] Elasticsearch date range query using two fields

查看:1683
本文介绍了Elasticsearch日期范围查询,使用两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储的文档包含两个字段,即startDate和endDate。我想使用输入日期运行Elastic查询,并返回其startDate和endDate包含该日期的所有文档。例如,

I'm storing documents with two fields, startDate and endDate. I want to run Elastic queries with an input date and return all documents whose startDate and endDate contain this date. For example,

doc1:
"_source": {
     "startDate": "2015-01-01",
     "endDate": "2015-01-10"
}

如果我输入的日期为2015年1月2日,则该文档应出现在结果中,因为输入的日期在开始日期和结束日期字段的范围内。

If I search with an input date of 2015-01-02, this document should appear in the results because the input date falls in the range of the start and end date fields.

我可以使用一个字段进行范围查询,但由于范围仅接受一个,因此我不知道如何使用两个日期字段:

I'm able to do a range query using one field, but I don't know how to use two date fields since range only accepts one:

{
    "query": {
        "range" : {
            "startDate" : {
                "lte": "2015-01-02"
            }
        }
    }
}

我还需要在同一日期执行将 endDate设置为 gte的范围查询。那将确定我需要检查的时间范围。任何建议将不胜感激。

I need to also perform a range query with "endDate" set to "gte" on that same date. That would establish the time range that I need to check. Any advice would be appreciated.

编辑:我最终希望使用Olivere的弹性库在Go中将其转换为Elasticsearch查询。

I eventually want to convert this into Elasticsearch queries in Go using olivere's elastic library.

推荐答案

您可以使用 filter 子句来做到这一点:

You can do this with filter clause:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "startDate": {
              "lte": "<startDate>"
            }
          }
        },
        {
          "range": {
            "endDate": {
              "gte": "<endDate>"
            }
          }
        }
      ]
    }
  }
}

这篇关于Elasticsearch日期范围查询,使用两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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