日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Springboot整合Elasticsearch及相關API

作者:Y4Snail 更新時間: 2022-08-15 編程語言

文章目錄

  • Springboot整合Elasticsearch
    • 自定義配置類引入es高級客戶端
  • 索引操作API
  • 文檔操作API

Springboot整合Elasticsearch

Springboot整合Elasticsearch只需要引入完成以下兩步操作

  • 引入依賴:spring-boot-starter-data-elasticsearch
  • 在屬性中修改<elasticsearch.version>標簽,將版本改為與elasticsearch服務器相同的版本即可
  • 自定義配置類引入es高級客戶端

自定義配置類引入es高級客戶端

@Configuration
public class ElasticSearchClientConfig {

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

}

索引操作API

@SpringBootTest
class EsIndexTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 1、es測試創(chuàng)建索引
     * */
    @Test
    void testCreateIndex() throws Exception {
        // 1.創(chuàng)建索引請求
        CreateIndexRequest request = new CreateIndexRequest("yang_index");
        // 2.客戶端執(zhí)行請求
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    /**
     * 2、es測試獲取索引
     * */
    @Test
    void testExistIndex() throws Exception {
        // 1.獲取索引請求
        GetIndexRequest request = new GetIndexRequest("yang_index");
        // 2.客戶端執(zhí)行請求
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    /**
     * 3、es測試刪除索引
     * */
    @Test
    void testDeleteIndex() throws Exception {
        DeleteIndexRequest request = new DeleteIndexRequest("yang_index");
        AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }

}

文檔操作API

@SpringBootTest
public class EsDocumentTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 1、es測試添加文檔
     * */
    @Test
    void testAddDocument() throws Exception {
        User user = new User("yang", 15);
        IndexRequest request = new IndexRequest("yang_index");
        request.id("2");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.source(JSON.toJSONString(user), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    /**
     * 2、es測試批量添加文檔
     * */
    @Test
    void testAddBatchDocument() throws Exception {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(TimeValue.timeValueSeconds(10));

        ArrayList<User> users = new ArrayList<>();
        users.add(new User("yang1", 21));
        users.add(new User("yang2", 22));
        users.add(new User("yang3", 23));
        users.add(new User("yang4", 24));
        users.add(new User("yang5", 25));

        for (int i = 0; i < users.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("yang_index")
                    .id("" + (i+1))
                    .source(JSON.toJSONString(users.get(i)), XContentType.JSON)
            );
        }
        BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.hasFailures());
    }

    /**
     * 3、es測試文檔是否存在
     * */
    @Test
    void testDocumentIsExists() throws Exception {
        GetRequest getRequest = new GetRequest("yang_index", "1");
        boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    /**
     * 4、es測試獲取文檔信息
     * */
    @Test
    void testGetDocument() throws Exception {
        GetRequest getRequest = new GetRequest("yang_index", "1");
        GetResponse document = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        String source = document.getSourceAsString();
        System.out.println(source);
    }

    /**
     * 5、es測試更新文檔信息
     * */
    @Test
    void testUpdateDocument() throws Exception {
        UpdateRequest updateRequest = new UpdateRequest("yang_index", "1");
        updateRequest.timeout(TimeValue.timeValueSeconds(1));
        User user = new User();
        user.setName("yang_111");
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    /**
     * 6、es測試刪除文檔
     * */
    @Test
    void testDeleteDocument() throws Exception {
        DeleteRequest request = new DeleteRequest("yang_index","1");
        request.timeout(TimeValue.timeValueSeconds(1));
        DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    /**
     * 7、es測試查詢文檔
     *      SearchRequest           搜索請求
     *      SearchSourceBuilder     條件構造
     *      HighlightBuilder        構建高亮
     *      TermQueryBuilder        精確查詢
     *      xxxQueryBuilder         表示對應的命令
     * */
    @Test
    void testQueryDocument() throws Exception {
        SearchRequest searchRequest = new SearchRequest("yang_index");
        //構建搜索條件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查詢條件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "yang");
        //組合
        sourceBuilder.query(termQueryBuilder)
                .timeout(TimeValue.timeValueSeconds(100));
        //封裝進searchRequest中
        searchRequest.source(sourceBuilder);
        //通過客戶端發(fā)送并得到響應
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

原文鏈接:https://blog.csdn.net/koutaoran4812/article/details/126336755

欄目分類
最近更新