在现有索引中创建新字段 - ElasticSearch

Creating a new field into existing index - ElasticSearch

我想创建一个新字段并将其添加到现有索引,这样我就可以将唯一值发送到该新字段。我希望有一个 API 来执行此操作,而不必在 Kibana 的 CLI 中执行此操作。但是我 运行 进入这个 article 告诉你如何向现有索引添加新字段。

我试图在 _source 字段下添加它,但它不允许我。

PUT customer-simulation-es-app-logs-development-2021-07/_mapping
{
  "_source":{
    "TransactionKey":{
      "type": "keyword"
    }
  }
}

然后我将它添加到 properties 这允许我:

PUT customer-simulation-es-app-logs-development-2021-07/_mapping
{
  "properties":{
    "TransactionKey":{
      "type": "keyword"
    }
  }
}

为了确保它已更新,我 运行 命令 GET customer-simulation-es-app-logs-development-2021-07/_mapping 执行了 return 它。

{
  "customer-simulation-es-app-logs-development-2021-07" : {
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "TransactionKey" : {
          "type" : "keyword"
        },
        "exceptions" : {
          "properties" : {
            "ClassName" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
.....

但是当我转到 Discover 并在字段中输入 T运行sactionKey 时,没有弹出任何内容。是不是我没有将新字段正确添加到现有索引中?

如果您 运行 是 7.11 之前的版本,则需要转至堆栈管理 > 索引模式并刷新索引模式,然后才能在“发现”视图中看到新字段。每次索引映射更改时都需要执行此操作。

自 7.11 起,索引模式在需要时 refreshed automatically