栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

知识点13--spring boot整合elasticsearch

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

知识点13--spring boot整合elasticsearch

本章知识点沿用知识点12的项目,介绍如何使用spring boot整合ES,没有ES的去我主页各类型大数据集群搭建文档-->大数据原生集群本地测试环境搭建三中可以看到ES如何搭建

不管你有没有ES,最好是没有,因为一定要知道一点,一定要去官网查一下你当前用的spring boot data es的版本是不是和你自己ES服务器所匹配的,这一点简直是天坑,spring boot提供的es封装API对es的版本要求相当苛刻,对不上就用不了,很多人折在版本问题,奉劝大家一句,除非正式的项目开发上,团队会给你提供需要版本的jar,正式开发本身版本都是经过架构师仔细考虑并且版本方面问题都解决了,自己学习自己开发,你最好不要再jar上做坚持,直接去改你的es服务版本,也不要轻信网上说的改pom版本,会连带着出很多不必要的问题,官网如下https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.requirements

比如,我在写这篇博文的时候spring boot沿用的前面知识点项目,spring boot版本是2.7.0,自动导入的spring boot data es版本是4.4.0,我的ES就要用7.17.3的,但是我本地先前用的es是6.6.2的,含泪重装。

同时不同的spring boot会受版本的影响,导致不同的操作ES方式会出现不同的问题,最常见的是操作方式过时,所以如果大家使用的版本和我的不一样,很可能导致无法操作

第一步:添加依赖



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

第二步:修改spring boot配置文件,添加如下的配置

spring.elasticsearch.uris=http://192.168.88.187:9200
spring.data.elasticsearch.repositories.enabled=true

第三步:对操作的实体Bean加注解

对你要在es操作的数据添加文档注释,indexName 是索引名称
@Document(indexName = "users")

对id字段,必须加上Id注解
@Id

对需要分词且存在中文的字段必须使用Field注解指定ik分词
@Field(index=true,store=true,analyzer="ik_max_word",searchAnalyzer="ik_max_word",type=FieldType.Text)

解释一下,需要分词的字段就是查询做条件的字段,比如我要操作的User类

package com.wy.scjg.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
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 org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;

@ToString
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@Document(indexName = "users")
public class User implements Serializable {
    
    @TableId(value = "id", type = IdType.AUTO)
    @Id
    private Long id;

    
    @Field(index=true,store=true,analyzer="ik_max_word",searchAnalyzer="ik_max_word",type= FieldType.Text)
    private String name;

    
    private Integer age;

    
    private String email;

    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date brith;

    private Integer did;

    //表中不存在的字段
    @TableField(exist = false)
    private String dname;

    
    private String zp;

    
    private String grz;
}

第四步:建立一个包,存放ES操作接口文件,并准备一个操作User数据的ES接口

包和类名大家自定义

package com.wy.scjg.repository;

import com.wy.scjg.bean.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface UserRepository extends ElasticsearchRepository {

}

第五步:用测试类,向ES操作初始化User表的数据

package com.wy.scjg;

import com.wy.scjg.bean.User;
import com.wy.scjg.repository.UserRepository;
import com.wy.scjg.service.UserService;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class ESTest {

	//注入  UserRepository 对象操作ES
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @Test
    void imports(){
        List list = userService.list();
        for(User user:list){
            userRepository.save(user);
        }
    }

}

运行代码后,查看es数据

这个时候我们就可以将详情,功能优化,因为ES没有说用它做修改的,都是优化查询

第六步:修改查询详情的Controller,将数据从ES查询

@RequestMapping("/detail")
public String detail(HttpServletRequest request,Long id){
    Optional byId = userRepository.findById(id.toString());

    //存储
    request.setAttribute("user",byId.get());
    return "/user_detail";
}

运行看效果

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

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

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