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 @ 2022-06-24 14:11  张占岭  阅读(697)  评论(0编辑  收藏  举报