SpringBoot和檢索


1 簡介

  • 我們的應用經常需要添加檢索功能,開源的ElasticSearch是目前全文搜索引擎的首選。它可以快速的存儲、搜索和分析海量數據。SpringBoot通過整合Spring Data ElasticSearch為我們提供了非常便捷的檢索功能的支持。
  • ElasticSearch是一個分布式搜索功能,提供Restful API,底層基於Lucene,采用多shard(分片)的方式保證數據安全,並且提供自動resharding的功能,github等大型的站點也是采用ElasticSearch作為其搜索服務。

2 概念

2.1 概念

  • 以員工文檔的形式存儲為例:一個文檔代表一個員工數據。存儲數據到ElasticSearch的行為叫做索引,但是在索引一個文檔之前,需要確定將文檔存儲在哪里。
  • 一個ElasticSeach集群可以包含多個索引,相應的每個索引可以包含多個類型。這些不同的類型存儲着多個文檔,每個文檔有多個屬性。
  • 類似關系:
    • 索引--數據庫。
    • 類型--表。
    • 文檔--表中記錄。
    • 屬性--列。

ES概念

2.2 應用示例(摘自官網)

  • 對於員工目錄,我們將做如下操作:

  • 1️⃣每個員工索引一個文檔,文檔包含該員工的所有信息。

  • 2️⃣每個文檔都將是 employee 類型

  • 3️⃣該類型位於 索引 megacorp 內。

  • 4️⃣該索引保存在我們的 Elasticsearch 集群中。

  • 實踐中這非常簡單(盡管看起來有很多步驟),我們可以通過一條命令完成所有這些動作:

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
  • 注意,路徑 /megacorp/employee/1 包含了三部分的信息:
  • megacorp:索引名稱。
  • employee:類型名稱。
  • 1:特定雇員的ID。

存儲數據到ES中

3 SpringBoot整合ElasticSearch

3.1 Docker安裝ElasticSearch

docker run -d --name elasticsearch -p 9200:9200 \
 -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
 -d elasticsearch:7.7.0

3.2 搭建環境

SpringBoot的版本是2.3.4.RELEASE,ES的版本是7.7.0。

  • 導入相關jar包的Maven坐標:
<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>
  • application.yml
spring:  
  elasticsearch:
    rest:
      uris: http://192.168.237.100:9200

3.3 編寫實體類

  • Article.java:
package com.sunxiaping.springboot.domain;

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

import java.io.Serializable;

@Document(indexName = "es",type = "_article")
public class Article implements Serializable {

    @Id
    private Integer id;

    private String author;

    private String title;

    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    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;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", author='" + author + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

3.4 編寫持久層接口

  • 編寫ArticleRepository接口:
package com.sunxiaping.springboot.repository;

import com.sunxiaping.springboot.domain.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleRepository extends ElasticsearchRepository<Article,Integer> {

}

3.5 測試

package com.sunxiaping.springboot;

import com.sunxiaping.springboot.domain.Article;
import com.sunxiaping.springboot.repository.ArticleRepository;
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.test.context.junit4.SpringRunner;

import java.util.Optional;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringbootApplicationTests {

    @Autowired
    private ArticleRepository articleRepository;

    @Test
    public void test(){
        Article article = new Article();
        article.setId(1);
        article.setTitle("震驚了,ES竟然可以這么用");
        article.setAuthor("UC編輯部");
        article.setContent("********震驚了,ES竟然可以這么用*********");
        articleRepository.save(article);
    }

    @Test
    public void test2(){
        Optional<Article> optional = articleRepository.findById(1);
        optional.ifPresent(article->{
            System.out.println("article = " + article);
        });


    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM