Elasticsearch:Simulate index API

1023 篇文章 596 订阅

从现有 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:

PUT /_component_template/ct1                    
{
  "template": {
    "settings": {
      "index.number_of_shards": 2
    }
  }
}

PUT /_component_template/ct2                    
{
  "template": {
    "settings": {
      "index.number_of_replicas": 0
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}

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

PUT /_index_template/final-template             
{
  "index_patterns": ["my-index-*"],
  "composed_of": ["ct1", "ct2"],
  "priority": 5
}

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

PUT my-index-1
GET my-index-1

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

{
  "my-index-1": {
    "aliases": {},
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "2",
        "provided_name": "my-index-1",
        "creation_date": "1659596411728",
        "number_of_replicas": "0",
        "uuid": "O6jrWHb-Rp-5ABA4v8hjdw",
        "version": {
          "created": "8030399"
        }
      }
    }
  }
}

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

POST /_index_template/_simulate_index/my-index-000001

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

{
  "template": {
    "settings": {
      "index": {
        "number_of_shards": "2",
        "number_of_replicas": "0",
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        }
      }
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        }
      }
    },
    "aliases": {}
  },
  "overlapping": []
}

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

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

PUT /_component_template/ct3                    
{
  "template": {
    "settings": {
      "index.number_of_shards": 4
    },
    "mappings": {
      "properties": {
        "text": {
          "type": "text"
        }
      }
    }
  }
}

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

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

PUT /_index_template/final-template             
{
  "index_patterns": ["my-index-*"],
  "composed_of": ["ct1", "ct2", "ct3"],
  "priority": 5
}

那么我们使用如下命令:

POST /_index_template/_simulate_index/my-index-000001

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

{
  "template": {
    "settings": {
      "index": {
        "number_of_shards": "4",
        "number_of_replicas": "0",
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        }
      }
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "text": {
          "type": "text"
        }
      }
    },
    "aliases": {}
  },
  "overlapping": []
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值