springboot整合elasticsearch

此篇文章为springboot整合elasticsearch的demo,以此做一下记录

环境准备

环境版本
springboot2.0.5.RELEASE
elasticsearch6.2.4

注意:建议不要用elasticsearch的最新版,参考了spring data elasticsearch官网
在这里插入图片描述
因为整合springboot根elasticsearch有版本要求,所以建议按照官网的版本来实现demo,以免有其他坑出现。

运行elasticsearch

运行环境:win10
去到对应elasticsearch目录下的bin目录运行elasticsearch.bat
在这里插入图片描述
cmd运行elasticsearch.bat
在这里插入图片描述
运行起来
在这里插入图片描述
访问

http://localhost:9200/

在这里插入图片描述

demo代码展示

代码结构图

在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.elasticsearch.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

application.yml


server:
  port: 8080

spring:
  data:
    elasticsearch:
      cluster-nodes: localhost:9300
#      cluster-name: my-application

model

com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel

package com.example.elasticsearch.demo.demo.elasticsearch.model;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

@Data
@Accessors(chain = true)
@Document(indexName = "blog", type = "java")
public class BlogModel implements Serializable {

    private static final long serialVersionUID = 6320548148250372657L;

    @Id
    private String id;

    private String title;

    //@Field(type = FieldType.Date, format = DateFormat.basic_date)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date time;
}

service

com.example.elasticsearch.demo.demo.elasticsearch.service.BlogRepository

package com.example.elasticsearch.demo.demo.elasticsearch.service;

import com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface BlogRepository extends ElasticsearchRepository<BlogModel, String> {

    List<BlogModel> findByTitleLike(String keyword);
}

controller

package com.example.elasticsearch.demo.demo.elasticsearch.controller;

import com.example.elasticsearch.demo.demo.elasticsearch.service.BlogRepository;
import com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogRepository blogRepository;

    @PostMapping("/add")
    public String add(@RequestBody BlogModel blogModel) {
        blogRepository.save(blogModel);
        return "SAVE IN ELASTICSEARCH OK";
    }

    @GetMapping("/get/{id}")
    public BlogModel getById(@PathVariable String id) {

        Optional<BlogModel> blogModelOptional = blogRepository.findById(id);
        if (blogModelOptional.isPresent()) {
            BlogModel blogModel = blogModelOptional.get();
            return blogModel;
        }
        return null;
    }

    @GetMapping("/get")
    public List getAll() {
        Iterable<BlogModel> iterable = blogRepository.findAll();
        List<BlogModel> list = new ArrayList<>();
        iterable.forEach(list::add);
        return list;
    }

    @GetMapping("/title")
    public List repSearchTitle(@RequestParam String keyword) {
        if (StringUtils.isEmpty(keyword))
            return null;
        return blogRepository.findByTitleLike(keyword);
    }
}

启动类

com.example.elasticsearch.demo.demo.DemoApplication

package com.example.elasticsearch.demo.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

postman请求连接

插入数据

在这里插入图片描述

{
    "title":":美国环境保护署",
    "time":"2019-05-06"
}
{
    "title":":美国多个品牌快餐店停售汉堡",
    "time":"2019-05-06"
}
{
    "title":":美国新冠感染病例超过125万例",
    "time":"2019-05-06"
}

插入后返回json

SAVE IN ELASTICSEARCH OK

查询数据

在这里插入图片描述
get请求可在浏览器访问

http://localhost:8080/blog/get

返回json

[
    {
        "id": "EfEE83EBpdk2CU62IpTH",
        "title": ":美国新冠感染病例超过125万例",
        "time": "2019-05-06"
    },
    {
        "id": "EvEW83EBpdk2CU62XpQN",
        "title": ":美国多个品牌快餐店停售汉堡",
        "time": "2019-05-06"
    },
    {
        "id": "E_EX83EBpdk2CU62MJRL",
        "title": ":美国环境保护署",
        "time": "2019-05-06"
    },
    {
        "id": "EPH98nEBpdk2CU62f5TW",
        "title": "Elasticsearch实战篇:Spring Boot整合ElasticSearch",
        "time": "2019-05-06"
    }
]

请求

http://localhost:8080/blog/title?keyword=美国

结果

[
    {
        "id": "E_EX83EBpdk2CU62MJRL",
        "title": ":美国环境保护署",
        "time": "2019-05-06"
    },
    {
        "id": "EfEE83EBpdk2CU62IpTH",
        "title": ":美国新冠感染病例超过125万例",
        "time": "2019-05-06"
    },
    {
        "id": "EvEW83EBpdk2CU62XpQN",
        "title": ":美国多个品牌快餐店停售汉堡",
        "time": "2019-05-06"
    }
]

请求url

http://localhost:8080/blog/get/E_EX83EBpdk2CU62MJRL

结果

{
    "id": "E_EX83EBpdk2CU62MJRL",
    "title": ":美国环境保护署",
    "time": "2019-05-06"
}

请求url


github连接

https://github.com/sky5cai/Elasticsearchdemo

参考资料

Spring Data Elasticsearch - Reference Documentation

https://docs.spring.io/spring-data/elasticsearch/docs/3.2.7.RELEASE/reference/html/#repositories.query-methods

Elasticsearch实战篇——Spring Boot整合ElasticSearch

https://segmentfault.com/a/1190000018625101
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值