简体   繁体   English

文本字段上的 Elasticsearch 日期范围查询

[英]Elasticsearch date range query over text field

I have 2 date fields in my documents say date_field_1 and date_field_2 .我的文档中有 2 个日期字段,比如date_field_1date_field_2 All documents has date_feild_1 data, but for date_filed_2 few of them have empty string.所有文档都有date_feild_1数据,但对于date_filed_2 ,其中很少有空字符串。 Because of this date_field_1 is marked as date field and date_field_2 is marked as text field.因此date_field_1被标记为日期字段,而date_field_2被标记为文本字段。

Now when I try executing the query,现在,当我尝试执行查询时,

{"range": {"date_field_1": {"gte": "2022-05-23", "lte": "2022-05-23"}}}

I am getting proper results.我得到了正确的结果。 But when I try the same for date_field_2 I am getting empty result.但是,当我对date_field_2尝试相同的操作时,我得到的结果是空的。 Is there a way to apply range query to date_field_2 over non empty documents?有没有办法在非空文档上将范围查询应用于date_field_2

You can use regex eventually:您最终可以使用正则表达式:

{
    "range": {
        "date_field_2": "2022-0[3-6]-[0-9][0-9]"
    }
}

The above query will return anything in between 2022-03-01 and 2022-06-30上述查询将返回2022-03-012022-06-30之间的任何内容
Basically you request data that contain in date_field_2 a value that contain 2022-0 then any number in 3-6 range, then a - and any 2 numbers in 0-9 range基本上,您请求在date_field_2中包含一个值的数据,该值包含2022-0 ,然后是3-6范围内的任何数字,然后是-0-9范围内的任何 2 个数字

Please be aware that it will also return wrong values, IF THEY EXISTS (like 2022-03-87 ) but that's a different story请注意,如果它们存在(例如2022-03-87 ),它也会返回错误的值,但这是另一回事

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

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