elasticsearch UpdateByQuery的使用

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

<dependency>
    <groupId>org.elasticsearch.modulegroupId>
    <artifactId>reindexartifactId>
    <version>2.4.3version>
dependency>

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

public static void updateByQuery(String index, String type, SetString, 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());
            }
        }
    }

你可能感兴趣的:(elasticsearch)