如何将新字段添加到 ElasticSeach 中的嵌套对象中?

sma*_*kov 3 elasticsearch

假设,我有一些预定义模式的索引,例如

     "mappings": {
          "transactions": {
            "dynamic": "strict",
            "properties": {
              
              "someDate": {
                "type": "date"
              },
              "nestedOjects": {
                "type": "nested",
                "properties": {
                  "someField": {
                    "type": "text"
                  }
              }
            }
Run Code Online (Sandbox Code Playgroud)

现在我需要通过将新字段添加到嵌套对象中来更新此映射。即我想要实现这样的目标:

     "mappings": {
          "transactions": {
            "dynamic": "strict",
            "properties": {
              
              "someDate": {
                "type": "date"
              },
              "nestedOjects": {
                "type": "nested",
                "properties": {
                  "someField": {
                    "type": "text"
                  },
                  "newField": {
                    "type": "text"
                  }
              }
            }
Run Code Online (Sandbox Code Playgroud)

Ash*_*inK 5

让我们假设您尚未创建任何索引。首先,我们将使用您现有的映射创建索引,如下所示(使用索引名称demo-index):

创建索引:

PUT demo-index
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "someDate": {
        "type": "date"
      },
      "nestedOjects": {
        "type": "nested",
        "properties": {
          "someField": {
            "type": "text"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要查看上面创建的索引的映射,您可以点击 api:GET demo-index/_mapping

更新现有的以在其中demo-index添加新字段。newFieldnestedOjects

更新索引映射:

PUT demo-index/_mapping
{
  "dynamic": "strict",
  "properties": {
    "someDate": {
      "type": "date"
    },
    "nestedOjects": {
      "type": "nested",
      "properties": {
        "someField": {
          "type": "text"
        },
        "newField": {
          "type": "text"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果您再次点击 api,GET demo-index/_mapping您将获得更新的映射。

如果您需要transactions作为头对象及其内部的其他所有内容,那么您可以执行以下操作(与创建索引时相同):

PUT demo-index/_mapping
{
  "dynamic": "strict",
  "properties": {
    "transactions": {
      "type": "nested",
      "properties": {
        "someDate": {
          "type": "date"
        },
        "nestedOjects": {
          "type": "nested",
          "properties": {
            "someField": {
              "type": "text"
            },
            "newField": {
              "type": "text"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建索引 ref API
  2. 更新映射参考API