ElasticSearch:如何使用月份和日期查询日期字段日期范围过滤器 [英] ElasticSearch: How to query a date field using a month & date range filter

查看:1000
本文介绍了ElasticSearch:如何使用月份和日期查询日期字段日期范围过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我已经知道如何从(时间戳)日期字段中过滤日期范围。这很简单:

Currently, I already know how to filter a date range from a (timestamp) date field. That's an easy one:

"range": {
    "date": {
        "gte": "2015-11-01",
        "lte": "2015-11-30"
    }
}

但是,当您对基于月份的范围感兴趣时,如何过滤日期,例如 gte: 02-22 lte: 03-21

But how to filter dates when you are interested in ranges based on month like gte:"02-22" and lte:"03-21"? Is this possible?

映射:

PUT dob
{
    "mappings": {
        "test":{
            "properties": {
                "dob":{
                    "type": "date",
                    "format": "dateOptionalTime"
                }
            }
        }
    }     
}

查询:

GET /dob/test/_search
{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "date": {
                                    "gte": "02-11",
                                    "lte": "03-20"
                                }
                            }
                        },
                        {
                            "script": {
                                "script": "doc.dob.date.getDayOfMonth() >= min && doc.dob.date.getMonthOfYear() <= max",
                                "params": {
                                    "min": 12,
                                    "max": 2
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

我想输出类似 date大于:02-11且小于:03-20 但我不想更改日期类型格式( yyyy-mm-dd ),也不想按字符串搜索它。

I want to output something like date greater than:02-11 & less than:03-20 but I don't want to change my date type format (yyyy-mm-dd) and also I don't want to search it by string. If i am doing something wrong please make me correct.

推荐答案

您可以在日期过滤器中指定格式,例如

You can specify format in date filter like

"date":
{
    "gte":"02-22","lte":03-20","format":"mm-dd"
}

这篇关于ElasticSearch:如何使用月份和日期查询日期字段日期范围过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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