首页 > 其他分享 >springboot整合Elasticsearch

springboot整合Elasticsearch

时间:2022-11-24 11:03:36浏览次数:65  
标签:springboot student springframework Elasticsearch elasticsearch 整合 org import

springboot     2.2.5.RELEASE

Elasticsearch     7.17.6

Elasticsearch下载地址https://www.elastic.co/cn/downloads/elasticsearch

还有个大概的对应版本图:

 

如果要使用IK分词器的话,也有版本对应关系:

先准备好环境,然后开始第一步:

一: 解压Elasticsearch,bin\elasticsearch.bat 启动。

二:pom 添加 Elasticsearch jar,版本让它自己控制,我这里没有指定。

<!-- 整合Elasticsearch -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  

三:新建student 实体类(没有装IK分词器的时候,不要指定ik_smart,ik_max_word等等,不然报错。)

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;


/** elasticsearch demo
* @date 20221123 11:13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
//indexName名字如果是字母那么必须是小写字母
@Document(indexName = "student")
public class Student implements Serializable {

    @Id
    @Field(store = true, type = FieldType.Keyword)
    private Integer id;


    @Field(store = true, type = FieldType.Keyword)
    private String name;

    @Field(store = true, type = FieldType.Text)
    //Text可以分词 ik_smart=粗粒度分词 ik_max_word 为细粒度分词
    private String address;

    @Field(index = false, store = true, type = FieldType.Integer)
    private Integer age;

    @Field(index = false, store = true, type = FieldType.Keyword)
    private String createTime;

}

  

第四步:创建接口类  StudentMapper

import ideal4j.pfa.resume.model.Student;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @ClassName: StudentMapper
 * @Description: elasticsearch demo
 * @Date 2022/11/23
 * @Version 1.0
 */
public interface StudentMapper extends ElasticsearchRepository<Student, Integer> {


}

  

第五步:创建action 类

import com.alibaba.fastjson.JSON;
import ideal4j.pfa.resume.mapper.StudentMapper;
import ideal4j.pfa.resume.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @ClassName: TestAction
 * @Description:
 * @Date 2022/11/23 13:59
 */
@RestController
public class TestAction {

    @Autowired
    public StudentMapper studentMapper;

    @PostMapping("/elasticsearch/test")
    public Student test(@RequestBody Student student) {
        return studentMapper.save(student);
    }

    @GetMapping("/elasticsearch/student")
    public Student get(@RequestParam Integer id) {
        Optional<Student> student = studentMapper.findById(id);
        System.out.println(JSON.toJSONString(student));
        return student.get();
    }


}

  

第六步:postman展示测试成果

 

 

 

 

问题一:Elasticsearch启动控制台乱码,参考:https://www.cnblogs.com/isyysblog/p/16917608.html

问题二:Elasticsearch版本不对应,sava 一直报错,查询也查不到,刚开始用的5.5.3。

问题三:刚开始可以不安装IK分词器,事儿多。

问题四:kibana 如果使用,要下载对应版本,不然用不了。

 

祝大家工作顺利,生活如意!

 

标签:springboot,student,springframework,Elasticsearch,elasticsearch,整合,org,import
From: https://www.cnblogs.com/isyysblog/p/16921086.html

相关文章

  • SpringBoot3整合MyBatis报错:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate
    遇到了一个SpringBoot3整合MyBatis的问题,然后解决了。当然,这其实不是个大问题,只是自己编码时遇到了,然后总结总结分享一下。如果有遇到类似问题的,可以参考一下。交代......
  • SpringBoot监控
    SpringBoot监控(了解)Actuator监控SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、配置属性、日志信息等。在第一天的sp......
  • springboot整合jsp打包
    本文主要分享了对整合jsp的springboot项目打jar包,如何正确的配置maven pom。二、打包配置1.pom.xml添加spring-boot-maven-plugin配置<build><plugins>......
  • springboot整合jsp传参
    ViewController:packagecom.keafmd.controller;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframewo......
  • Springboot应用中过滤器chain.doFilter后设置header无效
    本文是在使用过滤器添加动态header过程中遇到设置header无效,经过研究源码而产生。因为特殊需求,自定义的header必须在经过Controller处理之后,才能确定,所以不能在请求处理......
  • elasticsearch7报错解决
    @​​TOC​​​系统​​:centos7​​es版本​​:elasticsearch-7.17.6​​后台启动​​:./elasticsearch-d1.ThereisinsufficientmemoryfortheJavaRuntimeEnvironm......
  • mongoDB的应用场景以及Spring和mongodb的整合
    mongoDB的应用场景​ mongodb是非关系型数据库,他的存储数据可以超过上亿条(老版本的mongodb有丢数据的情况,新版本不会有,网上说的),mongodb适合存储一些量大表关系较简单的数......
  • Windows 下 ElasticSearch8.5.1下载安装及使用
    ElasticSearch下载安装及使用前言Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式的全文搜索引擎,基于restfulweb接口。Elasticsearch是用Java语言开发......
  • springBoot
    说说你对mvc的理解 MVC是一种设计模式,在这种设计模式下软件被分为了3层:model、view、controller;model层主要是定义实体对象等数据,封装了数据和对数据的操作,是实际进行数据......
  • 如何安装 Elasticsearch
    实战场景如何安装Elasticsearch知识点CentOSJavaElasticsearch安装Kibana安装菜鸟实战Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的......