全文檢索
1.全文搜索概念:
(1)數(shù)據(jù)結(jié)構(gòu):
·結(jié)構(gòu)化:只具有固定格式或者有限長度的數(shù)據(jù),如數(shù)據(jù)庫,元數(shù)據(jù)等
·非結(jié)構(gòu)化:指不定長或者無固定格式的數(shù)據(jù),如郵件,word文檔等
(2)非結(jié)構(gòu)化數(shù)據(jù)的檢索:
·順序掃描法:適合小數(shù)據(jù)量文件
·全文搜索:將非結(jié)構(gòu)化的數(shù)據(jù)轉(zhuǎn)為結(jié)構(gòu)化的數(shù)據(jù),然后創(chuàng)建索引,在進(jìn)行搜索
(3)概念:全文搜索是一種將文件中所有文本域搜索項(xiàng)匹配的文件資料檢索方式
2.全文搜索實(shí)現(xiàn)原理
3.全文搜索實(shí)現(xiàn)技術(shù):基于java的開源實(shí)現(xiàn)Lucene,ElasticSearch(具有自身的分布式管理功能),Solr
4.ElasticSearch簡介:
概念:
(1)高度可擴(kuò)展的開源全文搜索和分析引擎
(2)快速的,近實(shí)的多大數(shù)據(jù)進(jìn)行存儲,搜索和分析
(3)用來支撐有復(fù)雜的數(shù)據(jù)搜索需求的企業(yè)級應(yīng)用
特點(diǎn)及介紹:
(1)分布式
(2)高可用
(3)對類型,支持多種數(shù)據(jù)類型
(4)多API
(5)面向文檔
(6)異不寫入
(7)近實(shí)時:每隔n秒查詢,在寫入磁盤中
(8)基于Lucene
(9)Apache協(xié)議
5.ElasticSearch與Spring Boot集成
(1)配置環(huán)境:ElasticSearch,Spring Data ElasticSearch,JNA
(2)安裝ElasticSearch,下載包,解壓直接啟動即可,這里特別說一下ElasticSearch的一些異常問題,必須版本對應(yīng),其次端口問題一定要注意
(3)建立Spring Boot項(xiàng)目
(4)我們修改pom.xml文件,將相關(guān)依賴加進(jìn)去
(5)在項(xiàng)目代碼編寫之前我們必須在本地安裝ElasticSearch并在版本上與Spring Boot版本相兼容,其次注意端口號的問題,集成時ElasticSearch服務(wù)的端口號為9200,而客戶端端口號為9300
接下來我們啟動本地安裝的ElasticSearch然后在啟動我們的項(xiàng)目:
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的沈丘網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
4.0.0
com.dhtt.spring.boot.blog
spring.data.action
0.0.1-SNAPSHOT
jar
spring.data.action
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.data
spring-data-elasticsearch
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
net.java.dev.jna
jna
4.5.1
org.elasticsearch
elasticsearch
mysql
mysql-connector-java
5.1.46
org.hibernate
hibernate-core
5.3.7.Final
org.springframework.boot
spring-boot-maven-plugin
啟動項(xiàng)目進(jìn)行測試,觀察項(xiàng)目各項(xiàng)配置是否正確,項(xiàng)目能否成功啟動,項(xiàng)目啟動成功后
(5)接下來配置application.properties文件:
#thymeleaf配置
spring.thymeleaf.encoding=UTF-8
#熱部署靜態(tài)文件,不需要緩存,實(shí)時觀察文件修改效果
spring.thymeleaf.cache=false
#使用html5標(biāo)準(zhǔn)
spring.thymeleaf.mode=HTML5
spring.thymeleaf.suffix=.html
spring.resources.chain.strategy.content.enabled=true
#elasticsearch服務(wù)器地址
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#連接超時時間
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
#節(jié)點(diǎn)名字,默認(rèn)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
#數(shù)據(jù)庫連接配置
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)進(jìn)行后臺編碼:
文檔類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實(shí)體(文檔)類
*
* @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 + "]";
}
}
資源庫,定義數(shù)據(jù)查詢接口:
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 {
/**
* 分頁,查詢,去重
*
* @param title
* @param summary
* @param content
* @param pageable
* @return
*/
Page 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 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) {
//添加測試數(shù)據(jù)
esBlogRepository.deleteAll();
esBlogRepository.save(new EsBlog("登黃鶴樓", "王之渙的等黃鶴樓", "百日依山盡,黃河入海流,欲窮千里目,更上一層樓"));
esBlogRepository.save(new EsBlog("相思", "王維的相思", "紅豆生南國,春來發(fā)幾枝,愿君多采截,此物最相思"));
esBlogRepository.save(new EsBlog("靜夜思", "李白的靜夜思", "床前明月光,疑是地上霜,舉頭望明月,低頭思故鄉(xiāng)"));
//查詢獲取
PageRequest pageRequest=PageRequest.of(pageIndex,pageSize);
Page page= esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageRequest);
return page.getContent();
}
}
啟動項(xiàng)目,前臺進(jìn)行訪問:
前臺結(jié)果打印成功,故我們的Elasticsearch+Spring Boot集成成功