0

我有以下设置:

映射:

esClient.indices.putMapping({
  index: 'tests',
  body: {
    properties: {
      name: {
        type: 'text',
      },
      lastName: {
        type: 'text',
      },
    },
  },
});

这是我发布条目时的结果:

在此处输入图像描述

这是我查询条目时的结果:

curl -X GET "localhost:9200/tests/_search?pretty" -H 'Content-Type: application/json' -d'       
{
"size": 1000,
"query" : {
    "match_all" : {}
  }
}
'
{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "tests",
        "_type" : "_doc",
        "_id" : "KJbtj3kBRlqnip7VJLLI",
        "_score" : 1.0,
        "_source" : {
          "lastName" : 1,
          "name" : "lucas"
        }
      }
    ]
  }
}

我尝试使用以下 curl 更新条目的姓氏:

curl -X POST "localhost:9200/tests/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.lastName='johnson'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name": "lucas"
    }
  }
}
'

这是我得到的错误:

    {
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "ctx._source.lastName=johnson",
          "                     ^---- HERE"
        ],
        "script" : "ctx._source.lastName=johnson",
        "lang" : "painless",
        "position" : {
          "offset" : 21,
          "start" : 0,
          "end" : 28
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "ctx._source.lastName=johnson",
      "                     ^---- HERE"
    ],
    "script" : "ctx._source.lastName=johnson",
    "lang" : "painless",
    "position" : {
      "offset" : 21,
      "start" : 0,
      "end" : 28
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "cannot resolve symbol [johnson]"
    }
  },
  "status" : 400
}

如果我输入一个整数而不是一个字符串,它会更新它,否则我会不断收到该错误。

非常感谢你的帮助。

4

1 回答 1

0

您需要用 包围新lastName字段值' '

添加一个工作示例

指数数据:

{
    "name":"lucas",
    "lastName":"erla"
}

询问:

POST _update_by_query
{
  "script": {
    "source": "ctx._source.lastName='johnson'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name": "lucas"
    }
  }
}

通过查询API点击更新后,文档将更新为

GET /_doc/1

{
  "_index": "67641538",
  "_type": "_doc",
  "_id": "1",
  "_version": 3,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "lastName": "johnson",
    "name": "lucas"
  }
}
于 2021-05-21T17:42:41.393 回答