简体   繁体   English

如何批量更新多个id的弹性搜索?

[英]How to update elastic search in batches for multiple id?

I have a requirement where I need to update a field in elastic search for multiple ids.我有一个要求,我需要在弹性搜索中更新一个字段以获取多个 ID。 Currently I am using XcontentBuilder and passing an Id along with field name but it's a for loop that's why time complexity becomes horrible if I pass multiple Ids.目前我正在使用 XcontentBuilder 并传递一个 Id 和字段名称,但它是一个 for 循环,这就是为什么如果我传递多个 Id 时间复杂度变得可怕。 Is there a way where I can do same operation in batches?有没有办法可以批量执行相同的操作?

My Code is like this:我的代码是这样的:

UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("_doc");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
        .startObject()
            .field("gender", "male")
        .endObject());
client.update(updateRequest).get();

Id is a dynamic field and for each Id I am running a loop using above code. Id 是一个动态字段,对于每个 Id,我使用上面的代码运行一个循环。

I have not tested this but you can check If this can be of any help我没有测试过这个,但你可以检查这是否有任何帮助

Option 1:选项1:

    List<String> ids = new ArrayList<>();
    ids.add("1");
    ids.add("2");
    for (String id : ids) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("index");
        updateRequest.type("_doc");
        updateRequest.id("1");
        updateRequest.doc(jsonBuilder().startObject().field("gender", "male").endObject());
        bulkRequest.add(updateRequest);
    }
    BulkResponse bulkResponse = bulkRequest.execute().actionGet();

Option 2: Using Script选项 2:使用脚本

    List<String> ids = new ArrayList<>();
    ids.add("1");
    ids.add("2");
    Map<String, Object> params = new HashMap<>();
    String scriptCode = "ctx._source.gender=params.gender";
    params.put("gender", "male");
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for(String id : ids) {
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("index", "type", id)
                .setScript(new Script(ScriptType.INLINE, "painless", scriptCode, params));
        bulkRequest.add(updateRequestBuilder);
    }
    BulkResponse bulkResponse = bulkRequest.execute().actionGet();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring 引导中更新弹性搜索而不更改弹性服务器 - How to update elastic search in spring boot without changing the elastic server 如何在弹性搜索中创建具有相同ID的不同文档 - How to create different documents in elastic search but same ID 使用Java API进行弹性搜索6.2,如何具有自动生成的ID? - Elastic search 6.2 with java API, how to have auto generated ID? 如何使用Java客户端更新Elastic Search上的条目 - How to update an entry on Elastic Search using Java client 弹性搜索:根据源中的_field获取所有文档ID,并使用新数据更新_field - Elastic Search: To get all the document id depending on _field in source and update the _field with new data 如何使用弹性搜索Java API编写多个得分函数? - How to use elastic search java API to write multiple score functions? 如何在java中的弹性搜索Querybuilder中处理多个“和”“或”运算符 - How to handle multiple 'and' 'or' operators in Elastic search Querybuilder in java 如何在弹性搜索中为多个 boolean 查询生成查询 - How can I generate Query in elastic search for multiple boolean queries elasticsearch集群如何连接多个master节点 - How to connect to multiple master nodes of elastic search cluster 如何配置弹性搜索 - How to configuration Elastic Search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM