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

學無先后,達者為師

網站首頁 編程語言 正文

elasticsearch + spring boot 配置

作者:山聞愚 更新時間: 2022-04-10 編程語言

目錄

首先引入elasticsearch依賴

操作?elasticsearch有兩種方式

一、通過elasticsearch?client操作elasticsearch

二、通過spring框架操作elasticsearch


首先引入elasticsearch依賴


    org.elasticsearch
    elasticsearch
    7.6.2

操作?elasticsearch有兩種方式

一、通過elasticsearch?client操作elasticsearch



    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.6.2



    org.elasticsearch.plugin
    transport-netty4-client
    7.6.2

這種方式使用起來比較復雜,需要對API比較了解。適合需要定制化的需求。如果是簡單的增刪除改查,還是用第二種方式用spring框架二次封裝,以ORM的方式對數據進行操作可能更習慣使用。

//定義client鏈接
RestHighLevelClient client = new RestHighLevelClient(
		RestClient.builder(
				new HttpHost("localhost", 9200, "http"),
				new HttpHost("localhost", 9201, "http")));

// 然后用client進行增刪改查

二、通過spring框架操作elasticsearch

引入依賴



	org.springframework.data
	spring-data-elasticsearch
	4.0.9.RELEASE



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

?application.yml配置

#elasticsearch服務器連接配置
spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: localhost:9300

?配置好之后通過繼承org.springframework.data.elasticsearch.repository.ElasticsearchRepository或使用ElasticsearchRestTemplate操作elasticsearch。

@Component
public interface IndexRepository extends ElasticsearchRepository {

}

或

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

原文鏈接:https://blog.csdn.net/xjj1314/article/details/123788540

欄目分類
最近更新