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

时间:2019-07-19 10:02:18

标签: elasticsearch

假设,我的索引带有一些预定义的架构,例如

     "mappings": {
          "transactions": {
            "dynamic": "strict",
            "properties": {
              
              "someDate": {
                "type": "date"
              },
              "nestedOjects": {
                "type": "nested",
                "properties": {
                  "someField": {
                    "type": "text"
                  }
              }
            }

现在,我需要通过向嵌套对象添加新字段来更新此映射。即我想实现以下目标:

     "mappings": {
          "transactions": {
            "dynamic": "strict",
            "properties": {
              
              "someDate": {
                "type": "date"
              },
              "nestedOjects": {
                "type": "nested",
                "properties": {
                  "someField": {
                    "type": "text"
                  },
                  "newField": {
                    "type": "text"
                  }
              }
            }

2 个答案:

答案 0 :(得分:3)

让我们考虑您尚未创建任何索引。首先,我们将使用您现有的映射创建索引,如下所示(索引名称使用S0C S1C S0U S1U EC EU OC OU 8512.0 8512.0 0.0 2161.2 68160.0 67668.5 426816.0 249483.9 MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 112464.0 108027.1 14160.0 13222.6 48701 1484.856 124 590.113 2074.968 https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html S0C - Current survivor space 0 - 8MB S1C - Current survivor space 1 - 8MB EU - Current eden space capacity - 68 MB OC - Current old space capacity - 426 MB MC - Metaspace capacity - 112 MB CCSC - Compressed class space capacity - 14 MB --> 636 MB ):

创建索引:

demo-index

要查看上面创建的索引的映射,您可以点击api:PUT demo-index { "mappings": { "dynamic": "strict", "properties": { "someDate": { "type": "date" }, "nestedOjects": { "type": "nested", "properties": { "someField": { "type": "text" } } } } } }

更新现有的GET demo-index/_mapping,在demo-index内添加新字段newField

更新索引映射:

nestedOjects

现在,如果再次点击API PUT demo-index/_mapping { "dynamic": "strict", "properties": { "someDate": { "type": "date" }, "nestedOjects": { "type": "nested", "properties": { "someField": { "type": "text" }, "newField": { "type": "text" } } } } } ,您将获得更新的映射。

如果您需要GET demo-index/_mapping作为头对象及其内部的所有其他对象,则可以执行以下操作(与创建索引时相同):

transactions
  1. 创建索引参考API
  2. 更新映射参考API

答案 1 :(得分:0)

您可以使用以下更新映射 API 更新嵌套对象

PUT  /demo-index/_mapping
{
  "properties": {
    "nestedOjects": {
    "type": "nested",
      "properties": {
        "newField": {
          "type": "text"
        }
      }
    }
  }
}