SpringBoot integrates elastic search instance

1, Download and launch elastic search

Download address: https://www.elastic.co/downloads/past-releases

Select a version, Download

The blogger here tests 2.4.4

You can choose the ZIP package to download


There are some subtle differences between windows and mac when starting

windows: enter bin in the file directory, and then click elasticsearch.bat

mac: execute the command bin/elasticsearch on the terminal



2, Configure Maven

  1. <! -- Spring Boot Elasticsearch dependency -- >
  2. <dependency>
  3.    <groupId>org.springframework.boot</groupId>
  4.    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>


  1. <properties>
  2.    <elasticsearch.version>2.4.4</elasticsearch.version>
  3. </properties>



Add the spring boot starter data elastic search dependency and set the elastic search version to 2.4.4


ES and spingboot version reference:



3, Modify application.properties

# DataSource

Mainly add two last faces


4, Create ES Bean and Repository


package com.liuyanzhao.chuyun.domain.es;
import org.springframework.data.elasticsearch.annotations.Document;
import javax.persistence.Id;
import java.util.Date;
 * @author Speech and speech
 * @date 2018/1/22 4:45 p.m.
public class Article {
    //Article id, which must be id here
    private Long id;
    private String title;
    private String content;
    //Browse volume
    private Integer viewCount;
    //Release time
    private Date createTime;
    //Update time
    private Date updateTime;
    public Article() {
    public Long getId() {
        return id;
    public void setId(Long id) {
        this.id = id;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public String getContent() {
        return content;
    public void setContent(String content) {
        this.content = content;
    public Integer getViewCount() {
        return viewCount;
    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    public Date getCreateTime() {
        return createTime;
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    public Date getUpdateTime() {
        return updateTime;
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", viewCount=" + viewCount +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +



package com.liuyanzhao.chuyun.repository.es;
import com.liuyanzhao.chuyun.domain.es.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
 * @author Speech and speech
 * @date 2018/1/22 5:05 p.m.
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
    Page<Article> findDistinctByTitleContainingOrContentContaining(String title, String content, Pageable pageable);


5, Create test class


package com.liuyanzhao.chuyun.repository.es;
import com.liuyanzhao.chuyun.domain.es.Article;
import com.liuyanzhao.chuyun.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
 * @author Speech and speech
 * @date 2018/1/21 5:03 p.m.
public class ArticleRepositoryTest {
    private ArticleRepository articleRepository;
    public void initRepositoryData() {
        //Clear all data
        Article article = new Article();
        article.setId((long) 1);
        article.setTitle("<Butterfly love flower");
        article.setContent("The chrysanthemum on the threshold is worried about the smoke, the orchid is weeping the dew, the curtain is light and cold, and the swallow flies away. The bright moon does not know the bitterness of leaving hate, and the slanting light comes to the dawn to wear the Zhuhu. Last night, the west wind withered the green trees, alone on the high-rise, looking at the end of the world road. Where do you know if you want to send color paper and ruler?");
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        Article article2 = new Article();
        article2.setId((long) 2);
        article2.setTitle("<Butterfly love flower");
        article2.setContent("Stand on the wind of the dangerous building, look at the extreme spring sorrow, dark sky. In the light of the grass and smoke, no one can stop by saying. I want to get drunk and sing when I am drunk. It's still tasteless. my clothes grow daily more loose, yet care I not. For you am I thus wasting away in sorrow and pain.");
        article2.setCreateTime(new Date());
        article2.setUpdateTime(new Date());
        Article article3 = new Article();
        article3.setId((long) 3);
        article3.setTitle("<Qing Yu An·Lunar New Year's Eve");
        article3.setContent("East wind night flowers thousands of trees, but also blow down, the stars like rain. BMW carvings are full of fragrance. The sound of the Phoenix and the flute moves, the light of the jade pot turns, and the fish and the dragon dance all night. Moth son snow willow golden thread, smile Ying Ying Ying Xiang to go. Looking for him in the crowd, suddenly looking back, the man was in the dim light.");
        article3.setCreateTime(new Date());
        article3.setUpdateTime(new Date());
    public void findDistinctByTitleContainingOrContentContainingTest() throws Exception {
        Pageable pageable = new PageRequest(0,20);
        String title = "I love Luo Qi.";
        String content = "Flower tree";
        Page<Article> page = articleRepository.findDistinctByTitleContainingOrContentContaining(title, content, pageable);
        for(Article article : page.getContent()) {


Function @Test Method of annotation

Find a piece of data according to the title and content


Modify title and content

String title = "Butterfly Love";
String content = "Dongfeng";

Three pieces of data found


6, Visit http: / / localhost: 9200 / ﹣ plugin / head/

Because in the last article, we talked about installing a head plug-in. Now we can see the data in it (the redundant data was tested before)


7, New Controller class


package com.liuyanzhao.chuyun.controller;
import com.liuyanzhao.chuyun.domain.es.Article;
import com.liuyanzhao.chuyun.repository.es.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
 * @author Speech and speech
 * @date 2018/1/22 9:07 p.m.
public class ArticleController {
    private ArticleRepository ArticleRepository;
    public List<Article> list(@RequestParam(value = "title", required = false) String title,
                                @RequestParam(value = "content", required = false) String content,
                                @RequestParam(value = "pageIndex", defaultValue = "0") int pageIndex,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Pageable pageable = new PageRequest(pageIndex, pageSize);
        Page<Article> page = ArticleRepository.findDistinctByTitleContainingOrContentContaining(title, content, pageable);
        return page.getContent();

Because data has been added to elasticsearch in the test class before

All are now accessible on the browser:

http://localhost:8080/chuyun/article?title = langtaosha & content = Yiren

Keywords: Spring ElasticSearch Thymeleaf Java

Added by eroticheretic on Tue, 31 Dec 2019 19:29:29 +0200