elasticsearch批量导入问题

    @Test
    void bulkIndex() throws IOException{
        List<Product> products = new ArrayList<Product>();
        products.add(new Product("香烟",135,1));
//        products.add(new Product("瓜子",154,2));
//        products.add(new Product("矿泉水",613,3));
//        products.add(new Product("酱油",72,4));
//        products.add(new Product("大米",771,5));
        BulkRequest.Builder bk = new BulkRequest.Builder();
        int indexId = 4;
        for (Product product:products) {
            bk.operations(op->op.index(i->i.index("newindexgst")
//                    .id(UUID.randomUUID().toString())
                    .id("1")
                    .document(product)));
        }
        BulkResponse response = clint.configClint().bulk(bk.build());
        if (response.errors()) {
            System.out.println("Bulk had errors");
            for (BulkResponseItem item: response.items()) {
                if (item.error() != null) {
                    System.out.println(item.error().reason());
                }
            }
        }
    }


使用elasticsearch执行java依赖导入没有报错但是发现导入不成功



查看日志发现:[newindexgst][0] primary shard is not active Timeout: [1m], request: [BulkShardRequest [[newindexgst][0]] containing [5] requests]

解决方法:
将elasticsearch移动到一个大的磁盘空间



```java
    @GetMapping("saveUser")
    public ArrayList<User> saveUser() {
        //添加索引mapping索引会自动创建但mapping自只用默认的这会导致分词器不生效 所以这里我们手动导入mapping
        Random random = new Random();
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            User user = new User();
            user.setId(i);
            user.setName(names[random.nextInt(9)]);
            user.setAge(random.nextInt(40) + i);
            user.setInfo(infos[random.nextInt(2)]);
            users.add(user);
        }
        ArrayList<User> users1 = (ArrayList<User>) esUserService.saveAll(users);
        return users1;
    }

elasticreposity插入会成功,但是返回会失败,这个可以选择放弃

你可能感兴趣的:(elasticsearch,jenkins,大数据)