SpringBoot2.3集成ElasticSearch7.6

avatar 2021年11月20日18:55:13 6 2897 views
博主分享免费Java教学视频,B站账号:Java刘哥

最近在做基于web的svn平台,需要把文件名称等等信息存储到数据库,查询提供查询接口,为了提高查询性能,我们考虑把数据库信息也缓存到ES中一份。

本文介绍springBoot2集成ES7.6。

完整代码demo

1、pom.xml

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

2、application.yml

#spring配置
spring:
# ES 设置
  elasticsearch:
    rest:
      uris: ["http://127.0.0.1:9200"]
      connection-timeout: 10s

3、实体类

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.stereotype.Component;


import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

/**
 * 文件类对应索引
 */
@Component
@Document(indexName = "common_file", indexStoreType = "common_file", shards = 1, replicas = 0, refreshInterval = "10")
@Data
public class EsCommonFile implements Serializable
{

    /**
     * @Field 文档说明
     * @Field(type=FieldType.Text, analyzer="ik_max_word")     表示该字段是一个文本,并作最大程度拆分,默认建立索引
     * @Field(type=FieldType.Text,index=false) 表示该字段是一个文本,不建立索引
     * @Field(type=FieldType.Date) 表示该字段是一个文本,日期类型,默认不建立索引
     * @Field(type=FieldType.Long) 表示该字段是一个长整型,默认建立索引
     * @Field(type=FieldType.Keyword) 表示该字段内容是一个文本并作为一个整体不可分,默认建立索引
     * @Field(type=FieldType.Float) 表示该字段内容是一个浮点类型并作为一个整体不可分,默认建立索引
     * date 、float、long都是不能够被拆分的
     */


    /**
     * ID
     */
    @Id
    private Long id;

    /**
     * 父ID
     */
    @Field(type = FieldType.Keyword)
    private Long parentId;

    /**
     * 编码
     */
    @Field(type = FieldType.Keyword)
    private String longCode;

    /**
     * 文件名称
     */
    @Field(type = FieldType.Text, analyzer = "simple")
    private String fileName;

    /**
     * 文件类型(1 file,2 folder)
     */
    @Field(type = FieldType.Keyword)
    private Integer fileType;

    /**
     * 文件资源url
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String fileUrl;

    /**
     * 文件版本
     */
    @Field(type = FieldType.Keyword)
    private Long revision;

    /**
     * 创建人账号
     */
    @Field(type = FieldType.Keyword)
    private String creatorName;

    /**
     * 修改人账号
     */
    @Field(type = FieldType.Keyword)
    private String updaterName;

    /**
     * 创建人ID
     */
    @Field(type = FieldType.Keyword)
    private String creatorId;

    /**
     * 创建时间
     * 时间format参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
     * basic_date_time_no_millis = yyyyMMdd'T'HHmmssZ
     */
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time_no_millis)
    private Date createTime;

    /**
     * 更新人ID
     */
    @Field(type = FieldType.Keyword)
    private String updaterId;

    /**
     * 更新时间
     */
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time_no_millis)
    private Date updateTime;

}

 

4、Repostory接口

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * ES文件dao层
 *
 * @author liuyanzhao
 */
@Repository
public interface EsCommonFileRepository extends ElasticsearchRepository<EsCommonFile, Long>
{

    /**
     * 根据文件名称查询
     *
     * @param fileName
     * @return
     */
    List<EsCommonFile> findByFileName(String fileName);
}

 

5、SpringBoot启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@EnableElasticsearchRepositories("com.xxx.x.repository") // es repository所在包名
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

 

6、控制器测试类

// 部分包自己导入
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 测试类
 * @author lyz
 */
@Slf4j
@RestController
@RequestMapping("/esCommonFile")
@Api(tags = "es测试")
@ApiResource(resName = "es测试")
public class EsCommonFileController
{

    @Autowired
    private EsCommonFileRepository esCommonFileRepository;


    /**
     * 测试查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public Msg findAll()
    {

        Iterable<EsCommonFile> iterable = esCommonFileRepository.findAll();
        List<EsCommonFile> list = new ArrayList<>();
        iterable.forEach(p -> list.add(p));
        return Msg.getSuccess(list);
    }

    /**
     * 测试查询所有
     *
     * @return
     */
    @GetMapping("/findByFileName")
    public Msg findByFileName(String fileName)
    {
        List<EsCommonFile> list = esCommonFileRepository.findByFileName(fileName);
        return Msg.getSuccess(list);
    }

    /**
     * 测试添加
     *
     * @return
     */
    @PostMapping("/save")
    public Msg save()
    {
        EsCommonFile esCommonFile = new EsCommonFile();
        esCommonFile.setId(100L);
        esCommonFile.setFileName("test123.txt");
        esCommonFile.setFileUrl("http://DESKTOP-E7NQ281/svn/demo/trunk/test123.txt");
        esCommonFile.setFileType(1);
        esCommonFile.setLongCode("100000001");
        esCommonFile.setParentId(1L);
        esCommonFile.setRevision(1L);
        esCommonFile.setUpdaterId("6c5e98d5-6977-4047-b1ff-b08a40d3c8b7");
        esCommonFile.setCreatorId("6c5e98d5-6977-4047-b1ff-b08a40d3c8b7");
        esCommonFile.setUpdaterName("言曌");
        esCommonFile.setUpdaterName("言曌");
        esCommonFile.setCreatorName("言曌");
        esCommonFile.setCreateTime(new Date());
        esCommonFile.setUpdateTime(new Date());
        esCommonFileRepository.save(esCommonFile);
        return Msg.getSuccessMsg("成功");
    }

    /**
     * 测试删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Msg deleteById(Long id)
    {
        esCommonFileRepository.deleteById(id);
        return Msg.getSuccessMsg("成功");
    }
}

 

 

二、ES开启远程控制

这里只介绍windows的设置

在 elasticsearch.yml 添加

network.host: 0.0.0.0
http.port: 9200
transport.host: localhost
transport.tcp.port: 9300

重启ES

 

然后可以使用 elasticsearch-head 访问

然后就可以远程访问其他机器上的ES了

ES安装过于简单,这里就不介绍了

 

关于使用分词器创建索引的可以看这篇文章

https://liuyanzhao.com/1466610788520300545.html

 

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0