es6.6.1 java客戶端 client基礎操作


1.引入jar包

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.1</version>
</dependency>

2.測試demo

public static void main( String[] args ) throws IOException
    {
        
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                new HttpHost("192.168.249.129", 9200, "http")));
        
//        //創建索引
//        IndexRequest indexRequest = new IndexRequest("product", "car", "1");
//        //索引index(四種json,map,XContentBuilder,object)
//        Map<String, Object> source = new HashMap<String, Object>();
//        source.put("字段1", "值1");
//        source.put("字段2", new Date());
//        source.put("字段3", "值2");
//        indexRequest.source(source);
//        IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);
//        System.out.println(indexResponse.getResult());
//        
        //刪除
        DeleteRequest deleteRequest = new DeleteRequest("product", "car", "1");
        DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);
        System.out.println(deleteResponse.getResult());
        
        //查詢
        GetRequest getRequest = new GetRequest("product", "car", "1");
        GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());
        
//        判斷exist
//        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
//        System.out.println("exists="+exists);
        
        //更新
//        UpdateRequest updateRequest = new UpdateRequest("product", "car", "1");
//          Map<String, Object> source = new HashMap<String, Object>();
//          source.put("字段11", "值1");
//          source.put("字段22", new Date());
//          source.put("字段33", "值2");
//          updateRequest.doc(source);
//        UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
//        System.out.println(updateResponse.getResult());
        
        
        client.close();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM