Date Index Name Processor(日期索引名称处理器)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/date-index-name-processor.html

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10027638

贡献者 : 那伊抹微笑ApacheCNApache中文网

processor(处理器)的目的是通过使用 date math index name support 来将文档指向正确的时间以建立 index(索引),该 date(时间)基于 document(文档)中的 date(日期)或 timestamp(时间戳)字段。

processor(处理器)根据提供的 index name prefix(索引名称前缀),正在被处理的 document(文档)中的 date(日期)或 timestamp(时间戳)字段以及所提供的 date rounding(日期舍入),与 a date math index name expression(一个日期的数学表达式索引名称)来设置 _index meta field(元字段)。

首先,该 processor(处理器)从正在处理的 document(文档)中的 field(字段)抓取 date(日期)或 timestamp(时间戳)。或者,可以配置 date formatting(日期格式)以便如何将 field’s value(字段的值)解析成 date(日期)。那么这个 date(日期),所提供的 index name prefix(索引名称前缀)和所提供的 date rounding(日期舍入)将会被格式化为 a date math index name expression(一个日期的数学表达式索引名称)。此外,还可以选择 date formatting(日期格式化)以指定 date(日期)应该如何被格式化成 a date math index name expression(一个日期的数学表达式索引名称)。

以下一个示例 pipeline(管道),它将 documents(文档)指向基于 date1 字段中日期的,以 myindex- 前缀开头的 monthly index(月度索引):

  1. curl -XPUT 'localhost:9200/_ingest/pipeline/monthlyindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "description": "monthly date-time index naming",
  4. "processors" : [
  5. {
  6. "date_index_name" : {
  7. "field" : "date1",
  8. "index_name_prefix" : "myindex-",
  9. "date_rounding" : "M"
  10. }
  11. }
  12. ]
  13. }
  14. '

针对 index request(索引请求)使用该 pipeline(管道):

  1. curl -XPUT 'localhost:9200/myindex/type/1?pipeline=monthlyindex&pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "date1" : "2016-04-25T12:02:01.789Z"
  4. }
  5. '
  1. {
  2. "_index" : "myindex-2016-04-01",
  3. "_type" : "type",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "created" : true
  13. }

以上请求将不会将此 document(文档)放入 myindex 索引,而是放入到 myindex-2016-04-01 索引中,因为它 rounded by month(按月舍入)。产生这样的结果是因为 date-index-name-processor(日期索引名称处理器)覆盖了 document(文档)的 _index 属性。

为了查看实际的 index request(索引请求)中提供给 index(索引)的 date-math 值,导致上述 document(文档)被索引到 myindex-2016-04-01 中,我们可以使用 simulate request(模拟请求)来检查 processor(处理器)的效果。

  1. curl -XPOST 'localhost:9200/_ingest/pipeline/_simulate?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "pipeline" :
  4. {
  5. "description": "monthly date-time index naming",
  6. "processors" : [
  7. {
  8. "date_index_name" : {
  9. "field" : "date1",
  10. "index_name_prefix" : "myindex-",
  11. "date_rounding" : "M"
  12. }
  13. }
  14. ]
  15. },
  16. "docs": [
  17. {
  18. "_source": {
  19. "date1": "2016-04-25T12:02:01.789Z"
  20. }
  21. }
  22. ]
  23. }
  24. '

结果如下 :

  1. {
  2. "docs" : [
  3. {
  4. "doc" : {
  5. "_id" : "_id",
  6. "_index" : "<myindex-{2016-04-25||/M{yyyy-MM-dd|UTC}}>",
  7. "_type" : "_type",
  8. "_source" : {
  9. "date1" : "2016-04-25T12:02:01.789Z"
  10. },
  11. "_ingest" : {
  12. "timestamp" : "2016-11-08T19:43:03.850+0000"
  13. }
  14. }
  15. }
  16. ]
  17. }

上述示例 index(索引)已设置为 <myindex-{2016-04-25||/M{yyyy-MM-dd|UTC}}>Elasticsearch 理解这个意思,2016-04-01 正如 date math index name documentation 所描述的那样。

Table 17. Date index name options(表 17. 日期索引名称选项)

Name(名称) Required(必要的) Default(默认值) Description(描述)
field yes - 获取 date(日期)或 timestamp(时间戳)的 field(字段)。
index_name_prefix no - 在打印日期之前要添加的 index name(索引名称)的前缀。
date_rounding yes - 在格式化 date(日期)为 index name(索引名称)时如何舍入日期。有效值是 : y (year), M(month), w (week), d (day), h (hour), m (minute) and s (second)。
date_formats no yyyy-MM-dd’T’HH:mm:ss.SSSZ 预处理 document(文档)中用于解析 dates(日期) / timestamps(时间戳)的预期的 date formats(日期格式)的 array(数组)。可以是 Joda pattern 或者以下格式之一 : ISO8601UNIXUNIX_MSTAI64N
timezone no UTC 在解析 date(日期)以及当 date math index(日期数学索引)支持解析表达式到具体的 index name(索引名称)时所使用的 timezone(时区)。
locale no ENGLISH 在从预处理文档中解析 date(日期)时要用的 locale(语言环境),与解析的 month namesweek days 有关。
index_name_format no yyyy-MM-dd 在打印 parsed date(解析的日期)到 index name(索引名称)时要使用的格式。这里预期是一个有效的 Joda pattern。