ES 첨삭 검사

5750 단어 ES자바elasticsearch
 @Autowired
    private RestHighLevelClient restHighLevelClient;

    //    
    @Test
    public  void testCreateIndex() throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("jlr");
        IndicesClient indicesClient = restHighLevelClient.indices();
        CreateIndexResponse create = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
    }

    //    
    @Test
    public  void testExistIndex() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest("jlr");
        IndicesClient indicesClient = restHighLevelClient.indices();
        boolean b = indicesClient.exists(getIndexRequest,RequestOptions.DEFAULT);

        System.out.println(b);
    }

    //    
    @Test
    public void testCreateDocument() throws IOException {
        User user = new User("  1",19);
        //    
        IndexRequest indexRequest = new IndexRequest("jlr");
        indexRequest.id("2");
        indexRequest.timeout("1s");//        

        //     json      
        indexRequest.source(JSON.toJSONString(user), XContentType.JSON);

        //    
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(index.toString());
        System.out.println(index.status());

    }

    //    
    @Test
    public void testGetDocument() throws IOException {
        //    
        GetRequest getRequest = new GetRequest("jlr","1");

        //    
        GetResponse document = restHighLevelClient.get(getRequest,RequestOptions.DEFAULT);
        //{"_index":"jlr","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"age":19,"name":"  "}}
        System.out.println(document.toString());

        //    Map{name=  , age=19}      testCreateDocument     
        System.out.println(document.getSourceAsMap());

        //     Key   val
        Object name = document.getSource().get("name");
        System.out.println(name);
    }


    //    
    @Test
    public void testUpdateDocument() throws IOException {
        //    Update  
        UpdateRequest updateRequest = new UpdateRequest("jlr","1");
        updateRequest.timeout("1s");//       1s

        User user = new User("  ",22);
        //          POJO   json        
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);

        //      
        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(update.status());//   ok

    }


    //    
    @Test
    public void testDeleteDocument() throws IOException {
        //      
        DeleteRequest deleteRequest = new DeleteRequest("jlr","11");
        deleteRequest.timeout("1s");//       1s


        //        
        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

        System.out.println(delete.status());//     :  ok    :NOT_FOUND

    }


    //               
    @Test
    public void testBulkRequest() throws IOException {
        //           
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("100s");//       100s

        //      
        List userList = new ArrayList<>();
        userList.add(new User("sb1",1));
        userList.add(new User("sb2",2));
        userList.add(new User("sb3",3));
        userList.add(new User("sb4",4));
        userList.add(new User("sb5",5));
        userList.add(new User("sb6",6));
        userList.add(new User("sb7",7));
        userList.add(new User("sb8",8));


        for (int i=0; i < userList.size(); i++) {
            //   ADD         DeleteRequest ,UpdateRequest       
            bulkRequest.add(new IndexRequest("jlr")
                    .id(""+(i+10))//   id    id   UUID
                    .source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
        }

        //      
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(!bulk.hasFailures());//bulk.hasFailures  false  

    }


    //  
    //SearchSourceBuilder      
    //HighlightBuilder         
    //MatchAllQueryBuilder       
    //TermQueryBuilder         
    @Test
    public void testSearch() throws IOException {
        //           
        SearchRequest serach = new SearchRequest("jlr");

        //      
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        HighlightBuilder highlightBuilder= new HighlightBuilder();
        //searchSourceBuilder.highlighter()//    

        //        QueryBuilder     
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "sb1");//    (  ,    )
        //MatchAllQueryBuilder matchAllQueryBuildermatchAllQueryBuilder = QueryBuilders.matchAllQuery();//    

        //            
        searchSourceBuilder.query(termQueryBuilder);
        serach.source(searchSourceBuilder);
        //  
        SearchResponse search = restHighLevelClient.search(serach, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(search.getHits()));
        SearchHit[] hits = search.getHits().getHits();
        for(SearchHit item: hits) {
            Map sourceAsMap = item.getSourceAsMap();
            System.out.println(sourceAsMap);
        }

    }

좋은 웹페이지 즐겨찾기