ElasticSearch 批量增長索引

服務端批量增長索引,版本是5.1.1elasticsearch

 

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();

這樣就會把索引寫入。ui

相關文章
相關標籤/搜索