1. 程式人生 > >SpringBoot+ElasticSearch整合

SpringBoot+ElasticSearch整合

1、在使用SpringBoot整合Elasticsearch 之前,瞭解下它們之間的版本對應關係:

Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
x <= 1.3.5 y <= 1.3.4 z <= 1.7.2*
x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**

使用SpringBoot整合Elasticsearch,一般都是使用 SpringData 進行封裝的,然後再dao層介面繼承ElasticsearchRepository 類,該類實現了很多的方法,比如常用的CRUD方法。這裡使用Elasticsearch 5.6.11+SpringBoot 2.0.5+JDK 1.8版本搭建開發環境。

2、pom配置

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

2、application.properties的配置

spring.data.elasticsearch.repositories.enabled = true  //使用elasticsearch倉庫
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300  //預設
spring.data.elasticsearch.cluster-name = my-application  //要與elasticsearch.yml中使用的叢集名稱一致

3、索引與查詢例項

entity層:

package com.exm.springmybatis.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

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

@Document(indexName ="es_article",type = "article",indexStoreType = "fs",shards = 5,replicas = 1)
public class Article implements Serializable {
    @Id
    private Long id;
    private String title;  //標題
    private String abstracts;  //摘要
    private String content;  //內容
    private Date postTime;  //發表時間

    //set和get方法省略了
}

使用SpringData的時候,它需要在實體類中設定indexName 和type ,如果和傳統型資料庫比較的話,就相當於庫和表。需要注意的是indexName和type都必須是小寫!!!

dao層:

package com.exm.springmybatis.dao;

import com.exm.springmybatis.entity.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

public interface ArticleSearchRepository extends ElasticsearchRepository<Article,Long> {

}

dao層只需繼承ElasticsearchRepository該類。其中主要的方法就是 save、delete和search,其中save方法相當如insert和update,沒有就新增,有就覆蓋。delete方法主要就是刪除資料以及索引庫。至於search就是查詢了,包括一些常用的查詢,如分頁、權重之類的。

controller層:

package com.exm.springmybatis.controller;

import com.exm.springmybatis.dao.ArticleSearchRepository;
import com.exm.springmybatis.entity.Article;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Iterator;


@RestController
public class ESController {
    @Autowired
    ArticleSearchRepository articleSearchRepository;

    @RequestMapping("/add")
    public void testSaveArticleIndex() {
        Article article = new Article();
        article.setId(1L);
        article.setTitle("springboot integreate elasticsearch");
        article.setAbstracts("springboot integreate elasticsearch is very easy");
        article.setContent("elasticsearch based on lucene");
        article.setPostTime(new Date());
        articleSearchRepository.save(article);
    }

    @RequestMapping("/query")
    public void testSearch() {
        String queryString = "springboot";//搜尋關鍵字
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString);
        Iterable<Article> searchResult = articleSearchRepository.search(builder);
        Iterator<Article> iterator = searchResult.iterator();
        while (iterator.hasNext()) {
            Article article = iterator.next();
            System.out.println(article.getContent());
        }
    }
}

Controller層注意類註釋是@RestController。由於例子比較簡單Server層就沒有寫了。

索引:http://localhost:8090/add

搜尋:http://localhost:8090/query

head檢視索引:http://localhost:9100