简体   繁体   English

Elasticsearch日期范围查询使用两个字段

[英]Elasticsearch date range query using two fields

I'm storing documents with two fields, startDate and endDate. 我正在存储包含两个字段的文档:startDate和endDate。 I want to run Elastic queries with an input date and return all documents whose startDate and endDate contain this date. 我想运行带有输入日期的弹性查询,并返回其startDate和endDate包含此日期的所有文档。 For example, 例如,

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

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. 如果我使用2015-01-02的输入日期进行搜索,则此文档应出现在结果中,因为输入日期属于开始日期和结束日期字段的范围。

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

I need to also perform a range query with "endDate" set to "gte" on that same date. 我还需要在同一天将“endDate”设置为“gte”来执行范围查询。 That would establish the time range that I need to check. 这将确定我需要检查的时间范围。 Any advice would be appreciated. 任何意见,将不胜感激。

EDIT: I eventually want to convert this into Elasticsearch queries in Go using olivere's elastic library. 编辑:我最终希望使用olivere的弹性库将其转换为Go中的Elasticsearch查询。

You can do this with filter clause: 您可以使用filter子句执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM