關(guān)于政府補(bǔ)貼企業(yè)做網(wǎng)站的事免費(fèi)推廣引流平臺(tái)
文章目錄
- 1 背景
- 2 前言
- 3 快速部署ES
- 4 快速部署Kibana
- 5 發(fā)送請(qǐng)求給ES
- 5.1 打開Kibana控制臺(tái)
- 5.2 通過REST API發(fā)送請(qǐng)求
- 5.3 通過curl發(fā)送請(qǐng)求
- 5.4 添加數(shù)據(jù)
- 5.4.1 添加單個(gè)document
- 5.4.2 添加多個(gè)document
- 5.5 搜索數(shù)據(jù)
- 5.5.1 搜索所有documents
- 5.5.2 `match`查詢
- 6 總結(jié)
1 背景
因電商項(xiàng)目發(fā)展的需要,商品數(shù)量越來越大,用戶搜索的需求越來越高級(jí)(需要支持全文模糊搜索,并且性能要高),因此需要在項(xiàng)目中引入Elasticsearch。筆者0基礎(chǔ),簡單看過一些關(guān)于Elasticsearch的知識(shí)點(diǎn),今天通過Quick start快速切入Elasticsearch。后續(xù)會(huì)展開Elasticsearch的學(xué)習(xí)以及研究。
2 前言
- 筆者通過Elasticsearch的官方文檔來學(xué)習(xí),本文參考自:Quick start
- 建議讀者先通篇閱讀搭建ES的步驟,再回頭仔細(xì)跟著敲命令搭建
3 快速部署ES
官方有提供試用版的ES云服務(wù),筆者試用期已經(jīng)過了,因此今天是通過Dokcer手動(dòng)搭建ES(單節(jié)點(diǎn)集群)。
執(zhí)行以下命令,創(chuàng)建ES容器:
docker network create elastic
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.13.4
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -t docker.elastic.co/elasticsearch/elasticsearch:8.13.4
啟動(dòng)成功后,會(huì)出現(xiàn)以下這段文字,含有Elasticsearch的秘密以及登錄Kibana的token串,需要保存起來,后面會(huì)用到:
? Elasticsearch security features have been automatically configured!
? Authentication is enabled and cluster connections are encrypted.?? Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):84fZ4PuywWr_unOcr+JH?? HTTP CA certificate SHA-256 fingerprint:1ebd4a12d92c70ee71467917d89f10dad1d39aa92935b5f8671af20c38cd542f?? Configure Kibana to use this cluster:
? Run Kibana and click the configuration link in the terminal when Kibana starts.
? Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):eyJ2ZXIiOiI4LjEzLjQiLCJhZHIiOlsiMTcyLjIxLjAuMjo5MjAwIl0sImZnciI6IjFlYmQ0YTEyZDkyYzcwZWU3MTQ2NzkxN2Q4OWYxMGRhZDFkMzlhYTkyOTM1YjVmODY3MWFmMjBjMzhjZDU0MmYiLCJrZXkiOiJPV2FHMG84Qm85S1VKZVNRZkJPRTpyNlpWbl84dVE0MkE1NzN0NWw5aGZ3In0=?? Configure other nodes to join this cluster:
? Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):eyJ2ZXIiOiI4LjEzLjQiLCJhZHIiOlsiMTcyLjIxLjAuMjo5MjAwIl0sImZnciI6IjFlYmQ0YTEyZDkyYzcwZWU3MTQ2NzkxN2Q4OWYxMGRhZDFkMzlhYTkyOTM1YjVmODY3MWFmMjBjMzhjZDU0MmYiLCJrZXkiOiJOMmFHMG84Qm85S1VKZVNRZkJPRDpxc0pkX1pFZ1FQcTJ3ODU5dURfOG1nIn0=If you're running in Docker, copy the enrollment token and run:`docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.13.4`
為方便下面敲命令,我們?cè)赟hell客戶端保存ES的秘密,執(zhí)行以下命令:
export ELASTIC_PASSWORD="your_password"
將SSL證書從容器復(fù)制到本地電腦:
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
驗(yàn)證ES是否有啟動(dòng)成功:
curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
4 快速部署Kibana
新開一個(gè)Shell客戶端session,執(zhí)行以下命令創(chuàng)建Kibana容器:
docker pull docker.elastic.co/kibana/kibana:8.13.4
docker run --name kibana --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.13.4
啟動(dòng)成功后,會(huì)彈出Kibana的訪問地址,復(fù)制到瀏覽器訪問即可,注意,如果Kibana是部署在centos虛擬機(jī)內(nèi),訪問地址要用虛擬機(jī)的ip地址
5 發(fā)送請(qǐng)求給ES
5.1 打開Kibana控制臺(tái)
按照下圖操作,打開Kibana控制臺(tái):
5.2 通過REST API發(fā)送請(qǐng)求
在控制臺(tái)輸入
GET /?pretty
點(diǎn)擊運(yùn)行即可,如下圖所示:
5.3 通過curl發(fā)送請(qǐng)求
新開一個(gè)Shell客戶端的session,發(fā)送以下命令,其中
$ELASTIC_PASSWORD
替換成Elasticsearch的密碼:
curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
5.4 添加數(shù)據(jù)
簡單介紹ES中的數(shù)據(jù)概念:ES中的數(shù)據(jù)被叫做document,類比關(guān)系型數(shù)據(jù)庫中的一行記錄,ES中的數(shù)據(jù)是以JSON結(jié)構(gòu)存儲(chǔ)的。ES將document存在可搜索的索引中
5.4.1 添加單個(gè)document
在Kibana中執(zhí)行以下命令:
POST books/_doc
{"name": "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470}
響應(yīng)報(bào)文的格式如下:
{"_index": "books","_id": "O0lG2IsBaSa7VYx_rEia","_version": 1,"result": "created","_shards": {"total": 2,"successful": 2,"failed": 0},"_seq_no": 0,"_primary_term": 1
}
_id
是document在索引中具有唯一標(biāo)識(shí)的作用,可以類比關(guān)系型數(shù)據(jù)庫中的主鍵ID。
5.4.2 添加多個(gè)document
執(zhí)行以下請(qǐng)求,注意,使用 _bulk 端點(diǎn)在一次請(qǐng)求中添加多個(gè)文檔。批量數(shù)據(jù)必須是換行符分隔的 JSON (NDJSON)。每行都必須以換行符 (\n) 結(jié)尾,包括最后一行。:
POST /_bulk
{ "index" : { "_index" : "books" } }
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
{ "index" : { "_index" : "books" } }
{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}
{ "index" : { "_index" : "books" } }
{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}
{ "index" : { "_index" : "books" } }
{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}
{ "index" : { "_index" : "books" } }
{"name": "The Handmaids Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311}
5.5 搜索數(shù)據(jù)
5.5.1 搜索所有documents
執(zhí)行以下命令:
GET books/_search
響應(yīng)報(bào)文中的_source
就是構(gòu)建索引的時(shí)候原始的JSON對(duì)象
5.5.2 match
查詢
執(zhí)行以下命令,查詢name中帶有
brave
的document:
GET books/_search
{"query": {"match": {"name": "brave"}}
}
6 總結(jié)
- 通過官網(wǎng)的Quick start快速上手實(shí)操ES查詢,使用了Docker部署ES和Kibana,Kibana充當(dāng)提供控制臺(tái)的角色,ES充當(dāng)數(shù)據(jù)庫的角色
- 可以通過2種方式發(fā)送請(qǐng)求:HTTP REST API;CURL命令,本質(zhì)也是HTTP
- ES中有索引概念,索引中含有多個(gè)document,document是由一些JSON Object構(gòu)成的
- 添加document有2個(gè)接口:添加單個(gè)document
POST {索引名}/_doc
;添加多個(gè)documentPOST /bulk
,請(qǐng)求參數(shù)的格式如下:
{ "index" : { "_index" : "books" } }
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
- 搜索數(shù)據(jù)有2個(gè)接口:全量查
GET {索引名}/_search
;match查GET {索引名}/_search
,請(qǐng)求參數(shù)使用如下結(jié)構(gòu):
{"query": {"match": {"name": "brave"}}
}