ElasticSearch 7.6.x 版本 2020最新版 JavaRest api

戴資料發表於2020-04-12

點個關注吧,球球啦!
持續更新中……

前言

      週末閒來無事,到官網學習(fanyi)了下最新版本的ESJavaRest API。
ES版本:7.6.2
Springboot版本:2.2.6

準備工作及注意事項

1. Maven導包

      Springboot 2.2.6預設幫我們匯入的是ES 6.8.7的包,所以我們需要更改下版本

<elasticsearch.version>7.6.1</elasticsearch.version>

2. 初始化Client

@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient client(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost",9200,"http")
        ));
        return client;
    }
}

索引(index)

增加索引

    //建立索引
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("index1");
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
    }

刪除索引

    //刪除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index1");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        boolean isSuccessful = delete.isAcknowledged();

        System.out.println(isSuccessful);
    }

查詢索引是否存在

    //返回索引是否存在
    @Test
    void existIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("index1");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);
    }

文件(document)

新增文件

    //新增文件
    @Test
    void addDocument() throws IOException {
        User user = new User("dai", 22);
        IndexRequest request = new IndexRequest("index1")
                .id("1")
                .timeout(TimeValue.timeValueSeconds(1));

        IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);

        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }

刪除文件

    //刪除文件
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("index1", "2");
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(deleteResponse.status());
    }

查詢文件是否存在

    //判斷文件是否存在
    @Test
    void isExistDocument() throws IOException {
        GetRequest request = new GetRequest("index1", "1");
        //不獲取_source內容,提升效率
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");

        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

更新文件

    //更新文件
    @Test
    void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("index1", "1");
        request.timeout("1s");

        User user = new User("dai2", 22);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

        System.out.println(updateResponse.status());
    }

資料(datas)

批量插入資料

    //批量插入資料
    @Test
    void BulkRequest() throws IOException {

        BulkRequest bulkRequest = new BulkRequest()
                .timeout("5s");
        List<User> users = Arrays.asList(new User("dai1", 1), new User("dai2", 2), new User("dai3", 3));

        for (User user : users) {
            bulkRequest.add(new IndexRequest("index1")
                    //.id("xxx")
                    .source(JSON.toJSONString(user), XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //是否失敗,false表示成功
        System.out.println(bulkResponse.hasFailures());
        System.out.println(bulkResponse.status());
    }

查詢

    //查詢
    @Test
    void search() throws IOException {
        SearchRequest request = new SearchRequest("index1");

        //構建搜尋條件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // SearchRequest 搜尋請求
        // SearchSourceBuilder 條件構造
        // HighlightBuilder 構建高亮
        // TermQueryBuilder 精確查詢
        // MatchAllQueryBuilder .....
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQueryBuilder)
                .timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
        System.out.println("===================================");
        for (SearchHit documentFields : searchResponse.getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }

    }

點個贊再走,球球啦!

原創不易,白嫖不好,各位的支援和認可,就是我創作的最大動力,我們下篇文章見!

本部落格僅釋出於CSDN—一個帥到不能再帥的人 Mr_kidBK。轉載請標明出處。
https://blog.csdn.net/Mr_kidBK

點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
————————————————
在這裡插入圖片描述

相關文章