Elasticsearch:Simulate index API

1,586 阅读3分钟

从现有 index template 返回将应用于指定索引的索引配置。如果你对 index template 还没有什么理解的话,请阅读我之前的文章 “Elasticsearch:可组合的 Index templates - 7.8 版本之后”。它的使用例子:

POST /_index_template/_simulate_index/my-index-000001

警告:此功能处于技术预览阶段,可能会在未来版本中更改或删除。 Elastic 将尽最大努力解决任何问题,但技术预览版中的功能不受官方 GA 功能的支持 SLA 约束。

前提条件:如果启用了 Elasticsearch 安全功能,你必须拥有 manage_index_templates 或 manage cluster 权限才能使用此 API。

在下面,我将在 Elastic Stack 8.3.3 的安装中来进行展示。

例子

我们首先来创建两个component templates:

`

1.  PUT /_component_template/ct1                    
2.  {
3.    "template": {
4.      "settings": {
5.        "index.number_of_shards": 2
6.      }
7.    }
8.  }

10.  PUT /_component_template/ct2                    
11.  {
12.    "template": {
13.      "settings": {
14.        "index.number_of_replicas": 0
15.      },
16.      "mappings": {
17.        "properties": {
18.          "@timestamp": {
19.            "type": "date"
20.          }
21.        }
22.      }
23.    }
24.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

在上面,我们创建了两个分别称作 ct1 及 ct2 的 component template。我们接下来使用如下的命令把上面的两个 component template 组合为一个 index template:



1.  PUT /_index_template/final-template             
2.  {
3.    "index_patterns": ["my-index-*"],
4.    "composed_of": ["ct1", "ct2"],
5.    "priority": 5
6.  }


在上面,我们创建了 final-template。它定义了一个 index_patterns 为 my-index-*,也就是任何以 my-index- 为开头的索引的 settings,alias 及 mappings 由这个 template 里的 component template 来决定。在之前,我们可以通过这样的方法来验证我们的 template 是否正确:



1.  PUT my-index-1
2.  GET my-index-1


上面的最后一个命令显示的结果为:



1.  {
2.    "my-index-1": {
3.      "aliases": {},
4.      "mappings": {
5.        "properties": {
6.          "@timestamp": {
7.            "type": "date"
8.          }
9.        }
10.      },
11.      "settings": {
12.        "index": {
13.          "routing": {
14.            "allocation": {
15.              "include": {
16.                "_tier_preference": "data_content"
17.              }
18.            }
19.          },
20.          "number_of_shards": "2",
21.          "provided_name": "my-index-1",
22.          "creation_date": "1659596411728",
23.          "number_of_replicas": "0",
24.          "uuid": "O6jrWHb-Rp-5ABA4v8hjdw",
25.          "version": {
26.            "created": "8030399"
27.          }
28.        }
29.      }
30.    }
31.  }


很显然,上面的结果显示了我们想要的结果,但是我们也看到它含有一些其它我们并不想要的信息。我们在创建 template 时,只是想验证最终的 template 是否正确。在实际的很多例子中,我们可能会有很多的 component template,它们的设置可能还会有重叠,它们的 priority 也会有不同,那么最终的索引的设置是出自哪一个呢?我们其实不需要创建一个索引来验证,相反,我们只需要使用如下的命令来查看:

POST /_index_template/_simulate_index/my-index-000001

上面的命令返回的结果如下:

`

1.  {
2.    "template": {
3.      "settings": {
4.        "index": {
5.          "number_of_shards": "2",
6.          "number_of_replicas": "0",
7.          "routing": {
8.            "allocation": {
9.              "include": {
10.                "_tier_preference": "data_content"
11.              }
12.            }
13.          }
14.        }
15.      },
16.      "mappings": {
17.        "properties": {
18.          "@timestamp": {
19.            "type": "date"
20.          }
21.        }
22.      },
23.      "aliases": {}
24.    },
25.    "overlapping": []
26.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

它很清晰地表明了我们最终的 my-index-000001 的定义。

假如我们增加一个新的 component template 如下:

`

1.  PUT /_component_template/ct3                    
2.  {
3.    "template": {
4.      "settings": {
5.        "index.number_of_shards": 4
6.      },
7.      "mappings": {
8.        "properties": {
9.          "text": {
10.            "type": "text"
11.          }
12.        }
13.      }
14.    }
15.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

请注意:在上面,我们定义的 index.number_of_shards 和 ct1 中的有重复,并且值是不同的。 我们也添加了一个新的字段 text。

我们同时也修改之前的 index template 的定义如下:



1.  PUT /_index_template/final-template             
2.  {
3.    "index_patterns": ["my-index-*"],
4.    "composed_of": ["ct1", "ct2", "ct3"],
5.    "priority": 5
6.  }


那么我们使用如下命令:

POST /_index_template/_simulate_index/my-index-000001

而得到的最终的 my-index-000001 的定义如下:

`

1.  {
2.    "template": {
3.      "settings": {
4.        "index": {
5.          "number_of_shards": "4",
6.          "number_of_replicas": "0",
7.          "routing": {
8.            "allocation": {
9.              "include": {
10.                "_tier_preference": "data_content"
11.              }
12.            }
13.          }
14.        }
15.      },
16.      "mappings": {
17.        "properties": {
18.          "@timestamp": {
19.            "type": "date"
20.          },
21.          "text": {
22.            "type": "text"
23.          }
24.        }
25.      },
26.      "aliases": {}
27.    },
28.    "overlapping": []
29.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

显然,在我们没有创建 my-index-000001 的情况下,我们模拟测试了它最终的索引定义。