1 前言
Elastic Stack 核心產(chǎn)品包括 Elasticsearch【存儲數(shù)據(jù)】、Kibana【展示數(shù)據(jù)】、Beats 和 Logstash【收集與傳輸數(shù)據(jù)】(也稱為 ELK Stack)等等。能夠安全可靠地從任何來源獲取任何格式的數(shù)據(jù),然后對數(shù)據(jù)進行搜索、分析和可視化。sa
Elasticsearch 是一個分布式、RESTful 風(fēng)格的搜索和數(shù)據(jù)分析引擎,能夠解決不斷涌現(xiàn)出的各種用例。作為 Elastic Stack 的核心,Elasticsearch 會集中存儲您的數(shù)據(jù),讓您飛快完成搜索,微調(diào)相關(guān)性,進行強大的分析,并輕松縮放規(guī)模。
2 Elasticsearch 安裝
-
下載安裝包 Download Elasticsearch 下載版本7.8.0,操作系統(tǒng)為Windows
-
下載后解壓,運行bin文件下的elasticsearch.bat命令
-
elasticsearch的服務(wù)端口為 9200 \textcolor{red}{9200} 9200 瀏覽器獲得以下響應(yīng),服務(wù)正常啟動
3 數(shù)據(jù)格式
Elasticsearch 是面向 文檔型 \textcolor{red}{文檔型} 文檔型的存儲數(shù)據(jù)庫。一條數(shù)據(jù)就是一個文檔。為了方便大家理解,我們將Elasticsearch里存儲文檔數(shù)據(jù)和關(guān)系型數(shù)據(jù)庫Mysql存儲數(shù)據(jù)的概念進行一個類別。索引是獨立文檔的集合體。類比于Mysql中的DataBase,Type類別與數(shù)據(jù)庫中的表,在Elasticsearch7.X中Type的概念被刪除了。每個存儲的文檔數(shù)據(jù)就是數(shù)據(jù)庫中每行,文檔中的字段信息對應(yīng)著表的具體欄位。
4 倒排索引
Elasticsearch 使用一種稱為 倒排索引 \textcolor{red}{倒排索引} 倒排索引的結(jié)構(gòu),它適用于快速的全文搜索。一個倒排索引由文檔 中所有不重復(fù)詞的列表構(gòu)成,對于其中每個詞,有一個包含它的文檔列表。
假設(shè)我們有兩個文檔,每個文檔的正文字段包含如下內(nèi)容:
1、The quick brown fox jumped over the lazy dog
2、Quick brown foxes leap over lazy dogs in summer
為了創(chuàng)建倒排索引,我們首先將每個文檔的正文字段,拆分成單獨的詞(我們稱它為詞條或 Tokens),創(chuàng)建一個包含所有不重復(fù)詞條的排序列表,然后列出每個詞條出現(xiàn)在哪個文檔。
詞條 | 文檔 |
---|---|
quick | 文檔1 |
lazy | 文檔1 文檔2 |
brown | 文檔1 文檔2 |
fox | 文檔1 |
leap | 文檔2 |
… | … |
假如我們想想要搜索brown ,我們只需要查找包含每個詞條的倒排索引
詞條 | 文檔 |
---|---|
brown | 文檔1 文檔2 |
我們可以看到兩篇文檔中都包含brown字條,兩篇文章都被檢索出來了。
默認情況下,Elasticsearch 文檔每個字段都會被索引。如果某些字段不需要支持查詢,可以在映射中配置 “index”: false ,減少存儲空間占用,并且提升寫入速度。這部分內(nèi)容我將在后面為大家介紹。
5 常用HTTP請求操作Elasticsearch
5.1 創(chuàng)建索引
類比關(guān)系型數(shù)據(jù)庫,當(dāng)我們操作數(shù)據(jù)時,需要知道是針對哪一個數(shù)據(jù)庫。Index相當(dāng)于關(guān)系型數(shù)據(jù)庫的DB,所以需要創(chuàng)建索引??梢园l(fā)送PUT請求
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
\textcolor{red}{ http://localhost:9200/index1}
http://localhost:9200/index1 去創(chuàng)建名稱為Index1的索引
再次向瀏覽器發(fā)送該put請求時,會被告知異常
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [index1/naVrfF_oQ325n2p60qJCnA] already exists",
"index_uuid": "naVrfF_oQ325n2p60qJCnA",
"index": "index1"
}
],
"type": "resource_already_exists_exception",
"reason": "index [index1/naVrfF_oQ325n2p60qJCnA] already exists",
"index_uuid": "naVrfF_oQ325n2p60qJCnA",
"index": "index1"
},
"status": 400
}
5.2 查詢索引信息
發(fā)送Get請求去獲得指定索引信息
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
\textcolor{red}{http://localhost:9200/index1}
http://localhost:9200/index1
5.3 刪除索引
發(fā)送Delete請求去刪除指定索引信息
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
\textcolor{red}{http://localhost:9200/index1}
http://localhost:9200/index1
5.4 創(chuàng)建/修改文檔
創(chuàng)建文檔的操作,類似與向MySQL數(shù)據(jù)庫中的表插入指定的數(shù)據(jù)信息。與之不同的是,插入的是文檔數(shù)據(jù),傳遞請求的中請求體為Json數(shù)據(jù)
發(fā)送Post請求去創(chuàng)建文檔數(shù)據(jù)
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
/
d
o
c
\textcolor{red}{http://localhost:9200/index1/_doc}
http://localhost:9200/index1/d?oc
index1 指定的文檔請求
_doc 文檔操作
創(chuàng)建文檔成功后可以返回一個標(biāo)識文檔信息的唯一id。當(dāng)創(chuàng)建時不指定id時將會隨機生成一個字符串。也可以在創(chuàng)建文檔時指定文檔的唯一標(biāo)識??梢园l(fā)送Put請求去創(chuàng)建文檔數(shù)據(jù)
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
/
d
o
c
/
101
\textcolor{red}{http://localhost:9200/index1/_doc/101}
http://localhost:9200/index1/d?oc/101 ,指定id為101
PUT 和 POST 請求的區(qū)別:
- POST理解為新增或更新,PUT理解為更新。因此,在PUT中需要指定id。而POST的話,不指定id(ES會自動生成文檔id),指定id(有則修改,無則創(chuàng)建)。
- PUT會將新的json值完全替換掉舊的;而POST方式不傳 _update 參數(shù)新的json值完全替換掉舊的,帶_update 參數(shù)可以更新相同字段的值,其他數(shù)據(jù)不會改變,新提交的字段若不存在則增加。
- PUT操作是冪等的,POST操作不是冪等的。所謂冪等是指不管進行多少次操作,結(jié)果都一樣。比如用PUT修改一篇文章,然后在做同樣的操作,每次操作后的結(jié)果并沒有什么不同,當(dāng)我們多次發(fā)出同樣的POST請求后,其結(jié)果是創(chuàng)建了若干的資源【不指定文檔id的情形】。
再次向Elasticsearch服務(wù)器發(fā)送Put請求時,將會是一個修改文檔的請求
5.5查找文檔
根據(jù)文檔
i
d
查詢指定的文檔數(shù)據(jù)
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
/
d
o
c
/
1001
\textcolor{red}{根據(jù)文檔id查詢指定的文檔數(shù)據(jù)http://localhost:9200/index1/_doc/1001}
根據(jù)文檔id查詢指定的文檔數(shù)據(jù)http://localhost:9200/index1/d?oc/1001
這里示例查詢文檔id為1001的文檔信息
5.6局部修改文檔
文檔的修改分為局部的修改數(shù)據(jù)和完全覆蓋兩種模式,完全覆蓋修改的方式之前在使用PUT接口和POST接口 【攜帶id】 http://localhost:9200/index1/_doc/101已經(jīng)介紹。
局部修改可以發(fā)送Post
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
/
u
p
d
a
t
e
/
101
\textcolor{red}{http://localhost:9200/index1/_update/101 }
http://localhost:9200/index1/u?pdate/101 _update 文檔修改操作
5.7刪除文檔
根據(jù)文檔id 刪除指定的文檔數(shù)據(jù),發(fā)送Delete請求
h
t
t
p
:
/
/
l
o
c
a
l
h
o
s
t
:
9200
/
i
n
d
e
x
1
/
d
o
c
/
101
\textcolor{red}{http://localhost:9200/index1/_doc/101 }
http://localhost:9200/index1/d?oc/101
5.8分頁查詢
Post請求 h t t p : / / l o c a l h o s t : 9200 / i n d e x 1 / s e a r c h \textcolor{red}{ http://localhost:9200/index1/_search } http://localhost:9200/index1/s?earch _search 文檔搜索文章來源:http://www.zghlxwxcb.cn/news/detail-521065.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-521065.html
到了這里,關(guān)于Elasticsearch(1)——倒排索引與HTTP操作Elasticsearch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!