首页 > 解决方案 > 删除嵌套字段 Elasticsearch 映射的管道

问题描述

有一个类似于下面的 Elasticsearch 映射,我正在尝试使用重新索引 API 更新它。我已经学会了如何使用管道来执行各种操作,例如删除字段或更改类型,但是没有从嵌套类型中删除字段。例如,在descriptions现场我将如何设置管道来删除badfield

{
    "mappings": {
        "all": {
            "_all": {
                "enabled": false
            },
            "dynamic": "strict",
            "properties": {
                "address": {
                    "type": "text"
                },
                "businessName": {
                    "type": "text"
                },
                "descriptions": {
                    "type": "nested",
                    "properties": {
                        "dateSeen": {
                            "type": "date",
                            "format": "date_time"
                        },
                        "source": {
                            "type": "text",
                        },
                        "value": {
                            "type": "text"
                        },
                        "badfield": {
                            "type": "text"
                        }
                    }
                },
                "dateAdded": {
                    "type": "date",
                    "format": "date_time||date_time_no_millis"
                }
            }
        }
    }
}

关于重新索引的文档

有关使用删除字段删除字段的文档

顺便说一句,使用 ES 6。

我根据评论设置了一个处理器脚本,并遇到了该字段为空的问题,即使它明显存在。

{
    "processors": [{
            "script": {
                "source": """
                if (ctx._source.descriptions != null) { 
                    for(item in ctx._source.descriptions) { 
                         item.remove('badfield'); 
                    } 
                }
                """
            }
        }
    ]
}

编辑:_source从脚本中删除是问题,这意味着我不完全了解它的用法,但能够创建一个嵌套字段删除脚本。

标签: elasticsearch

解决方案


我之前回答过类似的问题。用于删除嵌套的逻辑可以移动到脚本处理器

也许文档需要澄清嵌套用例,但如果你只是试一试,它会起作用(在 ES 7.5 上测试)如果你想更深入地了解会发生什么,我想你需要检查源代码

PUT src
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name": {
        "type": "keyword"
      },
      "nestedField": {
        "type": "nested",
        "properties": {
          "field1": {
            "type": "boolean"
          },
          "field2": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

PUT dst
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name": {
        "type": "keyword"
      },
      "nestedField": {
        "type": "nested",
        "properties": {
          "field2": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

POST src/_doc
{
  "name": "name1",
  "nestedField": {
    "field1": true,
    "field2": false
  }
}

POST src/_doc
{
  "name": "name2",
  "nestedField": {
    "field1": false,
    "field2": true
  }
}

GET src
GET src/_search
GET dst/_search
GET dst

PUT _ingest/pipeline/test_pipeline
{
  "processors": [
    {
      "remove": {
        "field": "nestedField.field1"
      }
    }
  ]
}

POST _reindex
{
  "source": {
    "index": "src"
  },
  "dest": {
    "index": "dst",
    "pipeline": "test_pipeline"
  }
}

GET dst/_search
GET dst

#DELETE src
#DELETE dst

推荐阅读