中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

全文檢索ElasticSearch與Spring boot集成實例

發布時間:2020-06-20 14:15:17 來源:網絡 閱讀:3036 作者:墨營 欄目:軟件技術

全文檢索
1.全文搜索概念:
(1)數據結構:
·結構化:只具有固定格式或者有限長度的數據,如數據庫,元數據等
·非結構化:指不定長或者無固定格式的數據,如郵件,word文檔等
(2)非結構化數據的檢索:
·順序掃描法:適合小數據量文件
·全文搜索:將非結構化的數據轉為結構化的數據,然后創建索引,在進行搜索
(3)概念:全文搜索是一種將文件中所有文本域搜索項匹配的文件資料檢索方式
2.全文搜索實現原理
全文檢索ElasticSearch與Spring boot集成實例
3.全文搜索實現技術:基于java的開源實現Lucene,ElasticSearch(具有自身的分布式管理功能),Solr
4.ElasticSearch簡介:
概念:
(1)高度可擴展的開源全文搜索和分析引擎
(2)快速的,近實的多大數據進行存儲,搜索和分析
(3)用來支撐有復雜的數據搜索需求的企業級應用
特點及介紹:
(1)分布式
(2)高可用
(3)對類型,支持多種數據類型
(4)多API
(5)面向文檔
(6)異不寫入
(7)近實時:每隔n秒查詢,在寫入磁盤中
(8)基于Lucene
(9)Apache協議
5.ElasticSearch與Spring Boot集成
(1)配置環境:ElasticSearch,Spring Data ElasticSearch,JNA
(2)安裝ElasticSearch,下載包,解壓直接啟動即可,這里特別說一下ElasticSearch的一些異常問題,必須版本對應,其次端口問題一定要注意
(3)建立Spring Boot項目
(4)我們修改pom.xml文件,將相關依賴加進去
(5)在項目代碼編寫之前我們必須在本地安裝ElasticSearch并在版本上與Spring Boot版本相兼容,其次注意端口號的問題,集成時ElasticSearch服務的端口號為9200,而客戶端端口號為9300
接下來我們啟動本地安裝的ElasticSearch然后在啟動我們的項目:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dhtt.spring.boot.blog</groupId>
    <artifactId>spring.data.action</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring.data.action</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- spring boot集成elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 添加熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- JNA 的依賴 -->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <!-- 內存數據庫h3 -->
        <!-- <dependency> <groupId>com.h3database</groupId> <artifactId>h3</artifactId> 
            </dependency> -->

        <!-- MySql數據庫驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- hibernate持久層框架引入 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.7.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

啟動項目進行測試,觀察項目各項配置是否正確,項目能否成功啟動,項目啟動成功后
(5)接下來配置application.properties文件:

#thymeleaf配置
spring.thymeleaf.encoding=UTF-8
#熱部署靜態文件,不需要緩存,實時觀察文件修改效果
spring.thymeleaf.cache=false
#使用html5標準
spring.thymeleaf.mode=HTML5
spring.thymeleaf.suffix=.html
spring.resources.chain.strategy.content.enabled=true

#elasticsearch服務器地址
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#連接超時時間
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
 #節點名字,默認elasticsearch
#spring.data.elasticsearch.cluster-name=elasticsearch 
#spring.data.elasticsearch.repositories.enable=true
#spring.data.elasticsearch.properties.path.logs=./elasticsearch/log
#spring.data.elasticsearch.properties.path.data=./elasticsearch/data

#數據庫連接配置
spring.datasource.url=jdbc:mysql://localhost:3306/blog_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=qitao1996
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#jpa配置
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

(6)進行后臺編碼:
文檔類EsBlog:

package com.dhtt.spring.boot.blog.spring.data.action.entity;

import java.io.Serializable;

import javax.persistence.Id;

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

/**
 * EsBlog實體(文檔)類
 * 
 * @author QiTao
 *
 */
@Document(indexName="blog",type="blog")   //指定文檔
public class EsBlog implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4745983033416635193L;
    @Id
    private String id;
    private String title;
    private String summary;
    private String content;

    protected EsBlog() {
        super();
    }

    public EsBlog(String title, String summary, String content) {
        super();
        this.title = title;
        this.summary = summary;
        this.content = content;
    }

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "EsBlog [id=" + id + ", title=" + title + ", summary=" + summary + ", content=" + content + "]";
    }

}

資源庫,定義數據查詢接口:

package com.dhtt.spring.boot.blog.spring.data.action.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.dhtt.spring.boot.blog.spring.data.action.entity.EsBlog;

/**
 * EsBlogRepository接口
 * 
 * @author QiTao
 *
 */
public interface EsBlogRepository extends ElasticsearchRepository<EsBlog, String> {
    /**
     * 分頁,查詢,去重
     * 
     * @param title
     * @param summary
     * @param content
     * @param pageable
     * @return
     */
    Page<EsBlog> findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary,
            String content, PageRequest pageRequest);
}

最后編寫Controller類:

package com.dhtt.spring.boot.blog.spring.data.action.web.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dhtt.spring.boot.blog.spring.data.action.entity.EsBlog;
import com.dhtt.spring.boot.blog.spring.data.action.repository.EsBlogRepository;

@RestController
@RequestMapping("/blogs")
public class BlogController {
    @Autowired
    private EsBlogRepository esBlogRepository;

    @GetMapping
    public List<EsBlog> list(@RequestParam(value = "title") String title,
            @RequestParam(value = "summary") String summary, 
            @RequestParam(value = "content") String content,
            @RequestParam(value = "pageIndex", defaultValue = "0") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //添加測試數據
        esBlogRepository.deleteAll();
        esBlogRepository.save(new EsBlog("登黃鶴樓", "王之渙的等黃鶴樓", "百日依山盡,黃河入海流,欲窮千里目,更上一層樓"));
        esBlogRepository.save(new EsBlog("相思", "王維的相思", "紅豆生南國,春來發幾枝,愿君多采截,此物最相思"));
        esBlogRepository.save(new EsBlog("靜夜思", "李白的靜夜思", "床前明月光,疑是地上霜,舉頭望明月,低頭思故鄉"));
        //查詢獲取
        PageRequest pageRequest=PageRequest.of(pageIndex,pageSize);
        Page<EsBlog> page= esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageRequest);
        return page.getContent();

    }

}

啟動項目,前臺進行訪問:
全文檢索ElasticSearch與Spring boot集成實例

前臺結果打印成功,故我們的Elasticsearch+Spring Boot集成成功

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

浦北县| 光山县| 巧家县| 大宁县| 都匀市| 土默特右旗| 山东省| 托克托县| 鄂托克前旗| 化州市| 慈溪市| 西林县| 若尔盖县| 金寨县| 板桥市| 东兴市| 永清县| 江口县| 河源市| 铜鼓县| 黄石市| 贡嘎县| 海伦市| 桃源县| 临武县| 望谟县| 漳浦县| 巫溪县| 无极县| 建阳市| 滦平县| 江源县| 南木林县| 延安市| 南雄市| 武胜县| 阿图什市| 岳普湖县| 通辽市| 合江县| 奇台县|