ElasticsearchTemplate 批量插入

public void bulkIndex() throws Exception {
        int counter = 0;
        try {
            List queries = new ArrayList();

            String sql = "xxxx";

            List list = jdbcTemplate.query(sql, (rs, rowNum) -> {
                Model model = new Model
                return model ;
            });


            for (Model item : list) {
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setId(item.getPaymentReportId().toString());
                indexQuery.setSource(JSON.toJSONString(item));
                indexQuery.setIndexName("new_report_system");
                indexQuery.setType("payment_report");
                queries.add(indexQuery);
                //分批提交索引
                if (counter != 0 && counter % 1000 == 0) {
                    elasticsearchTemplate.bulkIndex(queries);
                    queries.clear();
                    System.out.println("bulkIndex counter : " + counter);
                }
                counter++;
            }
            //不足批的索引最后不要忘记提交
            if (queries.size() > 0) {
                elasticsearchTemplate.bulkIndex(queries);
            }
            elasticsearchTemplate.refresh("new_report_system");
            System.out.println("bulkIndex completed.");
        } catch (Exception e) {
            System.out.println("IndexerService.bulkIndex e;" + e.getMessage());
            throw e;
        }
    }

 

你可能感兴趣的:(JavaWeb和框架运用)