elasticsearch UpdateByQuery的使用

来源:互联网 发布:isis软件下载 编辑:程序博客网 时间:2024/05/21 15:02

上一篇发了解决elasticsearch UpdateByQuery的问题,后来遇到好多小伙伴问我,为什么他使用不了UpdateByQuery,遂科普下。
在于你可能没有引入reindex包:

<dependency>    <groupId>org.elasticsearch.module</groupId>    <artifactId>reindex</artifactId>    <version>2.4.3</version></dependency>

使用方法也很简单,举个栗子,这个栗子的作用是把所有满足条件的session_id记录都使用对应script更新:

public static void updateByQuery(String index, String type, Set<Map<String, Object>> docs, String scriptStr) {        if (docs == null || docs.isEmpty()){            LOG.info("No data can updateByQuery to es! index:{}.", index);            return;        }        UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);        for (Map<String, Object> doc : docs) {            if (doc==null || doc.isEmpty()){                return;            }            Script script = new Script(scriptStr);            BulkIndexByScrollResponse scrollResponse =                    ubqrb.source(index)                            .script(script)                            .filter(QueryBuilders.termQuery("session_id", doc.get("orgin_session_id")))                            .abortOnVersionConflict(false).get();            for (BulkItemResponse.Failure failure : scrollResponse.getIndexingFailures()) {                LOG.error(failure.getMessage());            }        }    }
原创粉丝点击