ES單機(jī)部署
下載es:?
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.5-
linux-x86_64.tar.gz
tar -zvxf elasticsearch-7.17.5-linux-x86_64.tar.gz
配置 Elasticsearch
關(guān)閉防火墻
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
配置elasticsearch.yml
?
#設(shè)置允許訪問地址,配置位0.0.0.0允許任意主機(jī)訪問
- #network.host: 192.168.0.1
+ network.host: 0.0.0.0
# 配置集群
# node.name: node-1
+ node.name: node-1
- #discovery.seed_hosts: ["host1", "host2"]
discovery.seed_hosts: ["node-1"]
- #cluster.initial_master_nodes: ["node-1", "node-2"]
+ cluster.initial_master_nodes: ["node-1"]
修改Linux句柄數(shù)
?
# 查看當(dāng)前最大句柄數(shù)
sysctl -a | grep vm.max_map_count
# 修改句柄數(shù)
vi /etc/sysctl.conf
+ vm.max_map_count=262144
# 臨時(shí)生效
sysctl -w vm.max_map_count=262144
關(guān)閉swap
因?yàn)镋S的數(shù)據(jù)大量都是常駐內(nèi)存的,一旦使用了虛擬內(nèi)存就會(huì)導(dǎo)致查詢速度下降,一般需要關(guān)閉
swap,但是要保證有足夠的內(nèi)存
?
# 臨時(shí)關(guān)閉
swapoff -a
# 永久關(guān)閉
vi /etc/fstab
注釋掉swap的一行
修改最大線程數(shù)及創(chuàng)建ES用戶
vi /etc/security/limits.conf
# 添加以下內(nèi)容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
# 重啟服務(wù)
reboot
# es不能以root啟動(dòng),創(chuàng)建用戶
useradd elasticsearch
passwd elasticsearch
# 增加sudoers權(quán)限
vi /etc/sudoers
+ elasticsearch ALL=(ALL) ALL
# 給ES的安裝目錄進(jìn)行授權(quán)
chown -R elasticsearch:elasticsearch elasticsearch-7.17.5
# jvm配置
vi jvm.options
+ -Xms4g
+ -Xmx4g
添加IK分詞器
在github中下載對(duì)應(yīng)版本的分詞器
https://github.com/infinilabs/analysis-ik/releases
根據(jù)自己的ES版本選擇相應(yīng)版本的IK分詞器,因?yàn)榘惭b的ES是 7.17.5 ,所以也下載相應(yīng)的IK分詞
器
將下載的分詞器復(fù)制到ES安裝目錄的 plugins 目錄中并進(jìn)行解壓
mkdir ik && cd ik
unzip elasticsearch-analysis-ik-7.17.5.zip
# 啟動(dòng)
su elasticsearch
# 前臺(tái)啟動(dòng)
sh bin/elasticsearch
# 后臺(tái)啟動(dòng)
sh bin/elasticsearch -d
訪問主機(jī)的9200端口
http://192.168.101.20:9200
?kibana安裝
下載安裝 Kibana ?
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.5-linuxx86_64.tar.gz
tar -zvxf kibana-7.17.5-linux-x86_64.tar.gz
mv kibana-7.17.5-linux-x86_64 kibana-7.17.5
# 配置kibana
vi config/kibana.yml
- #server.port: 5601
+ server.port: 5601
- #server.host: "localhost"
+ server.host: "0.0.0.0"
- #elasticsearch.hosts: ["http://localhost:9200"]
+ elasticsearch.hosts: ["http://localhost:9200"]
# 啟動(dòng)kibana
su elasticsearch
#前臺(tái)運(yùn)行
sh bin/kibana
#后臺(tái)運(yùn)行
nohup sh bin/kibana >/dev/null 2>&1 &
訪問地址:http://192.168.101.20:5601
ES快速入門
索引管理
列出索引
?
GET /_cat/indices?v
創(chuàng)建索引
查看索引
?
?索引是否存在
?
關(guān)閉索引
?
在一些業(yè)務(wù)場(chǎng)景,我們可能需要禁止掉某些索引的訪問功能,但是又不想刪除這個(gè)索引
刪除索引?
?映射管理
映射的創(chuàng)建時(shí)基于索引的,你必須要先創(chuàng)建索引才能創(chuàng)建映射,es中的映射相當(dāng)于傳統(tǒng)數(shù)據(jù)庫(kù)中
的表結(jié)構(gòu),數(shù)據(jù)存儲(chǔ)的格式就是通過映射來規(guī)定的
創(chuàng)建映射
?
可以在創(chuàng)建索引時(shí)指定映射,其中 mappings.properties 為固定結(jié)構(gòu),指定創(chuàng)建映射屬性
PUT customer
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
查看映射

?
文檔管理?
?創(chuàng)建文檔(業(yè)務(wù)ID)
文檔可以類比為關(guān)系型數(shù)據(jù)庫(kù)中的表數(shù)據(jù),添加的數(shù)據(jù)格式為 JSON 格式
注意需要在索引后面添加 _doc ,表示操作文檔
在未指定id生成情況,每執(zhí)行一次post將生成一個(gè)新文檔
如果index不存在,將會(huì)默認(rèn)創(chuàng)建
新增文檔,自動(dòng)生成文檔id,并且如果如果添加文檔的索引不存在時(shí)會(huì)自動(dòng)創(chuàng)建索引
post customer/_doc/1
{
"name" : "張三",
"age" : 15
}
# 返回結(jié)果
{
"_index" : "customer", #所屬索引
"_type" : "_doc", #所屬mapping type
"_id" : "1", #文檔id
"_version" : 1, #文檔版本
"result" : "created", #文檔創(chuàng)建成功
"_shards" : {
"total" : 2, #所在分片有兩個(gè)分片
"successful" : 1, #只有一個(gè)副本成功寫入,可能節(jié)點(diǎn)機(jī)器只有一臺(tái)
"failed" : 0 #失敗副本數(shù)
},
"_seq_no" : 0, #第幾次操作該文檔
"_primary_term" : 1 #詞項(xiàng)數(shù)
}
?創(chuàng)建文檔(自動(dòng)ID)
自動(dòng)生成的ID是一個(gè)不會(huì)重復(fù)的隨機(jī)數(shù),使用GUID算法,可以保證在分布式環(huán)境下,不同節(jié)點(diǎn)同一時(shí)間創(chuàng)建的 _id 一定是不沖突的
?
post customer/_doc
{
"name" : "李四",
"age" : 50
}
?更新文檔
?全量更新
和新增文檔一樣,輸入相同的 URL 地址請(qǐng)求,如果請(qǐng)求體變化,會(huì)將原有的數(shù)據(jù)內(nèi)容覆蓋
post customer/_doc/1
{
"name" : "張三",
"age" : 50
}
?增量更新
通過指定 _doc 方式默認(rèn)是全量更新,如果需要更新指定字段則需要將 _doc 改為 _update ,請(qǐng)求
內(nèi)容需要增加doc表示,原始 {“key”: value} ,更新 {“doc”: {“key”: value}}
post customer/_update/1
{
"doc": {
"age" : 55
}
}
?查詢文檔
HEAD customer/_doc/1 #查看是否存儲(chǔ),返回200表示已存儲(chǔ)
GET customer/_doc/1 #返回源數(shù)據(jù)的查詢
GET customer/_doc/1?_source=false # 有時(shí)候只是查詢,不需要具體的源文檔字段
GET customer/_search # 查詢所有
?刪除文檔
DELETE customer/_doc/1 #指定文檔id進(jìn)行刪除
# 根據(jù)查詢條件刪除,會(huì)先查詢?nèi)缓笤趧h除,可能耗時(shí)會(huì)比較長(zhǎng)
post customer/_delete_by_query
{
"query": {
"match": {
"age": "15"
}
}
}
?中文分詞器
IKAnalyzer已經(jīng)推出了3個(gè)大版本,在 2012 版本中,IK 實(shí)現(xiàn)了簡(jiǎn)單
的分詞歧義排除算法,標(biāo)志著 IK 分詞器從單純的詞典分詞向模擬語(yǔ)義分詞衍化
IK提供了兩個(gè)分詞算法: ik_smart:最少切分, ik_max_word:最細(xì)粒度劃分
ik_smart ?
原始內(nèi)容:傳智教育的教學(xué)質(zhì)量是杠杠的
GET _analyze
{
"analyzer": "ik_smart",
"text": "傳智教育的教學(xué)質(zhì)量是杠杠的"
}
?
?ik_max_word
GET _analyze
{
"analyzer": "ik_max_word",
"text": "傳智教育的教學(xué)質(zhì)量是杠杠的"
}
自定義詞庫(kù)
我們輸入“傳智教育的教學(xué)質(zhì)量是杠杠的”,但是分詞器會(huì)把“傳智教育”進(jìn)行拆開,分為了“傳”,
“智”,“教育”,但我們希望的是“傳智教育”可以不被拆開。
?進(jìn)入elasticsearch目錄 plugins/ik/config 中,創(chuàng)建我們自己的字典文件 yixin.dic ,并添加
內(nèi)容:文章來源:http://www.zghlxwxcb.cn/news/detail-850738.html
cd plugins/ik/config
echo "傳智教育" > custom.dic
# 進(jìn)入我們的elasticsearch目錄 : plugins/ik/config ,打開 IKAnalyzer.cfg.xml 文件,進(jìn)行
如下配置:
vi IKAnalyzer.cfg.xml
#增加如下內(nèi)容
<entry key="ext_dict">custom.dic</entry>
?重啟ElasticSearch,再次使用kibana測(cè)試.文章來源地址http://www.zghlxwxcb.cn/news/detail-850738.html
GET _analyze
{
"analyzer": "ik_max_word",
"text": "傳智教育的教學(xué)質(zhì)量是杠杠的"
}
到了這里,關(guān)于架構(gòu)師系列-搜索引擎ElasticSearch(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!