按查询弹性搜索更新 [英] Elasticsearch Update by Query

查看:258
本文介绍了按查询弹性搜索更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据ES版本2.3.4的搜索查询更新多个文档。我的用例是搜索两个字段匹配某些值的文档,然后添加一个具有一定值的新字段。所以让我们来搜索所有员工,名字叫John,姓氏Smith,并在其配置文件中添加一个新的工作字段,其值为Engineer。
所以我的第一个问题是是否可以使用update_by_query API的doc选项(与更新API相同的方式)。



如果没有,脚本必须被使用(这是我现在的方式),那么也许有人可以帮助我摆脱以下错误:

  {error:{root_cause:[{type:class_cast_exception,reason:java.lang.String不能转换为java.util.Map} ],type:class_cast_exception,reason:java.lang.String不能转换为java.util.Map},status:500} 
/ pre>

我使用的代码如下:

  curl -XPOST -s'http:// localhost:9200 / test_index / _update_by_query?conflict = proceed'-d'
{
query:{
filtered:{
查询:{
match_all:{}
},
过滤器:{
bool:{
必须 [
{
match:{first_name:John}
},
{
match:{last_name:Smith }
}
]
}
}
}
},
script:ctx._source.job = \Engineer \
$'

发送相同的查询(没有脚本字段)使用count API

解决方案

正确的语法是这样的:

 script:{
inline:ctx._source.job = \Engineer \
}


I am trying to update several documents based on a search query with ES version 2.3.4. My use case is to search for documents where two fields match certain values and then add a new field with a certain value. So let's say I want to search all employees with first name "John" and last name "Smith" and add a new field "job" to their profiles with the value "Engineer" in it. So my first question is whether it is possible to do this using the "doc" option with the update_by_query API (the same way like with the update API).

If not, and script must be used (which is the way I'm doing it now) then maybe somebody can help me getting rid of the following error:

{"error":{"root_cause":[{"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"}],"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"},"status":500}

The code I'm using looks as follows:

curl -XPOST -s 'http://localhost:9200/test_index/_update_by_query?conflicts=proceed' -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"match": { "first_name" : "John" }
},
{
"match": { "last_name" : "Smith" }
}
]
}
}
}
},
"script" : "ctx._source.job = \"Engineer\""
}'

When sending the same query (without the "script" field) using the count API no error is reported and the correct number of documents is retuned.

解决方案

The correct syntax is this:

  "script": {
    "inline": "ctx._source.job = \"Engineer\""
  }

这篇关于按查询弹性搜索更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆