es~批量更新bulkIndex和bulkUpdate

重要说明

  • bulkIndex 批量索引文档更新,文档不存在就建立,存在就覆盖,如果文档原来有3个字段,批量更新时有2个字段,在bulkIndex之后,它最后会变成最新的2个字段
  • bulkUpdate 批量更新文档字段,如果文档原来有3个字段,批量更新时有2个字段,结果还是3个字段

依赖添加

    <properties>
        <fastjson.version>1.2.68</fastjson.version>
        <elasticsearch.high.version>6.5.4</elasticsearch.high.version>
        <elasticsearch.client.version>6.8.7</elasticsearch.client.version>
    </properties>
    <dependencies>
     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.elasticsearch.client</groupId>
          <artifactId>elasticsearch-rest-high-level-client</artifactId>
          <version>${elasticsearch.high.version}</version>
      </dependency>
      <dependency>
          <groupId>org.elasticsearch</groupId>
          <artifactId>elasticsearch</artifactId>
          <version>${elasticsearch.client.version}</version>
      </dependency>
  </dependencies>

bulkIndex实例

    /**
     * 批量替换,不存在就建立,存在就替换,原字段会删除,新字段会添加
     *
     * @throws JsonProcessingException
     */
    @Test
    public void bulkIndex() throws JsonProcessingException {
        List<IndexQuery> queries = new ArrayList<>();

        IndexQuery indexQuery = new IndexQuery();
        indexQuery.setId("552625903956398080");
        Map<String, Object> sourceMap = new HashMap<>();
        sourceMap.put("name", "xx1");
        indexQuery.setSource(new ObjectMapper().writeValueAsString(sourceMap));
        indexQuery.setType("esdto");
        indexQuery.setIndexName("esdto");
        queries.add(indexQuery);

        indexQuery = new IndexQuery();
        indexQuery.setId("552306605039816704");
        sourceMap = new HashMap<>();
        sourceMap.put("name", "xx2");
        indexQuery.setSource(new ObjectMapper().writeValueAsString(sourceMap));
        indexQuery.setType("esdto");
        indexQuery.setIndexName("esdto");
        queries.add(indexQuery);

        elasticsearchTemplate.bulkIndex(queries,
                BulkOptions.builder().withTimeout(TimeValue.MINUS_ONE).build());
    }

bulkUpdate实例

   /**
     * 批量更新某些字段.
     *
     * @throws JsonProcessingException
     */
    @Test
    public void bulkUpdate() throws JsonProcessingException {
        List<UpdateQuery> updateQueries = new ArrayList<>();

        Map<String, Object> sourceMap = new HashMap<>();
        sourceMap.put("name", "xx3");
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.doc(sourceMap);
        UpdateQuery updateQuery = new UpdateQueryBuilder()
                .withId("552629576220545024")
                .withIndexName("esdto")
                .withType("esdto")
                .withUpdateRequest(updateRequest)
                .build();
        updateQueries.add(updateQuery);


        sourceMap = new HashMap<>();
        sourceMap.put("name", "xx4");
        updateRequest = new UpdateRequest();
        updateRequest.doc(sourceMap);
        updateQuery = new UpdateQueryBuilder()
                .withId("552629636035514368")
                .withIndexName("esdto")
                .withType("esdto")
                .withUpdateRequest(updateRequest)
                .build();
        updateQueries.add(updateQuery);

        elasticsearchTemplate.bulkUpdate(updateQueries);
    }

posted @   张占岭  阅读(8042)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2019-06-24 TOTP算法实现二步验证
2016-06-24 Lind.DDD.UoW~方法回调完成原子化操作
2015-06-24 MongoDB学习笔记~管道中的分组实现group+distinct
2013-06-24 品味人生~咖啡与程序员
2012-06-24 说说设计模式~简单工厂模式(Factory)
2012-06-24 说说设计模式~单件模式(Singleton)
2011-06-24 JS函数的原型及对象,对象方法,对象属性的学习
点击右上角即可分享
微信分享提示