SpringBoot集成ES

Maven依赖

 
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

新建一个配置类

ElasticSearchCOnfig.java


@Configurable
public class ElasticsearchConfig {

//    高级的客户端
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")));
    }

}

测试

n {

        GetRequest request = new GetRequest("changan_index","1");

        GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSource());  // 获取文档内容 返回 Map
        System.out.println(documentFields);  // 获取文档所有信息
    }


    /*
    更新文档信息
     */

    @Test
    void updateDocument() throws IOException {

        UpdateRequest updateRequest = new UpdateRequest("changan_index","1");
        updateRequest.timeout("1s");

        // 创建新对象
        User user = new User("ChanganKing",22);

        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);

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

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

        System.out.println(update);


    }


    /*
    删除文档
     */
    @Test
    void deleteDocument() throws IOException {

        DeleteRequest deleteRequest = new DeleteRequest("changan_index","1");
        deleteRequest.timeout("1s");

        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status());

    }


    /*
    特殊的批量插入数据
     */
    @Test
    void bulkInsert() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("1s");

        ArrayList arrayList = new ArrayList<>();

        arrayList.add(new User("hjj",18));
        arrayList.add(new User("cw",21));
        arrayList.add(new User("hhh",23));
        arrayList.add(new User("娇姐",19));
        arrayList.add(new User("哈哈哈",22));
        arrayList.add(new User("King",22));


        for (int i = 0; i < arrayList.size() ; i++) {

            bulkRequest.add(
                    new IndexRequest("changan_index")
                            .id(""+(i+1))
                            .source(JSON.toJSONString(arrayList.get(i)), XContentType.JSON));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

        //是否失败 false 就是成功
        System.out.println(bulk.hasFailures());

    }


//
//    /*
//    查询数据
//     */
    @Test
    void queryData() throws IOException {

        SearchRequest searchRequest = new SearchRequest("changan_index");

        // 构建搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        /*
         * 查询条件, 我们可以使用 QueryBuilders 工具类来实现
         * QueryBuilders.termQuery()  精确查询
         * QueryBuilders.matchAllQuery() 查询所有数据
         */
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "hjj");

        searchSourceBuilder.query(termQueryBuilder);

        // 设置搜索时长
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(search.getHits()));

        System.out.println("===========");


        for (SearchHit searchHit: search.getHits().getHits()) {

            System.out.println(searchHit.getSourceAsMap());
        }

    }

你可能感兴趣的:(springboot,ES,elasticsearch)