【问题标题】:why script processor works in reindex api and not working on pipeline为什么脚本处理器在 reindex api 中工作而不在管道上工作
【发布时间】:2021-10-02 19:36:49
【问题描述】:

我根据 projectId 创建 idice,如下所示:

//直接调用reindex API就可以了

POST _reindex?wait_for_completion=false
{
  "conflicts": "proceed",
  "source": {
    "index": "xxxxx-rlk-test1-2021-07-22"
  },
  "dest": {
    "index": "xxxxxx",
    "op_type": "create"
  },
  "script": {
    "lang": "painless",
    "source": """
          if (ctx._source.kubernetes != null){
            if (ctx._source.kubernetes.namespace_labels['field_cattle_io/projectId'] != null){
              ctx._index = 'xxxxxx-rlk-'+ (ctx._source.kubernetes.namespace_labels['field_cattle_io/projectId']) + '' + (ctx._index.substring('xxxxxx-rlk-test-'.length(), ctx._index.length()))
            }else {
              ctx._index = 'xxxxxx-rlk-'+ (ctx._source.kubernetes.namespace_labels['field_cattle_io/projectId']) +'-noproject'
            }
          }
      """
  }
}

但是当我想像这样使用管道重新索引时:

PUT _ingest/pipeline/group-by-projectid-pipeline
{
  "description": "this pipeline split indices by pipeline",
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
          if (ctx.kubernetes != null){
            if (ctx.kubernetes.namespace_labels['field_cattle_io/projectId'] != null){
              ctx._index = 'xxxxxx-rlk-'+ (ctx.kubernetes.namespace_labels['field_cattle_io/projectId']) +'' + (ctx._index.substring('xxxxxx-rlk-test-'.length(), ctx._index.length()))
            }else {
              ctx._index = 'xxxxxx-rlk-'+ (ctx.kubernetes.namespace_labels['field_cattle_io/projectId']) +'-noproject'
            }
          }
      """
      }
    }
  ]
}

和:

POST _reindex
{
  "conflicts": "proceed",
  "source": {
    "index": "xxxxxx-rlk-test1-2021-07-22"
  },
  "dest": {
    "index": "xxxxxx",
    "pipeline": "group-by-projectid-pipeline",
    "op_type": "create"
  }
}

然后elasticsearch说(关于(ctx._index.substring('xxxxxx-rlk-test-'.length(), ctx._index.length()))):

"type" : "string_index_out_of_bounds_exception", “原因”:“开始 16,结束 6,长度 6”

提前感谢您的帮助!

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-5 elasticsearch-dsl elasticsearch-opendistro


    【解决方案1】:

    这是因为脚本在两种情况下不会同时执行。

    在重新索引调用期间没有管道,脚本在文档到达目标索引之前执行,因此ctx._index是源索引的名称,即xxxxxx-rlk-test1-2021-07-22,所以你的子字符串通话有效。

    使用管道重新索引调用期间,脚本处理器在文档即将到达目标索引时运行,因此ctx._index 是目标索引的名称,即@987654325 @。

    这是'...'.substring(16, 6) 不起作用的原因。所以你应该在第二种情况下采取不同的方式。

    解决这个问题的简单方法(如果您想保持相同的逻辑)是使用与源索引具有相同长度的虚拟目标索引,无论如何您都应该修改:

    POST _reindex
    {
      "conflicts": "proceed",
      "source": {
        "index": "xxxxxx-rlk-test1-2021-07-22"
      },
      "dest": {
        "index": "xxxxxx-rlk-xxxxx-2021-07-22",        <--- change this
        "pipeline": "group-by-projectid-pipeline",
        "op_type": "create"
      }
    }
    

    【讨论】:

    • 你好 Val,使用管道方法,我想知道有没有办法获取源索引的名称?其中源索引名称具有以下模式:project-*
    • 示例:project-mdb-30-07-2021、project-mdb-29-07-2021、project-xyrz-29-07-2021
    • 让我们看看这个讨论:stackoverflow.com/questions/68593549/…
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2022-07-31
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多