springboot2.4.5集成ES

springboot2.4.5对应的es版本号7.9.3

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.4.5</version>
  </parent>

<elasticsearch.version>7.9.3</elasticsearch.version>

1.引入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2.添加配置

server.port=8081

# es
spring.elasticsearch.rest.uris = 127.0.0.1:9200
spring.elasticsearch.rest.username=''
spring.elasticsearch.rest.password=''

# 日期格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

3.构建entity

package com.example.springboot.es.entity.es;

import lombok.Data;
import org.springframework.data.annotation.Id;
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 java.util.Date;

/**
 * created by <a href="mailto:18045142590@163.com" > mm</a>
 */
@Data
@Document(indexName = "blog", useServerConfiguration = true, createIndex = false)
public class ESBlog {

    @Id
    private Integer id;

    @Field(type = FieldType.Text, analyzer = "id_max_word")
    private String title;
    @Field(type = FieldType.Text, analyzer = "id_max_word")
    private String author;
    @Field(type = FieldType.Text, analyzer = "id_max_word")
    private String content;
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
    private Date createTime;
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
    private Date updateTime;
}

4.构建repository

package com.example.springboot.es.repository.es;

import com.example.springboot.es.entity.es.ESBlog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * created by <a href="mailto:18045142590@163.com" > mm</a>
 */
public interface ESBlogRepository extends ElasticsearchRepository<ESBlog, Integer> {
}

5.构建controller

package com.example.springboot.es.controller;

import com.example.springboot.es.entity.es.ESBlog;
import com.example.springboot.es.repository.es.ESBlogRepository;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * created by <a href="mailto:18045142590@163.com" > mm</a>
 */
@RestController
@AllArgsConstructor
@RequestMapping("/es")
public class ESController {

    private final ESBlogRepository esBlogRepository;

    @GetMapping("/test")
    public ESBlog test() {
        return esBlogRepository.findAll().iterator().next();
    }

}

6.接口测试http://127.0.0.1:8081/es/test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值