SpringBoot整合Elasticsearch框架

新建SpringBoot项目:

修改pom.xml文件,引入spring-boot-data-elasticsearch Jar 包:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
   <version>2.2.2.RELEASE</version>
</dependency>

修改application.yml文件,引入elasticsearch配置:

spring:
  data:
    elasticsearch:
      ##集群名称,elasticsearch.yml的cluster.name: chenxi配置
      ##详情见 http://chenxitag.elasticsearch.cluster.com
      cluster-name: chenxi
      ##集群地址逗号分隔,注意此地方用的端口为9300,Es集群TCP协议端口
      cluster-nodes: 192.168.0.1:9300,192.168.0.2:9300

新建测试Entity:

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "chenxi", type = "user"/*shards = 1, replicas = 2 ##可指定分片数和副本数*/)
@Data
public class UserEntity {
    @Id
    private Integer id;
    private String name;
    private Integer age;
}

新建测试Dao:

import com.es.entity.UserEntity;
import org.springframework.data.repository.CrudRepository;

public interface UserDao extends CrudRepository<UserEntity, Integer> {
}

新建Test类测试:

import com.es.dao.UserDao;
import com.es.entity.UserEntity;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@SpringBootTest
@Slf4j
class EsTestApplicationTests {

   @Test
   void contextLoads() {
   }

   @Autowired
   private UserDao userDao;

   @Autowired
   private ElasticsearchTemplate esTemplate;

   @Test
   void esSave(){

      UserEntity esEntity = new UserEntity();
      esEntity.setId(1);
      esEntity.setName("chenxi");
      esEntity.setAge(22);

      userDao.save(esEntity);
   }

   @Test
   void esFind(){
      log.info(new Gson().toJson(userDao.findById(1)));
      //info out "{"value":{"id":1,"name":"chenxi","age":22}}"
   }

   @Test
   void esTemplate(){
      log.info(String.valueOf(esTemplate.createIndex("template_index")));
      //info out "true"
   }

错误信息:NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{P20ipqqfSNCzjirh0puSTQ}{192.168.0.1}{192.168.0.1:9300}, {#transport#-2}{I4slCrNuTbmVTOSZ3DG7hA}{192.168.0.2}{192.168.0.2:9300}]

请检查Es是否启动,以及Es环境elasticsearch.yml集群配置项:

# ———————————- Cluster ———————————–

#

# Use a descriptive name for your cluster:

# 节点集群名称,保证三台服务器节点集群名称相同

cluster.name: chenxi

原文地址:https://www.cnblogs.com/chenxitag/p/12320902.html

时间: 2024-07-31 00:16:32

SpringBoot整合Elasticsearch框架的相关文章

SpringBoot整合ElasticSearch实现多版本的兼容

前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和SpringBoot进行结合使用. ElasticSearch介绍 ElasticSearch是一个基于Lucene的搜索服务器,其实就是对Lucene进行封装,提供了 REST API 的操作接口 ElasticSearch作为一个高度可拓展的开源全文搜索和分析引擎,可用于快速地对大数据进行存储,搜索和分析.

Spring Boot 整合 ElasticSearch 框架

在应用中经常会有检索的功能——查询数据库中包含关键字的数据,如果采用查数据库(like 关键字)的方式,效率会非常低.为了解决这个问题,我们引入了 ElasticSearch 框架. ElasticSearch 下载安装,请参考博客:https://blog.csdn.net/yjclsx/article/details/81302041 接下来,我们使用 Spring Data Elasticsearch Repositories 集成 ElasticSearch 步骤1:引入映射 <depe

SpringBoot整合elasticsearch(三)

Docker安装elasticsearch 启动注意2点,1是内存,2是线程数(此处进行简单安装,后面会详细补充es文档) 1 [[email protected] ~]# docker images 2 REPOSITORY TAG IMAGE ID CREATED SIZE 3 elasticsearch latest 874179f19603 12 days ago 771 MB 4 springbootdemo4docker latest cd13bc7f56a0 2 weeks ago

6.3 Springboot整合Mybatis框架

一.整合Mybatis框架 1.步骤: 在整合Druid的基础上继续(略) pom.xml中添加mybatis依赖 编写实体类 编写mapper层(dao层) 编写mapper映射文件 在application.yml文件中添加配置 编写控制器 2.pom.xml中添加mybatis依赖 <!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot 的,而不是Spring Boot自己的--> <dependency> <groupId>o

【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

一.Linux下安装ElasticSearch 1.检测是否安装了Elasticsearch 1 ps aux |grep elasticsearch 2.安装JDK 3.下载Elasticsearch 1 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.tar.gz 解压Elasticsearch tar -zxvf elasticsearch-6.0.0.tar.gz 移动Elasti

springboot 整合 elasticsearch

1引入jar包 <!--elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> 2实体类 package com.miracle.config.elasticsearch.ent

(六)SpringBoot整合Swagger2框架

一:什么是Swagger Swagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务. 二:添加Swagger2依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency>

springboot整合elasticsearch遇到的错误

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.2018-12-29 11:54:39.572 ERROR 7563 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.

spring-boot整合SSM框架

1.整合spring-MVC拦截器 1.编写拦截器(实现HandlerInterceptor接口) 2.编写配置类实现WebMvcConfigurer,在该类中添加各种组件. a.注册拦截器 b.添加拦截器到spring mvc 拦截器里 2.整合事物和连接池 1.事物配置 a.添加事物相关的启动器依赖,mysql相关依赖 b.编写业务类Service使用事物注解@Transactional 2.数据库连接池hlkari配置 只需要在application配置文件中指定数据库相关参数 3.整合M