【问题标题】:Pipeline to delete nested field Elasticsearch mapping删除嵌套字段 Elasticsearch 映射的管道
【发布时间】:2020-02-06 08:13:29
【问题描述】:

有一个类似于下面的 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"
                }
            }
        }
    }
}

Documentation on re-indexing

Documentation on removing a field using the removing fields

顺便说一句,使用 ES 6。

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

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

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

【问题讨论】:

  • 没听懂。您要取消字段中的数据还是“更新”映射?除非您重新索引索引,否则您将无法更新映射。所以,只是想确认你在找什么
  • 当您想从映射中删除字段时,需要重新索引和管道来删除存在的字段和数据。我假设如果该字段是嵌套的,也是可能的,但是管道结构并不那么明显。我将发布一个参考文档来说明。 @AndreyBorisko

标签: elasticsearch


【解决方案1】:

我之前回答过类似的question。用于删除嵌套的逻辑可以移至script processor

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

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

【讨论】:

  • 我实际上是 6,所以不确定这是否重要(我会更新我的帖子),但我尝试使用该确切的逻辑返回错误。将再次测试。
  • 当然,否则你可以使用我提到的脚本处理器。
  • 终于对删除处理器进行了测试,但没有成功。不断收到此错误:field1 不是整数,不能用作路径 nestedfield.field1 的一部分的索引
  • 刚刚意识到你回答的是我的问题哈
  • 解决了这个问题,_source 没有按照我的想法行事,删除它可以让脚本完美运行。如果删除处理器工作会很好,但可能是版本控制或其他东西阻止了这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多