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