企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Date math support in index names(索引名称对 Date 和 Math 的支持) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/date-math-index-names.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/date-math-index-names.html) 译文链接 : [http://www.apache.wiki/pages/viewpage.action?pageId=4882849](http://www.apache.wiki/pages/viewpage.action?pageId=4882849) 贡献者 : [小瑶](/display/~chenyao),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) **Date math** 索引名称解析可以让您搜索一系列 **time-series indices**(时间序列索引),而不是搜索所有时间序列索引并过滤结果或维护 **aliases**(别名)。限制搜索的索引数量减少了集群上的集群上的负载,并提高了执行性能。例如,如果您在日常日志中 **searching for errors**(搜索错误 ),则可以使用 **date math name** 模板将搜索限制为过去两天。 几乎所有的具有索引参数的 **API** 都支持在 **index parameter value**(索引参数值)中包含 **date math** 。 **date math** 索引名称采用以下形式 :  ``` <static_name{date_math_expr{date_format|time_zone}}> ``` 上述的说明 :  | 位置 | 说明 | | --- | --- | | **static_name** | 是名称的 **static text**( 静态文本)部分 | | **date_math_expr** | 是动态计算日期的动态 **date math** 表达式 | | **date_format** | 是计算日期应呈现的可选格式。默认是 **YYYY.MM.dd** | | **time_zone** | 是可选的时区。默认为 **utc** 。 | 你必须将 **date math** 索引名称表达式包含在尖括号中,并且所有的特殊字符都应进行 **URI** 编码。例如 :  ``` # GET /<logstash-{now/d}>/_search curl -XGET 'localhost:9200/%3Clogstash-%7Bnow%2Fd%7D%3E/_search?pretty' -H 'Content-Type: application/json' -d' { "query" : { "match": { "test": "data" } } } ' ``` Note(注意): ## date math 字符的百分号编码 用于 **date** 舍入的特殊字符必须按照如下** URI** 编码 : | 字符 | 编码 | | --- | --- | | `&lt;` | `%3C` | | &gt; | %3E | | / | `%2F` | | { | %7B | | } | %7D | | &#124; | `%7C` | | + | %2B | | : | %3A | | `,` | `%2C` | 以下示例显示了不同形式的 **date math index names**(日期数学索引名称)和它们解析的 **final index names**(最终索引名称),它们给定的当前时间是 **2024 **年 **3 **月 **22 **日中午 **utc**。 | 表达式 | 解析结果 | | --- | --- | | **&lt;logstash-{now/d}&gt;** | **logstash-2024.03.22** | | **&lt;logstash-{now/M}&gt;** | **logstash-2024.03.01** | | **&lt;logstash-{now/M{YYYY.MM}}&gt;** | **logstash-2024.03** | | **&lt;logstash-{now/M-1M{YYYY.MM}}&gt;** | **logstash-2024.02** | | **&lt;logstash-{now/d{YYYY.MM.dd&#124;+12:00}}&gt;** | **logstash-2024.03.23** | 为了在索引名称模板的 **static part**(静态部分)中使用字符** {** 和** }** ,使用** \** 对其进行转义,例如 :  ``` <elastic\\{ON\\}-{now/M}> resolves to elastic{ON}-2024.03.01 ``` 下面的示例显示了一个搜索请求,搜索** LogStash** 索引在过去的三天里,假设索引使用默认的 **LogStash** 索引名称格式, **logstash-YYYY.MM.dd** 。 ``` # GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search GET /%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search { "query" : { "match": { "test": "data" } } } ```