一、ElasticSearch概述 官網:https://www.elastic.co/cn/downloads/elasticsearch Elaticsearch,簡稱為es,es是一個開源的高擴展的分散式全文檢索引擎,它可以近乎實時的存儲、檢索數據;本身擴展性很好,可以擴展到上百台伺服器,處理 ...
一、ElasticSearch概述
Elaticsearch,簡稱為es,es是一個開源的高擴展的分散式全文檢索引擎,它可以近乎實時的存儲、檢索數據;本身擴展性很好,可以擴展到上百台伺服器,處理PB級別(大數據時代)的數據。es也使用java開發並使用Lucene作為其核心來實現所有索引和搜索的功能,但是它的目的是通過簡單的RESTful API來隱藏Lucene的複雜性,從而讓全文搜索變得簡單。
據國際權威的資料庫產品評測機構DB Engines的統計,在2016年1月,ElasticSearch已超過Solr等,成為排名第一的搜索引擎類應用。
總結
1、es基本是開箱即用(解壓就可以用!) ,非常簡單。Solr安裝略微複雜一丟丟!
2、Solr 利用Zookeeper進行分散式管理,而Elasticsearch自身帶有分散式協調管理功能。
3、Solr 支持更多格式的數據,比如JSON、XML、 CSV ,而Elasticsearch僅支持json文件格式。
4、Solr 官方提供的功能更多,而Elasticsearch本身更註重於核心功能,高級功能多有第三方插件提供,例如圖形化界面需要kibana友好支撐
5、Solr 查詢快,但更新索引時慢(即插入刪除慢) ,用於電商等查詢多的應用;
- ES建立索引快(即查詢慢) ,即實時性查詢快,用於facebook新浪等搜索。
- Solr是傳統搜索應用的有力解決方案,但Elasticsearch更適用於新興的實時搜索應用。
6、Solr比較成熟,有一個更大,更成熟的用戶、開發和貢獻者社區,而Elasticsearch相對開發維護者較少,更新太快,學習使用成本較高。
二、ElasticSearch安裝
Windows下安裝
1、安裝
下載地址:https://www.elastic.co/cn/downloads/
歷史版本下載:https://www.elastic.co/cn/downloads/past-releases/
解壓即可(儘量將ElasticSearch相關工具放在統一目錄下)
2、熟悉目錄
bin 啟動文件目錄
config 配置文件目錄
1og4j2 日誌配置文件
jvm.options java 虛擬機相關的配置(預設啟動占1g記憶體,內容不夠需要自己調整)
elasticsearch.ym1 elasticsearch 的配置文件! 預設9200埠!跨域!
1ib 相關jar包
modules 功能模塊目錄
plugins 插件目錄
ik分詞器
3、啟動
bin目錄下的elasticsearch.bat
訪問地址: localhost:9200
{
"name" : "TIANYH",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "IOHRCRK6TKibMGdNZq4YtA",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安裝可視化界面
elasticsearch-head
使用前提:需要安裝nodejs
1、下載地址
https://github.com/mobz/elasticsearch-head
2、安裝
解壓即可(儘量將ElasticSearch相關工具放在統一目錄下)
3、啟動
cd elasticsearch-head
# 安裝依賴npm install
# 啟動npm run start#
# 訪問http://localhost:9100/
開啟跨域(在elasticsearch解壓目錄config下elasticsearch.yml中添加)
# 開啟跨域http.cors.enabled: true
# 所有人訪問http.cors.allow-origin: "*"
重啟elasticsearch
理解:
- 如果你是初學者
- 索引 可以看做 “資料庫”
- 類型 可以看做 “表”
- 文檔 可以看做 “庫中的數據(表中的行)”
- 這個head,我們只是把它當做可視化數據展示工具,之後所有的查詢都在kibana中進行
- 因為不支持json格式化,不方便
安裝kibana
Kibana是一個針對ElasticSearch的開源分析及可視化平臺,用來搜索、查看交互存儲在Elasticsearch索引中的數據。使用Kibana ,可以通過各種圖表進行高級數據分析及展示。Kibana讓海量數據更容易理解。它操作簡單,基於瀏覽器的用戶界面可以快速創建儀錶板( dashboard )實時顯示Elasticsearch查詢動態。設置Kibana非常簡單。無需編碼或者額外的基礎架構,幾分鐘內就可以完成Kibana安裝並啟動Elasticsearch索引監測。
1、下載地址:
下載的版本需要與ElasticSearch版本對應
https://www.elastic.co/cn/downloads/
歷史版本下載:https://www.elastic.co/cn/downloads/past-releases/
2、安裝
解壓即可(儘量將ElasticSearch相關工具放在統一目錄下)
3、啟動
bin目錄下的kibanan.bat
訪問地址: localhost:5601
4、kibana漢化
編輯器打開kibana解壓目錄/config/kibana.yml
,添加
i18n.locale: "zh-CN"
重啟kibana
瞭解ELK
-
ELK是
Elasticsearch、Logstash、 Kibana三大開源框架首字母大寫簡稱
。市面上也被成為Elastic Stack。
- 其中Elasticsearch是一個基於Lucene、分散式、通過Restful方式進行交互的近實時搜索平臺框架。
- 像類似百度、谷歌這種大數據全文搜索引擎的場景都可以使用Elasticsearch作為底層支持框架,可見Elasticsearch提供的搜索能力確實強大,市面上很多時候我們簡稱Elasticsearch為es。
- Logstash是ELK的中央數據流引擎,用於從不同目標(文件/數據存儲/MQ )收集的不同格式數據,經過過濾後支持輸出到不同目的地(文件/MQ/redis/elasticsearch/kafka等)。
- Kibana可以將elasticsearch的數據通過友好的頁面展示出來 ,提供實時分析的功能。
- 其中Elasticsearch是一個基於Lucene、分散式、通過Restful方式進行交互的近實時搜索平臺框架。
-
市面上很多開發只要提到ELK能夠一致說出它是一個日誌分析架構技術棧總稱 ,但實際上ELK不僅僅適用於日誌分析,它還可以支持其它任何數據分析和收集的場景,日誌分析和收集只是更具有代表性。並非唯一性。
收集清洗數據(Logstash) ==> 搜索、存儲(ElasticSearch) ==> 展示(Kibana)
三、ElasticSearch核心概念
概述
1、索引(ElasticSearch)
- 包多個分片
2、欄位類型(映射)
- 欄位類型映射(欄位是整型,還是字元型…)
3、文檔
4、分片(Lucene索引,倒排索引)
ElasticSearch是面向文檔,關係行資料庫和ElasticSearch客觀對比!一切都是JSON!
Relational DB | ElasticSearch |
---|---|
資料庫(database) | 索引(indices) |
表(tables) | types <慢慢會被棄用!> |
行(rows) | documents |
欄位(columns) | fields |
elasticsearch(集群)中可以包含多個索引(資料庫) ,每個索引中可以包含多個類型(表) ,每個類型下又包含多個文檔(行) ,每個文檔中又包含多個欄位(列)。
物理設計:
elasticsearch在後臺把每個索引劃分成多個分片,每分分片可以在集群中的不同伺服器間遷移
一個人就是一個集群! ,即啟動的ElasticSearch服務,預設就是一個集群,且預設集群名為elasticsearch
邏輯設計:
一個索引類型中,包含多個文檔,比如說文檔1,文檔2。當我們索引一篇文檔時,可以通過這樣的順序找到它:索引 => 類型 => 文檔ID ,通過這個組合我們就能索引到某個具體的文檔。 註意:ID不必是整數,實際上它是個字元串。
文檔(”行“)
之前說elasticsearch是面向文檔的,那麼就意味著索引和搜索數據的最小單位是文檔,elasticsearch中,文檔有幾個重要屬性:
- 自我包含,一篇文檔同時包含欄位和對應的值,也就是同時包含key:value !
- 可以是層次型的,一個文檔中包含自文檔,複雜的邏輯實體就是這麼來的!
- 靈活的結構,文檔不依賴預先定義的模式,我們知道關係型資料庫中,要提前定義欄位才能使用,在elasticsearch中,對於欄位是非常靈活的,有時候,我們可以忽略該欄位,或者動態的添加一個新的欄位。
儘管我們可以隨意的新增或者忽略某個欄位,但是,每個欄位的類型非常重要,比如一個年齡欄位類型,可以是字元串也可以是整形。因為elasticsearch會保存欄位和類型之間的映射及其他的設置。這種映射具體到每個映射的每種類型,這也是為什麼在elasticsearch中,類型有時候也稱為映射類型。
類型(“表”)
類型是文檔的邏輯容器,就像關係型資料庫一樣,表格是行的容器。類型中對於欄位的定義稱為映射,比如name映射為字元串類型。我們說文檔是無模式的,它們不需要擁有映射中所定義的所有欄位,比如新增一個欄位,那麼elasticsearch是怎麼做的呢?
- elasticsearch會自動的將新欄位加入映射,但是這個欄位的不確定它是什麼類型,elasticsearch就開始猜,如果這個值是18,那麼elasticsearch會認為它是整形。但是elasticsearch也可能猜不對,所以最安全的方式就是提前定義好所需要的映射,這點跟關係型資料庫殊途同歸了,先定義好欄位,然後再使用,別整什麼么蛾子。
索引(“庫”)
索引是映射類型的容器, elasticsearch中的索引是一個非常大的文檔集合。 索引存儲了映射類型的欄位和其他設置。然後它們被存儲到了各個分片上了。我們來研究下分片是如何工作的。
一個集群至少有一個節點,而一個節點就是一個elasricsearch進程,節點可以有多個索引預設的,如果你創建索引,那麼索引將會有個5個分片(primary shard ,又稱主分片)構成的,每一個主分片會有一個副本(replica shard,又稱複製分片)
有3個節點的集群,可以看到主分片和對應的複製分片都不會在同一個節點內,這樣有利於某個節點掛掉了,數據也不至於失。實際上,一個分片是一個Lucene索引(一個ElasticSearch索引包含多個Lucene索引) ,一個包含倒排索引的文件目錄,倒排索引的結構使得elasticsearch在不掃描全部文檔的情況下,就能告訴你哪些文檔包含特定的關鍵字。不過,等等,倒排索引是什麼鬼?
倒排索引(Lucene索引底層)
簡單說就是 按(文章關鍵字,對應的文檔<0個或多個>)形式建立索引,根據關鍵字就可直接查詢對應的文檔(含關鍵字的),無需查詢每一個文檔,如下圖
四、IK分詞器(elasticsearch插件)
IK分詞器:中文分詞器
分詞:即把一段中文或者別的劃分成一個個的關鍵字,我們在搜索時候會把自己的信息進行分詞,會把資料庫中或者索引庫中的數據進行分詞,然後進行一一個匹配操作,預設的中文分詞是將每個字看成一個詞(不使用用IK分詞器的情況下),比如“我愛狂神”會被分為”我”,”愛”,”狂”,”神” ,這顯然是不符合要求的,所以我們需要安裝中文分詞器ik來解決這個問題。
IK提供了兩個分詞演算法: ik_smart
和ik_max_word
,其中ik_smart
為最少切分, ik_max_word
為最細粒度劃分!
1、下載
版本要與ElasticSearch版本對應
下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
2、安裝
ik文件夾是自己創建的
加壓即可(但是我們需要解壓到ElasticSearch的plugins目錄ik文件夾下)
4、使用 ElasticSearch安裝補錄/bin/elasticsearch-plugin
可以查看插件
E:\ElasticSearch\elasticsearch-7.6.1\bin>elasticsearch-plugin list
5、使用kibana測試
ik_smart
:最少切分
GET _analyze
{
"analyzer": "ik_smart",
"text": "白日依山盡黃河入海流"
}
{
"tokens" : [
{
"token" : "白日",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "依",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "山",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "盡",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "黃河",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "入海流",
"start_offset" : 7,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 5
}
]
}
ik_max_word
:最細粒度劃分(窮盡詞庫的可能)
GET _analyze
{
"analyzer": "ik_max_word",
"text": "白日依山盡黃河入海流"
}
{
"tokens" : [
{
"token" : "白日",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "依",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "山",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "盡",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "黃河",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "入海流",
"start_offset" : 7,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "入海",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "海流",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 7
}
]
}
6、添加自定義的詞添加到擴展字典中
elasticsearch目錄/plugins/ik/config/IKAnalyzer.cfg.xml
打開 IKAnalyzer.cfg.xml
文件,擴展字典
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!--用戶可以在這裡配置自己的擴展字典 -->
<entry key="ext_dict">my.dic</entry>
<!--用戶可以在這裡配置自己的擴展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶可以在這裡配置遠程擴展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用戶可以在這裡配置遠程擴展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
編寫 my.dic
白日依山盡
黃河入海流
GET _analyze
{
"analyzer": "ik_smart",
"text": "白日依山盡黃河入海流"
}
{
"tokens" : [
{
"token" : "白日依山盡",
"start_offset" : 0,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "黃河入海流",
"start_offset" : 5,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 1
}
]
}
五、Rest風格說明
一種軟體架構風格,而不是標準,只是提供了一組設計原則和約束條件。它主要用於客戶端和伺服器交互類的軟體。基於這個風格設計的軟體可以更簡潔,更有層次,更易於實現緩存等機制。
基本Rest命令說明:
method | url地址 | 描述 |
---|---|---|
PUT(創建,修改) | localhost:9200/索引名稱/類型名稱/文檔id | 創建文檔(指定文檔id) |
POST(創建) | localhost:9200/索引名稱/類型名稱 | 創建文檔(隨機文檔id) |
POST(修改) | localhost:9200/索引名稱/類型名稱/文檔id/_update | 修改文檔 |
DELETE(刪除) | localhost:9200/索引名稱/類型名稱/文檔id | 刪除文檔 |
GET(查詢) | localhost:9200/索引名稱/類型名稱/文檔id | 查詢文檔通過文檔ID |
POST(查詢) | localhost:9200/索引名稱/類型名稱/文檔id/_search | 查詢所有數據 |
測試
1、創建一個索引,添加
PUT /test/type/1
{
"name": "測試",
"age": 18
}
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
2、欄位數據類型
-
字元串類型
-
text、
keyword
- text:支持分詞,全文檢索,支持模糊、精確查詢,不支持聚合,排序操作;text類型的最大支持的字元長度無限制,適合大欄位存儲;
- keyword:不進行分詞,直接索引、支持模糊、支持精確匹配,支持聚合、排序操作。keyword類型的最大支持的長度為——32766個UTF-8類型的字元,可以通過設置ignore_above指定自持字元長度,超過給定長度後的數據將不被索引,無法通過term精確匹配檢索返回結果。
-
-
數值型
- long、Integer、short、byte、double、float、half float、scaled float
-
日期類型
- date
-
te布爾類型
- boolean
-
二進位類型
- binary
-
等等…
3、指定欄位的類型(使用PUT)
類似於建庫(建立索引和欄位對應類型),也可看做規則的建立
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test2"
}
4、獲取3建立的規則
GET test2
{
"test2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1676438148562",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "d-qUkOZKQJKzd68KHiN_pw",
"version" : {
"created" : "7060199"
},
"provided_name" : "test2"
}
}
}
}
5、獲取預設信息
_doc
預設類型(default type),type 在未來的版本中會逐漸棄用,因此產生一個預設類型進行代替
PUT /test3/_doc/1
{
"name": "黃河",
"age": 18
}
{
"_index" : "test3",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
GET test3
{
"test3" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1676438576004",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "QmHErZuzSvmczgtgyzC7oA",
"version" : {
"created" : "7060199"
},
"provided_name" : "test3"
}
}
}
}
如果自己的文檔欄位沒有被指定,那麼ElasticSearch就會給我們預設配置欄位類型
擴展:通過GET _cat/
可以獲取ElasticSearch的當前的很多信息!
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
6、修改
兩種方案
①舊的(使用put覆蓋原來的值)
- 版本+1(_version)
- 但是如果漏掉某個欄位沒有寫,那麼更新是沒有寫的欄位 ,會消失
PUT /test/type/1
{
"name": "測試",
"age": 19
}
GET /test/_doc/1
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "測試",
"age" : 19
}
}
PUT /test/type/1
{
"age": 20
}
GET /test/_doc/1
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"age" : 20
}
}
②新的(使用post的update)
- version不會改變
- 需要註意doc
- 不會丟失欄位
POST /test/_doc/1/_update
{
"doc":{
"age":11
}
}
GET /test/_doc/1
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "測試",
"age" : 11
}
}
7、刪除
DELETE /test
{
"acknowledged" : true
}
8、查詢(簡單條件)
GET /test/_doc/_search?q=age:19
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "測試",
"age" : 19
}
}
]
}
}
9、複雜查詢
①查詢匹配
match
:匹配(會使用分詞器解析(先分析文檔,然後進行查詢))_source
:過濾欄位sort
:排序form
、size
分頁
GET /test/_doc/_search
{
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "測試",
"age" : 19
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "小李",
"age" : 19
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "小張",
"age" : 18
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "小明",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "明明",
"age" : 16
}
}
]
}
}
GET /test/_doc/_search
{
"query":{
"match":{
"name":"明"
}
},
"_source":["age","name"],
"sort":[{"age":{"order":"asc"}}],
"from":0,
"size":20
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"name" : "小明",
"age" : 16
},
"sort" : [
16
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "明明",
"age" : 16
},
"sort" : [
16
]
}
]
}
}
②多條件查詢(bool)
must
相當於and
should
相當於or
must_not
相當於not (... and ...)
filter
過濾
GET /test/_doc/_search
{
"query":{
"bool":{
"must":[{"match":{"age":16}},{"match":{"name":"小"}}],
"filter":{
"range":{
"age":{
"gte":15,
"lte":17
}
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.2940125,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.2940125,
"_source" : {
"name" : "小明",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.2940125,
"_source" : {
"name" : "小黃",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.2940125,
"_source" : {
"name" : "小黑",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.2940125,
"_source" : {
"name" : "小花",
"age" : 16
}
}
]
}
}
③匹配數組
- 貌似不能與其它欄位一起使用
- 可以多關鍵字查(空格隔開)— 匹配欄位也是符合的
match
會使用分詞器解析(先分析文檔,然後進行查詢)- 搜詞
GET /test/_doc/_search
{
"query":{
"match":{
"name":"明 黑"
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.9388659,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.9388659,
"_source" : {
"name" : "小黑",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.4651942,
"_source" : {
"name" : "明明",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0729234,
"_source" : {
"name" : "小明",
"age" : 16
}
}
]
}
}
④精確查詢
term
直接通過 倒排索引 指定詞條查詢- 適合查詢 number、date、keyword ,不適合text
GET /test/_doc/_search
{
"query":{
"term":{
"age":16
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "小明",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "明明",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "小黃",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"name" : "小黑",
"age" : 16
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"name" : "小花",
"age" : 16
}
}
]
}
}
⑤text和keyword
- text:
- 支持分詞,全文檢索、支持模糊、精確查詢,不支持聚合,排序操作;
- text類型的最大支持的字元長度無限制,適合大欄位存儲;
- keyword:
- 不進行分詞,直接索引、支持模糊、支持精確匹配,支持聚合、排序操作。
- keyword類型的最大支持的長度為——32766個UTF-8類型的字元,可以通過設置ignore_above指定自持字元長度,超過給定長度後的數據將不被索引,無法通過term精確匹配檢索返回結果。
// 設置索引類型
PUT /test2
{
"mappings": {
"properties": {
"text":{
"type":"text"
},
"keyword":{
"type":"keyword"
}
}
}
}
// 設置欄位數據
PUT /test2/_doc/1
{
"text":"測試keyword和text是否支持分詞",
"keyword":"測試keyword和text是否支持分詞"
}
GET /test2/_doc/_search
{
"query":{
"match":{
"text":"測試"
}
}
}
{
"took" : 426,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"text" : "測試keyword和text是否支持分詞",
"keyword" : "測試keyword和text是否支持分詞"
}
}
]
}
}
GET /test2/_doc/_search
{
"query":{
"match":{
"keyword":"測試"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
GET _analyze
{
"analyzer": "keyword",
"text": ["白日依山盡"]
}
{
"tokens" : [
{
"token" : "白日依山盡",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 0
}
]
}
GET _analyze
{
"analyzer": "standard",
"text": ["白日依山盡"]
}
{
"tokens" : [
{
"token" : "白",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "日",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "依",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "山",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "盡",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
}
]
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": ["白日依山盡"]
}
{
"tokens" : [
{
"token" : "白日依山盡",
"start_offset" : 0,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "白日",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "依",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "山",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "盡",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 4
}
]
}
⑥高亮查詢
GET /test/_doc/_search
{
"query":{
"match":{"name":"小"}
},
"highlight":{
"fields":{
"name":{}
}
}
}
{
"took" : 89,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 0.18681718,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.18681718,
"_source" : {
"name" : "小李",
"age" : 19
},
"highlight" : {
"name" : [
"<em>小</em>李"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.18681718,
"_source" : {
"name" : "小張",
"age" : 18
},
"highlight" : {
"name" : [
"<em>小</em>張"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.18681718,
"_source" : {
"name" : "小明",
"age" : 16
},
"highlight" : {
"name" : [
"<em>小</em>明"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : 0.18681718,
"_source" : {
"name" : "小黃",
"age" : 16
},
"highlight" : {
"name" : [
"<em>小</em>黃"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : 0.18681718,
"_source" : {
"name" : "小黑",
"age" : 16
},
"highlight" : {
"name" : [
"<em>小</em>黑"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : 0.18681718,
"_source" : {
"name" : "小花",
"age" : 16
},
"highlight" : {
"name" : [
"<em>小</em>花"
]
}
}
]
}
}
GET /test/_doc/_search
{
"query":{
"match":{"name":"小"}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 0.18681718,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.18681718,
"_source" : {
"name" : "小李",
"age" : 19
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>李"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.18681718,
"_source" : {
"name" : "小張",
"age" : 18
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>張"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.18681718,
"_source" : {
"name" : "小明",
"age" : 16
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>明"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : 0.18681718,
"_source" : {
"name" : "小黃",
"age" : 16
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>黃"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : 0.18681718,
"_source" : {
"name" : "小黑",
"age" : 16
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>黑"
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : 0.18681718,
"_source" : {
"name" : "小花",
"age" : 16
},
"highlight" : {
"name" : [
"<p class='key' style='color:red'>小</p>花"
]
}
}
]
}
}
六、SpringBoot整合
1、導入依賴
導入elasticsearch
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
提前導入fastjson、lombok
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<!-- lombok需要安裝插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2、創建並編寫配置類
@Configuration
public class ElasticSearchConfig {
// 註冊 rest高級客戶端
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost",9200,"http")
)
);
return client;
}
}
3、創建並編寫實體類
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -3843548915035470817L;
private String name;
private Integer age;
}
4、測試
註入 RestHighLevelClient
@Autowired
public RestHighLevelClient restHighLevelClient;
索引的操作
1、索引的創建
public void CreatIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("test6");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
System.out.println(response);
restHighLevelClient.close();
return ;
}
2、索引的獲取,並判斷其是否存在
public void IndexIsExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("test6");
boolean exists = restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
System.out.println(exists);
restHighLevelClient.close();
return;
}
3、索引的刪除
public void DeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test6");
AcknowledgedResponse response = restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
restHighLevelClient.close();
return;
}
文檔的操作
1、文檔的添加
public void AddDocument() throws IOException {
User user = new User("笑笑",25);
IndexRequest request = new IndexRequest("test");
request.id("16");
request.timeout(TimeValue.timeValueMillis(1000));
request.source(JSON.toJSONString(user),XContentType.JSON);
IndexResponse response = restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(response.status());
System.out.println(response);
restHighLevelClient.close();
return;
}
2、文檔信息的獲取
public void GetDocument() throws IOException {
GetRequest request = new GetRequest("test","1");
GetResponse response = restHighLevelClient.get(request,RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
restHighLevelClient.close();
return;
}
3、文檔的獲取,並判斷其是否存在
public void DocumentIsExists() throws IOException {
GetRequest request = new GetRequest("test","1111");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request,RequestOptions.DEFAULT);
System.out.println(exists);
restHighLevelClient.close();
return;
}
4、文檔的更新
public void UpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("test","16");
User user = new User("黑黑",18);
request.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request,RequestOptions.DEFAULT);
System.out.println(response.status());
restHighLevelClient.close();
return;
}
5、文檔的刪除
public void DeleteDocument() throws Exception {
DeleteRequest request = new DeleteRequest("test","1");
request.timeout("1s");
DeleteResponse response = restHighLevelClient.delete(request,RequestOptions.DEFAULT);
System.out.println(response.status());
restHighLevelClient.close();
}
6、文檔的查詢
public void Search() throws Exception {
SearchRequest request = new SearchRequest("test");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name","明");
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.highlighter(new HighlightBuilder());
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchSourceBuilder.query(termQueryBuilder);
// searchSourceBuilder.query(matchAllQueryBuilder);
searchSourceBuilder.from(0);
searchSourceBuilder.size(100);
request.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(JSON.toJSONString(hits));
System.out.println("++++++++++++++++++++++++++++++++++++++++");
for (SearchHit documentFields: hits.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
restHighLevelClient.close();
}
錯誤的批量添加數據
public void test() throws Exception {
IndexRequest request = new IndexRequest("bulk");
request.source(JSON.toJSONString(new User("小1",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小2",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小3",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小4",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小5",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小6",12)),XContentType.JSON);
request.source(JSON.toJSONString(new User("小7",12)),XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(indexResponse.status());
restHighLevelClient.close();
}
7、批量添加數據
public void testBullk() throws Exception {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("小1",12));
users.add(new User("小2",12));
users.add(new User("小3",12));
users.add(new User("小4",12));
users.add(new User("小5",12));
users.add(new User("小6",12));
for (User user:users) {
bulkRequest.add(new IndexRequest("bulk").source(JSON.toJSONString(user),XContentType.JSON));
}
BulkResponse response = restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println(response.status());
restHighLevelClient.close();
}
七、ElasticSearch實戰
防京東商城搜索(高亮)
1、導入依賴
<dependencies>
<!-- jsoup解析頁面 -->
<!-- 解析網頁 爬視頻可 研究tiko -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<!-- ElasticSearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- devtools熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok 需要安裝插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、導入前端素材
ES資料地址:鏈接:https://pan.baidu.com/s/1qdvSk7SdVnlI8QzeK5gxaA
提取碼:ldrh
3、編寫 application.preperties
配置文件
# 更改埠,防止衝突
server.port=9999
# 關閉thymeleaf緩存
spring.thymeleaf.cache=false
4、測試controller和view
@Controller
public class DemoApi {
@GetMapping({"/","index"})
public String index(){
return "index";
}
}
5、編寫service
ContentService
@Service
public class ContentService {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 1、解析數據放入 es 索引中
public Boolean parseContent(String keyword) throws IOException {
// 獲取內容
List<Content> contents = HtmlParseUtil.parseJD(keyword);
// 內容放入 es 中
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("2m"); // 可更具實際業務是指
for (int i = 0; i < contents.size(); i++) {
bulkRequest.add(
new IndexRequest("jd_goods")
.id(""+(i+1))
.source(JSON.toJSONString(contents.get(i)), XContentType.JSON)
);
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
// restHighLevelClient.close();
return !bulk.hasFailures();
}
// 2、根據keyword分頁查詢結果
public List<Map<String, Object>> search(String keyword, Integer pageIndex, Integer pageSize) throws IOException {
if (pageIndex < 0){
pageIndex = 0;
}
SearchRequest jd_goods = new SearchRequest("jd_goods");
// 創建搜索源建造者對象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 條件採用:精確查詢 通過keyword查欄位name
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", keyword);
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));// 60s
// 分頁
searchSourceBuilder.from(pageIndex);
searchSourceBuilder.size(pageSize);
// 高亮
// ....
// 搜索源放入搜索請求中
jd_goods.source(searchSourceBuilder);
// 執行查詢,返回結果
SearchResponse searchResponse = restHighLevelClient.search(jd_goods, RequestOptions.DEFAULT);
// restHighLevelClient.close();
// 解析結果
SearchHits hits = searchResponse.getHits();
List<Map<String,Object>> results = new ArrayList<>();
for (SearchHit documentFields : hits.getHits()) {
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
results.add(sourceAsMap);
}
// 返回查詢的結果
return results;
}
// 3、 在2的基礎上進行高亮查詢
public List<Map<String, Object>> highlightSearch(String keyword, Integer pageIndex, Integer pageSize) throws IOException {
SearchRequest searchRequest = new SearchRequest("jd_goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 精確查詢,添加查詢條件
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", keyword);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchSourceBuilder.query(termQueryBuilder);
// 分頁
searchSourceBuilder.from(pageIndex);
searchSourceBuilder.size(pageSize);
// 高亮 =========
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
searchSourceBuilder.highlighter(highlightBuilder);
// 執行查詢
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// 解析結果 ==========
SearchHits hits = searchResponse.getHits();
List<Map<String, Object>> results = new ArrayList<>();
for (SearchHit documentFields : hits.getHits()) {
// 使用新的欄位值(高亮),覆蓋舊的欄位值
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
// 高亮欄位
Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();
HighlightField name = highlightFields.get("name");
// 替換
if (name != null){
Text[] fragments = name.fragments();
StringBuilder new_name = new StringBuilder();
for (Text text : fragments) {
new_name.append(text);
}
sourceAsMap.put("name",new_name.toString());
}
results.add(sourceAsMap);
}
return results;
}
}
6、編寫controller
@Controller
public class DemoApi {
@GetMapping({"/","index"})
public String index(){
return "index";
}
@Autowired
private ContentService contentService;
@ResponseBody
@GetMapping("/parse/{keyword}")
public Boolean parse(@PathVariable("keyword") String keyword) throws IOException {
return contentService.parseContent(keyword);
}
@ResponseBody
@GetMapping("/search/{keyword}/{pageIndex}/{pageSize}")
public List<Map<String, Object>> parse(@PathVariable("keyword") String keyword,
@PathVariable("pageIndex") Integer pageIndex,
@PathVariable("pageSize") Integer pageSize) throws IOException {
return contentService.search(keyword,pageIndex,pageSize);
}
@ResponseBody
@GetMapping("/h_search/{keyword}/{pageIndex}/{pageSize}")
public List<Map<String, Object>> highlightParse(@PathVariable("keyword") String keyword,
@PathVariable("pageIndex") Integer pageIndex,
@PathVariable("pageSize") Integer pageSize) throws IOException {
return contentService.highlightSearch(keyword,pageIndex,pageSize);
}
}
7、爬蟲(jsoup)
HtmlParseUtil
public class HtmlParseUtil {
public static void main(String[] args) throws IOException {
/// 使用前需要聯網
// 請求url
String url = "http://search.jd.com/search?keyword=java";
// 1.解析網頁(jsoup 解析返回的對象是瀏覽器Document對象)
Document document = Jsoup.parse(new URL(url), 30000);
// 使用document可以使用在js對document的所有操作
// 2.獲取元素(通過id)
Element j_goodsList = document.getElementById("J_goodsList");
// 3.獲取J_goodsList ul 每一個 li
Elements lis = j_goodsList.getElementsByTag("li");
// 4.獲取li下的 img、price、name
for (Element li : lis) {
String img = li.getElementsByTag("img").eq(0).attr("src");// 獲取li下 第一張圖片
String name = li.getElementsByClass("p-name").eq(0).text();
String price = li.getElementsByClass("p-price").eq(0).text();
System.out.println("=======================");
System.out.println("img : " + img);
System.out.println("name : " + name);
System.out.println("price : " + price);
}
}
public static List<Content> parseJD(String keyword) throws IOException {
/// 使用前需要聯網
// 請求url
String url = "http://search.jd.com/search?keyword=" + keyword;
// 1.解析網頁(jsoup 解析返回的對象是瀏覽器Document對象)
Document document = Jsoup.parse(new URL(url), 30000);
// 使用document可以使用在js對document的所有操作
// 2.獲取元素(通過id)
Element j_goodsList = document.getElementById("J_goodsList");
// 3.獲取J_goodsList ul 每一個 li
Elements lis = j_goodsList.getElementsByTag("li");
// System.out.println(lis);
// 4.獲取li下的 img、price、name
// list存儲所有li下的內容
List<Content> contents = new ArrayList<Content>();
for (Element li : lis) {
// 由於網站圖片使用懶載入,將src屬性替換為data-lazy-img
String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");// 獲取li下 第一張圖片
String name = li.getElementsByClass("p-name").eq(0).text();
String price = li.getElementsByClass("p-price").eq(0).text();
// 封裝為對象
Content content = new Content(name,img,price);
// 添加到list中
contents.add(content);
}
System.out.println(contents);
// 5.返回 list
return contents;
}
}
Content
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content implements Serializable {
private static final long serialVersionUID = -8049497962627482693L;
private String name;
private String img;
private String price;
}
8、前後端分離
引入js
<script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
修改後的index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<title>狂神說Java-ES仿京東實戰</title>
<link rel="stylesheet" th:href="@{/css/style.css}"/>
<script th:src="@{/js/jquery.min.js}"></script>
</head>
<body class="pg">
<div class="page">
<div id="app" class=" mallist tmall- page-not-market ">
<!-- 頭部搜索 -->
<div id="header" class=" header-list-app">
<div class="headerLayout">
<div class="headerCon ">
<!-- Logo-->
<h1 id="mallLogo">
<img th:src="@{/images/jdlogo.png}" alt="">
</h1>
<div class="header-extra">
<!--搜索-->
<div id="mallSearch" class="mall-search">
<form name="searchTop" class="mallSearch-form clearfix">
<fieldset>
<legend>天貓搜索</legend>
<div class="mallSearch-input clearfix">
<div class="s-combobox" id="s-combobox-685">
<div class="s-combobox-input-wrap">