Springboot + ElasticSearch 構建博客檢索系統


項目效果預覽:

 

 

 

 

 

 

安裝成功的效果圖:

 

 kibana安裝:

 

 

 

 ES使用的是倒排索引

 

 

 

 

 

 


 

 

參考:https://www.yuque.com/gaohanghang/vx5cb2/aa576g#HuZ1N

《Springboot + ElasticSearch 構建博客檢索系統》

視頻地址:https://www.imooc.com/learn/1161

 

代碼地址:https://github.com/gaohanghang/springboot-blog-es

 

我所使用的 elasticsearch、logstash、kibana的版本均為 7.5.0 最新版

 

簡介:從實際需求分析開始,打造個人博客檢索系統。內容涵蓋 ES安裝、ES基本概念和數據類型、Mysql 到 ES 數據同步、SpringBoot 操作 ES。通過本課,讓學員對ES有一個初步認識,理解ES的一些適用場景,以及如何使用springboot來同ES進行交互。

 

第1章 課程介紹

 

1-1 課程導學

 

image.png

 

image.png

 

image.png

 

  • 可以反復看
  • 上手做
  • 學會應用

 

第2章 初識 ElassticSearch

 

2-1 ElasticSearch 概念和適用場景

 

image.png

 

2-2 ElasticSearch 數據類型,和關系型數據庫對比

 

image.png

 

2-3 安裝 ES、postman、Kibana

image.png

 

image.png

 

 

image.png

 

2-4 演示 postman、kibana對ES的交互

 

PostMan

 

Get 查看所有索引  localhost:9200/_all  PUT 創建索引-test  localhost:9200/test   DEL 刪除索引-test  localhost:9200/test   PUT 創建索引-person-1  localhost:9200/person   PUT 新增數據-person-1  localhost:9200/person/_doc/1  {  "first_name" : "John",  "last_name" : "Smith",  "age" : 25,  "about" : "I love to go rock climbing",  "interests" : [ "sports", "music" ] }  PUT 新增數據-person-2  localhost:9200/person/_doc/2  {  "first_name" : "Eric",  "last_name" : "Smith",  "age" : 23,  "about" : "I love basketball",  "interests" : [ "sports", "reading" ] }  GET 搜索數據-person-id  localhost:9200/person/_doc/1  GET 搜索數據-person-name  localhost:9200/person/_doc/_search?q=first_name:john  {  "took": 56,  "timed_out": false,  "_shards": {  "total": 1,  "successful": 1,  "skipped": 0,  "failed": 0  },  "hits": {  "total": {  "value": 1,  "relation": "eq"  },  "max_score": 0.6931472,  "hits": [  {  "_index": "person",  "_type": "_doc",  "_id": "1",  "_score": 0.6931472,  "_source": {  "first_name": "John",  "last_name": "Smith",  "age": 25,  "about": "I love to go rock climbing",  "interests": [  "sports",  "music"  ]  }  }  ]  } } 

 

Kibana


http://localhost:5601/app/kibana

 

GET _search {  "query": {  "match_all": {}  } }   GET _all  GET /person/_doc/1  POST /person/_search {  "query": {  "bool": {  "should": [  {"match": {  "first_name": "Eric"  }  }  ]  } }  POST /person/_search {  "query": {  "bool": {  "should": [  {"match": {  "last_name": "Smith"  }  },  {  "match": {  "about": "basketball"  }  }  ]  }  } }    POST /person/_search {  "query": {  "bool": {  "must": [  {"match": {  "last_name": "Smith"  }  },  {  "match": {  "about": "basketball"  }  }  ]  }  } }

 

image.png

 

image.png

 

第3章 博客網站全文檢索

 

3-1 基於 Mysql 實現

 

image.png

 

CREATE DATABASE blog;  USE blog;  CREATE TABLE `t_blog` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',  `title` varchar(60) DEFAULT NULL COMMENT '博客標題',  `author` varchar(60) DEFAULT NULL COMMENT '博客作者',  `content` mediumtext COMMENT '博客內容',  `create_time` datetime DEFAULT NULL COMMENT '創建時間',  `update_time` datetime DEFAULT NULL COMMENT '更新時間',  PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4   # 自己造的幾條數據 INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (1, 'Springboot 為什么這', 'bywind', '沒錯 Springboot ', '2019-12-08 01:44:29', '2019-12-08 01:44:34'); INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (3, 'Springboot 中 Redis', 'bywind', 'Spring Boot', '2019-12-08 01:44:29', '2019-12-08 01:44:29'); INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (4, 'Springboot 中如何優化', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29'); INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (5, 'Springboot 消息隊列', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29'); INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (6, 'Docker Compose + Springboot', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29');

 

SELECT * FROM t_blog WHERE title LIKE "%spring%" or content LIKE "%spring%"

 

 

image.png

 

3-2 基於ES實現

 

image.png

 

 

 

image.png

 

第4章 Mysql、ES 數據同步

 

4-1 數據同步中間件

 

image.png

 

image.png

image.png

不足:不支持 ES6.X 以上、Mysql 8.X 以上

 

image.png

image.png

image.png

time 標識最大時間

 

4-2 logstash全量、增量同步解決方案

 

https://www.elastic.co/cn/downloads/logstash

 

jar 包下載地址

 

https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.31

 

mysql.conf

 

input{  jdbc{  # jdbc驅動包位置  jdbc_driver_library => "/Users/gaohanghang/software/0ELK7.5/logstash-7.5.0/mysql-connector-java-5.1.31.jar"  # 要使用的驅動包類  jdbc_driver_class => "com.mysql.jdbc.Driver"  # mysql數據庫的連接信息  jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/blog"  # mysql用戶  jdbc_user => "root"  # mysql密碼  jdbc_password => "root"  # 定時任務,多久執行一次查詢,默認一分鍾,如果想要沒有延遲,可以使用 schedule => "* * * * * *"  schedule => "* * * * *"  # 清空上傳的sql_last_value記錄  clean_run => true  # 你要執行的語句  statement => "select * FROM t_blog WHERE update_time > :sql_last_value AND update_time < NOW() ORDER BY update_time desc"  } }  output {  elasticsearch{  # es host : port  hosts => ["127.0.0.1:9200"]  # 索引  index => "blog"  # _id  document_id => "%{id}"  } }

 

 

bin/logstash -f config/mysql.conf

 

會報錯,錯誤信息:

 

Unable to find driver class via URLClassLoader in given driver jars: com.mysql.jdbc.Driver and com.mysql.jdbc.Driver

 

我用的 logstash7.5.0

 

這里只是一個解決方法。只需將驅動程序Jar文件復制到<logstash_install_dir>/logstash-core/lib/jars/目錄。

 

image.png

 

然后刪除conf里的jdbc驅動包配置

 

mysql.conf

 

input{  jdbc{  # 要使用的驅動包類  jdbc_driver_class => "com.mysql.jdbc.Driver"  # mysql數據庫的連接信息  jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/blog"  # mysql用戶  jdbc_user => "root"  # mysql密碼  jdbc_password => "root"  # 定時任務,多久執行一次查詢,默認一分鍾,如果想要沒有延遲,可以使用 schedule => "* * * * * *"  schedule => "* * * * *"  # 清空上傳的sql_last_value記錄  clean_run => true  # 你要執行的語句  statement => "select * FROM t_blog WHERE update_time > :sql_last_value AND update_time < NOW() ORDER BY update_time desc"  } }  output {  elasticsearch{  # es host : port  hosts => ["127.0.0.1:9200"]  # 索引  index => "blog"  # _id  document_id => "%{id}"  } }

成功

 

image.png

 

GET /blog/_stats  GET /blog/_search

 

image.png

 

 

第 5 章

 

5-1 分詞器介紹

 

image.png

 

5-2 IK分詞器的安裝和使用

 

POST _analyze {  "analyzer": "standard",  "text" : "hello imooc" }

 

image.png

 

POST _analyze {  "analyzer": "standard",  "text" : "我是中國人" }

 

image.png

 

ik分詞器下載地址:

 

https://github.com/medcl/elasticsearch-analysis-ik/releases

 

image.png

 

啟動報錯

 

image.png

 

https://github.com/medcl/elasticsearch-analysis-ik/issues/384

 

ES存放文件路徑中不能帶有空格啊!!!!

 

 

image.png

 

 

POST _analyze {  "analyzer": "ik_smart",  "text" : "我是中國人" }

 

image.png

 

POST _analyze {  "analyzer": "ik_max_word",  "text" : "我是中國人" }

 

image.png

 

POST _analyze {  "analyzer": "ik_max_word",  "text" : "我是慕課網" }

 

image.png

 

字典添加慕課網后

 

image.png

 

5-3 springboot 項目搭建

 

image.png

 

server:  port: 8081 spring:  #數據庫配置  datasource:  driver-class-name: com.mysql.cj.jdbc.Driver  url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8  username: root  password: root  # hikari 數據源專用配置  hikari:  maximum-pool-size: 20  minimum-idle: 5   # jpa 相關配置  jpa:  hibernate:  ddl-auto: update  show-sql: true  # 數據庫方言  database-platform: org.hibernate.dialect.MySQLDialect   # es 配置    # mvc 靜態資源映射  mvc:  static-path-pattern: /**   # 靜態資源熱部署  devtools:  livereload:  enabled: true  restart:  additional-paths: static/**   # 日期格式化  jackson:  date-format: yyyy-MM-dd HH:mm:ss

 

代碼地址:https://github.com/gaohanghang/springboot-blog-es

 

5-4 項目結構和JPA演示

image.png

 

 

5-5 springboot 集成ES

 

 

 

 

5-6 項目后端 REST API 實現

 

image.png

 

image.png

 

image.png

第 6 章 

 

6-1 課程回顧與總結

 

image.png

 

image.png

 


免責聲明!

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



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