通过查询API

时间:2017-08-17 01:26:44

标签: elasticsearch field elasticsearch-5 elasticsearch-painless

ElasticSearch版本= 5.5

我继承了一些代码,通过update by query API将任意文档字段更新为任意值,如下所示:

{
 "query": ...
 "script": {
   "inline": "ctx._source." + field + " = " + value + ";"
   }
 }

由于每分钟最大编译断路器,这偶尔会导致查询失败。

partial update适用于这种情况,但据我所知,部分更新仅在标准更新API中支持,而不是由查询API更新:

{
 "query": ...
 "doc": {
   field: value
   }
 }

Unknown key for a START_OBJECT in [doc]

至少,作为一个ES新手,我认为这个错误意味着什么。在查询API文档的更新中也没有提到doc

假设我确定部分更新无法正常工作,按照scripting guide中的建议参数化脚本似乎是下一步,但似乎没有任何办法访问由参数指定的源字段:

# I wouldn't expect this to work, but tried anyway
{
 "query": ...
 "script": {
   "inline": "ctx._source.params.field = value;",
   "params": {
      "field": field,
      "value": value
      }
   }
 }

"caused_by":{"type":"null_pointer_exception","reason":null}}

尝试通过doc-values访问该字段似乎也不起作用:

{
 "query": ...
 "script": {
   "inline": "doc[params.field] = value;",
   "params": {
      "field": field,
      "value": value
      }
   }
 }

caused_by":{"type":"null_pointer_exception","reason":null}}

有没有办法修复这个脚本,还是需要更广泛的重构?

1 个答案:

答案 0 :(得分:5)

您通过逐个查询API(+ partial updates are not supported)向右,here

你几乎找到了正确的方法来做到这一点,就像这样:

{
 "query": ...
 "script": {
   "inline": "ctx._source[params.field] = params.value;",
   "params": {
      "field": field,
      "value": value
      }
   }
 }

<强>更新 自ES 6起,将inline替换为source