🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Date Index Name Processor(日期索引名称处理器) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/date-index-name-processor.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/date-index-name-processor.html) 译文链接 : [http://www.apache.wiki/pages/viewpage.action?pageId=10027638](http://www.apache.wiki/pages/viewpage.action?pageId=10027638) 贡献者 : [那伊抹微笑](/display/~wangyangting),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 该 **processor**(处理器)的目的是通过使用 [date math index name support](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html "Date math support in index names") 来将文档指向正确的时间以建立 **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**(月度索引):  ``` curl -XPUT 'localhost:9200/_ingest/pipeline/monthlyindex?pretty' -H 'Content-Type: application/json' -d' { "description": "monthly date-time index naming", "processors" : [ { "date_index_name" : { "field" : "date1", "index_name_prefix" : "myindex-", "date_rounding" : "M" } } ] } ' ``` 针对 **index request**(索引请求)使用该 **pipeline**(管道):  ``` curl -XPUT 'localhost:9200/myindex/type/1?pipeline=monthlyindex&pretty' -H 'Content-Type: application/json' -d' { "date1" : "2016-04-25T12:02:01.789Z" } ' ``` ``` { "_index" : "myindex-2016-04-01", "_type" : "type", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true } ``` 以上请求将不会将此 **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**(处理器)的效果。 ``` curl -XPOST 'localhost:9200/_ingest/pipeline/_simulate?pretty' -H 'Content-Type: application/json' -d' { "pipeline" : { "description": "monthly date-time index naming", "processors" : [ { "date_index_name" : { "field" : "date1", "index_name_prefix" : "myindex-", "date_rounding" : "M" } } ] }, "docs": [ { "_source": { "date1": "2016-04-25T12:02:01.789Z" } } ] } ' ``` 结果如下 :  ``` { "docs" : [ { "doc" : { "_id" : "_id", "_index" : "<myindex-{2016-04-25||/M{yyyy-MM-dd|UTC}}>", "_type" : "_type", "_source" : { "date1" : "2016-04-25T12:02:01.789Z" }, "_ingest" : { "timestamp" : "2016-11-08T19:43:03.850+0000" } } } ] } ``` 上述示例 **index**(索引)已设置为 **&lt;myindex-{2016-04-25||/M{yyyy-MM-dd|UTC}}&gt;**。**Elasticsearch** 理解这个意思,**2016-04-01** 正如 [date math index name documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html "Date math support in index names") 所描述的那样。 ## 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** 或者以下格式之一 : **ISO8601**,**UNIX**,**UNIX_MS** 或 **TAI64N**。 | | **`timezone`** | **no** | **UTC** | 在解析 **date**(日期)以及当 **date math** **index**(日期数学索引)支持解析表达式到具体的 **index** **name**(索引名称)时所使用的 **timezone**(时区)。 | | **`locale`** | **no** | **ENGLISH** | 在从预处理文档中解析 date(日期)时要用的 **locale**(语言环境),与解析的 **month names** 或 **week** **days** 有关。 | | **`index_name_format`** | **no** | **yyyy-MM-dd** | 在打印 **parsed** **date**(解析的日期)到 **index** **name**(索引名称)时要使用的格式。这里预期是一个有效的 **Joda** **pattern。** |