服务端批量增加索引,版本是5.1.1

 

TransportClient client;
        Settings esSettings = Settings.builder()
                .put("cluster.name", "elasticsearch") //设置ES实例的名称
                .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
                .build();
        client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
        //此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
        try {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        Map<String,Object> infoMap = new HashMap<String, Object>();
        infoMap.put("name", "广告信息11");

        Map<String,Object> infoMap2 = new HashMap<String, Object>();
        infoMap.put("name", "广告信息22");

        IndexRequest indexRequest = new IndexRequest("index","type","5");
        indexRequest.source(infoMap);
        BulkRequestBuilder bulk = client.prepareBulk();
        bulk.add(indexRequest);

        IndexRequest indexRequest1 = new IndexRequest("index","type","6");
        indexRequest1.source(infoMap2);
        bulk.add(indexRequest1);

        BulkResponse bulkResponse = bulk.execute().actionGet();

这样就会把索引写入。

posted on 2017-10-12 17:13  知己一生  阅读(1120)  评论(0编辑  收藏  举报