絕對有可能在索引時使用pattern_capture
token filter來做到這一點。
您首先要爲每個日期部分定義一個分析器+標記過濾器組合,並將其分配給日期字段的子字段。每個令牌過濾器將只捕獲組是感興趣的
{
"settings": {
"analysis": {
"analyzer": {
"year_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"year"
]
},
"month_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"month"
]
},
"day_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"day"
]
},
"hour_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"hour"
]
},
"minute_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"minute"
]
},
"second_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"second"
]
}
},
"filter": {
"year": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"(\\d{4})-\\d{2}-\\d{2}[tT]\\d{2}:\\d{2}:\\d{2}[zZ]"
]
},
"month": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"\\d{4}-(\\d{2})-\\d{2}[tT]\\d{2}:\\d{2}:\\d{2}[zZ]"
]
},
"day": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"\\d{4}-\\d{2}-(\\d{2})[tT]\\d{2}:\\d{2}:\\d{2}[zZ]"
]
},
"hour": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"\\d{4}-\\d{2}-\\d{2}[tT](\\d{2}):\\d{2}:\\d{2}[zZ]"
]
},
"minute": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"\\d{4}-\\d{2}-\\d{2}[tT]\\d{2}:(\\d{2}):\\d{2}[zZ]"
]
},
"second": {
"type": "pattern_capture",
"preserve_original": false,
"patterns": [
"\\d{4}-\\d{2}-\\d{2}[tT]\\d{2}:\\d{2}:(\\d{2})[zZ]"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"date": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ssZ",
"fields": {
"year": {
"type": "string",
"analyzer": "year_analyzer"
},
"month": {
"type": "string",
"analyzer": "month_analyzer"
},
"day": {
"type": "string",
"analyzer": "day_analyzer"
},
"hour": {
"type": "string",
"analyzer": "hour_analyzer"
},
"minute": {
"type": "string",
"analyzer": "minute_analyzer"
},
"second": {
"type": "string",
"analyzer": "second_analyzer"
}
}
}
}
}
}
}
然後,當你指數的日期,如2016-01-22T10:01:23Z
,你會得到每一個填入相關的部分,即日期子場
date
:2016-01-22T10:01:23Z
date.year
:2016
date.month
:01
date.day
:22
date.hour
:10
date.minute
:01
date.second
:23
你就可以自由地聚集在任何這些子領域的得到你想要的東西。
主要使用此解決方案結束。我確實添加了第二個字段來實際存儲'日',因爲我想保持日期格式符合ISO,但是這絕對簡化了解決方案。 –
謝謝。我很高興這對你有幫助! – Val
這很好,但是如果我想'year','month'等等是關鍵字呢? –