码迷,mamicode.com
首页 > 编程语言 > 详细

(19)ElasticSearch java项目中的批量操作mget和bulk

时间:2019-12-23 00:27:49      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:index   err   one   amp   throws   rtc   end   inter   clu   

  1、查询索引是index1,类型是blog,id是8、10和索引是lib3,类型是user,id是1、2、3的文档

@Test
    public void testMultiGet() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build(); 
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //执行批量查询,并返回结果
        MultiGetResponse response = client.prepareMultiGet()
                                   .add("index1","blog","8","10")
                                   .add("lib3","user","1","2","3")
                                   .get();
        //遍历输出结果,输出json字符串
        for(MultiGetItemResponse item:response) {
            GetResponse gr = item.getResponse();
            if(gr != null && gr.isExists()) {
                System.out.println(gr.getSourceAsString());
            }
        }
        client.close();
   }

  2、给索引index1,类型blog批量添加id为3和4的文档

@Test
    public void testBulk() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build(); 
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建文档
        XContentBuilder doc1 = XContentFactory.jsonBuilder()
                 .startObject()
                 .field("id","9")
                 .field("title","工厂模式9")
                 .field("content","静态工厂,实例工厂9。")
                 .field("postdate","2018-05-20")
                 .field("url","csdn.net/79239027")
                 .endObject();
        XContentBuilder doc2 = XContentFactory.jsonBuilder()
                 .startObject()
                 .field("id","29")
                 .field("title","工厂模式29")
                 .field("content","静态工厂,实例工厂29。")
                 .field("postdate","2018-05-20")
                 .field("url","csdn.net/79239027")
                 .endObject();
        //bulk请求
        BulkRequestBuilder bulkBuild = client.prepareBulk();
        //指定创建文档的位置
        bulkBuild.add(client.prepareIndex("index1","blog","3").setSource(doc1));
        bulkBuild.add(client.prepareIndex("index1","blog","4").setSource(doc2));
        //返回结果
        BulkResponse response = bulkBuild.get();
        //如果创建成功输出OK
        System.out.println(response.status());
        if(response.hasFailures()) {
            System.out.println("失败了");
        }
        client.close();
   }

(19)ElasticSearch java项目中的批量操作mget和bulk

标签:index   err   one   amp   throws   rtc   end   inter   clu   

原文地址:https://www.cnblogs.com/javasl/p/12070410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!