Elasticsearch JS - 未定义变量 [x]

Elasticsearch JS - Variable [x] is not defined

在测试 Elasticsearch 索引中,我已经索引了一个文档,现在我想通过将其 length 属性 设置为 100 来更新文档。我想通过 elasticsearch 包通过脚本(因为这是一个简化的例子来说明我的问题)来做到这一点。

client.update({
  index: 'test',
  type: 'object',
  id: '1',
  body: {
    script: 'ctx._source.length = length',
    params: { length: 100 }
  }
})

但是,我收到以下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[6pAE96Q][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "ctx._source.length = length",
        "                     ^---- HERE"
      ],
      "script": "ctx._source.length = length",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [length]is not defined."
      }
    }
  },
  "status": 400
}

即使我在 body.params.length.

中包含了 length 属性,也会发生这种情况

使用以下内容:

我该如何解决这个问题?

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

处的文档有误

在他们的例子中,他们输入:

client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    script: 'ctx._source.tags += tag',
    params: { tag: 'some new tag' }
  }
}, function (error, response) {
  // ...
});

而实际上,body.script 应该是:

client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    script: {
      lang: 'painless',
      source: 'ctx._source.tags += params.tag',
      params: { tag: 'some new tag' }
    }
  }
}, function (error, response) {
  // ...
});


因此,如果您将脚本更改为:

script: {
  lang: 'painless',
  source: 'ctx._source.length = params.length',
  params: { length: 100 }
}

应该可以!


您可能需要参考 Painless Examples - Updating Fields with Painless 页面!