zoukankan      html  css  js  c++  java
  • (19)ElasticSearch java项目中的批量操作mget和bulk

      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();
       }
  • 相关阅读:
    Call KernelIoControl in user space in WINCE6.0
    HOW TO:手工删除OCS在AD中的池和其他属性
    关于新版Windows Server 2003 Administration Tools Pack
    关于SQL2008更新一则
    微软发布3款SQL INJECTION攻击检测工具
    HyperV RTM!
    OCS 2007 聊天记录查看工具 OCSMessage
    CoreConfigurator 图形化的 Server Core 配置管理工具
    OC 2007 ADM 管理模板和Live Meeting 2007 ADM 管理模板发布
    Office Communications Server 2007 R2 即将发布
  • 原文地址:https://www.cnblogs.com/javasl/p/12070410.html
Copyright © 2011-2022 走看看