Elasticsearch-“星期几”的DateTime映射

我在课堂上有以下财产:

public DateTime InsertedTimeStamp { get; set; }

通过ES中的以下映射

"insertedTimeStamp ":{

"type":"date",

"format":"yyyy-MM-ddTHH:mm:ssZ"

},

我想进行汇总以返回按“星期几”分组的所有数据,即“星期一”,“星期二” …等

我知道我可以在聚合调用中使用“脚本”来执行此操作,但是,据我了解,如果有很多文档,使用脚本不会对性能产生不小的影响。

有没有一种方法可以用“子属性”映射属性。即我可以用一个字符串:

"somestring":{

"type":"string",

"analyzer":"full_word",

"fields":{

"partial":{

"search_analyzer":"full_word",

"analyzer":"partial_word",

"type":"string"

},

"partial_back":{

"search_analyzer":"full_word",

"analyzer":"partial_word_back",

"type":"string"

},

"partial_middle":{

"search_analyzer":"full_word",

"analyzer":"partial_word_name",

"type":"string"

}

}

},

全部具有.net代码中类的单个属性。

我可以做一些类似的事情来分别存储“完整日期”,然后分别存储“年”,“月”和“天”等(在索引时间存储某种“脚本”),还是我需要在存储中添加更多属性?上课并分别映射它们?这是Transform所做的吗?(现在已贬值,因此似乎表明我需要单独的字段…)

回答:

使用索引pattern_capture过滤器在索引编制时绝对可以做到这一点。

首先,您需要为每个日期部分定义一个分析器+令牌过滤器组合,并将每个分配给日期字段的子字段。每个令牌过滤器将仅捕获其感兴趣的组。

{

"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,您将获得每个填充了相关部分的日期子字段,即

  • date2016-01-22T10:01:23Z
  • date.year2016
  • date.month01
  • date.day22
  • date.hour10
  • date.minute01
  • date.second23

然后,您可以随意在任何这些子字段上进行汇总以获得所需的内容。

以上是 Elasticsearch-“星期几”的DateTime映射 的全部内容, 来源链接: utcz.com/qa/403195.html

回到顶部