I use Elasticsearch 6.8.12, i need to add a update by query API in Java, i check docs in ES and know ScriptQueryBuilder can build script but i don't know how to use it, i searched in google and couldn't found any usable tutorial.
Share what i know currently:
- we can use
_update_by_queryto update like Mysqlset extra= test where user='kimchy', refer to es update by query
POST twitter/_update_by_query
{
"script": {
"source": "ctx._source.['extra'] = 'test'",
"lang": "painless"
},
"query": {
"term": {
"user": "kimchy"
}
}
}
ScriptQueryBuildercan generate script query json, dependency:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.12</version>
</dependency>
I tried below code but it run failed:
public String generateUpdateQuery() {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
Map<String, Object> params = new HashMap<>(16);
params.put("user","kimchy");
Script script = new Script(Script.DEFAULT_SCRIPT_TYPE, null, null, params);
ScriptQueryBuilder scriptQueryBuilder = QueryBuilders.scriptQuery(script);
System.out.println(scriptQueryBuilder.toString());
return null;
}
My requirement:
I need to generate script json using ScriptQueryBuilder QueryBuilders, the ES API is _update_by query
Thanks!