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();
       }
  • 相关阅读:
    mac 系统下删除目录的所有.svn文件
    java DES加密解密文件
    也许,未来需要重新规划
    android选择图片或拍照图片上传到服务器(包括上传参数)
    iOS DES ECB模式对称加密解密
    iOS开发中防止键盘挡住UITextField解决方案
    xCode 4.X 免证书真机公布及调试
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    java DES ECB模式对称加密解密
    解决error: failed to launch"/private/var/mobile/Applications/XX" timed out waiting for app to launch
  • 原文地址:https://www.cnblogs.com/javasl/p/12070410.html
Copyright © 2011-2022 走看看