elasticsearch jest client 注意事项

 public void indexSampleArticles() {

        Article article1 = new Article();
        article1.setId(10L);
        article1.setAuthor("Robert Anthony Salvatore2222222222222");
        article1.setContent("Homeland follows the story of Drizzt from around the time and circumstances of his birth and his upbringing amongst the drow (dark elves). " +
                "The book takes the reader into Menzoberranzan, the drow home city. From here, the reader follows Drizzt on his quest to follow his principles in a land where such " +
                "feelings are threatened by all his family including his mother Matron Malice. In an essence, the book introduces Drizzt Do'Urden," +
                " one of Salvatore's more famous characters from the Icewind Dale Trilogy.");

        Article article2 = new Article();
        article2.setId(3L);
        article2.setAuthor("John Ronald Reuel Tolkien");
        article2.setContent("The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. " +
                "The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. " +
                "It was written in stages between 1937 and 1949, much of it during World War II.[1] It is the third best-selling novel ever written, with over 150 million copies sold");

        try {
            // Delete articles index if it is exists
            //DeleteIndex deleteIndex = new DeleteIndex.Builder("articles").build();
            //jestClient.execute(deleteIndex);

            IndicesExists indicesExists = new IndicesExists.Builder("articles").build();
            JestResult result = jestClient.execute(indicesExists);

            if (!result.isSucceeded()) {
                // Create articles index
                CreateIndex createIndex = new CreateIndex.Builder("articles").build();
                jestClient.execute(createIndex);
            }

            /**
             *  if you don't want to use bulk api use below code in a loop.
             *
             *  Index index = new Index.Builder(source).index("articles").type("article").build();
             *  jestClient.execute(index);
             *
             */

            Bulk bulk = new Bulk.Builder()
                    .addAction(new Index.Builder(article1).index("articles").type("article").build())
                    .addAction(new Index.Builder(article2).index("articles").type("article").build())
                    .build();

            result = jestClient.execute(bulk);

            System.out.println(result.getJsonString());

        } catch (IOException e) {
            logger.error("Indexing error", e);
        } catch (Exception e) {
            logger.error("Indexing error", e);
        }

    }

   当重复id插入时,会做更新操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值