栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch + spring boot 配置

elasticsearch + spring boot 配置

目录

首先引入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;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号