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);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章