全文搜索引擎Elasticsearch

Elasticsearch簡介

Elasticsearch是一個(gè)基于Apache Lucene(TM)的開源、分布式、RESTful接口的全文搜索引擎。Lucene可以被認(rèn)為是迄今為止最先進(jìn)、性能最好的、功能最全的搜索引擎庫。使用Java開發(fā)并使用Lucene作為其核心來實(shí)現(xiàn)所有索引和搜索的功能,Elasticsearch通過簡單的RESTful API來隱藏Lucene的復(fù)雜性,從而讓全文搜索變得簡單。
Github地址:https://github.com/elastic/elasticsearch
官方網(wǎng)站:https://www.elastic.co/products/elasticsearch
英文文檔:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
中文社區(qū):https://elasticsearch.cn/

Elasticsearch特點(diǎn):

1.速度快:可以在極短的時(shí)間內(nèi)存儲(chǔ)、搜索和分析大量的數(shù)據(jù)。通常作為具有復(fù)雜搜索場景情況下的核心發(fā)動(dòng)機(jī)。
2.可拓展性強(qiáng):Elasticsearch還是一個(gè)分布式文檔數(shù)據(jù)庫,其中每個(gè)字段均可被索引,而且每個(gè)字段的數(shù)據(jù)均可被搜索,Elasticsearch能夠橫向擴(kuò)展至數(shù)以百計(jì)的服務(wù)器存儲(chǔ)以及處理PB級的數(shù)據(jù)。
3.彈性,高度可用:Elasticsearch檢測到故障,以確保集群和數(shù)據(jù)安全可用。通過跨集群復(fù)制,輔助集群可以作為熱備份運(yùn)行。
4.靈活性:適用于多種數(shù)據(jù)類型

Elasticsearch下載與安裝

下載地址:https://www.elastic.co/downloads/elasticsearch
根據(jù)自己的操作系統(tǒng)進(jìn)行選擇

image.png

下載完解壓即可
bin目錄中雙擊elasticsearch.bat即可

image.png

Elasticsearch啟動(dòng)監(jiān)聽兩個(gè)端口,9300和9200
9300端口是使用tcp客戶端連接使用的端口
9200端口是通過http協(xié)議連接Elasticsearch使用的端口
讀取的配置文件是config目錄下的elasticsearch.yml

image.png

建議注冊成系統(tǒng)服務(wù),執(zhí)行命令elasticsearch-service.bat install

配置文件目錄config

image.png

elasticsearch.yml 主配置文件
jvm.options jvm參數(shù)配置文件
log4j2.properties 日志配置文件

主配置文件elasticsearch.yml內(nèi)容
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
Cluster設(shè)置集群的

cluster.name集群的名稱,用于區(qū)分不同的集群,系統(tǒng)默認(rèn)為elasitcsearch

Node主要是設(shè)置節(jié)點(diǎn)

node.name節(jié)點(diǎn)名稱
node.attr.rack指定節(jié)點(diǎn)的部落屬性,這是一個(gè)比集群更大的范圍
node.master是否允許作為主節(jié)點(diǎn),默認(rèn)值為true
node.data是否存儲(chǔ)數(shù)據(jù),即存儲(chǔ)索引片段,默認(rèn)值為true

Paths關(guān)于數(shù)據(jù)和日志的存放路徑的,這兩個(gè)設(shè)置十分重要,因?yàn)楸热缫M(jìn)行版本升級,如果程序與數(shù)據(jù)分離,將非常容易實(shí)現(xiàn)。程序的崩潰也不影響數(shù)據(jù)。

path.data數(shù)據(jù)存儲(chǔ)位置
path.logs日志文件的路徑
path.work臨時(shí)文件位置
path.plugins插件安裝位置

Memory內(nèi)存設(shè)置

bootstrap.memory_lock啟動(dòng)后是否鎖定內(nèi)存,提高Elasticsearch的性能

Network有關(guān)網(wǎng)絡(luò)的設(shè)置,比如RESTful接口,包括curl、瀏覽器、Kibana等HTTP連接過來的,都是通過這里設(shè)置

network.host設(shè)置綁定的ip地址,可以是ipv4ipv6的,默認(rèn)本地回環(huán)
http.port設(shè)置對外服務(wù)的http端口,默認(rèn)為9200

Discovery設(shè)置集群的節(jié)點(diǎn)之間的連接的

discovery.zen.ping.unicast.hosts設(shè)置集群內(nèi)節(jié)點(diǎn)的主機(jī)
discovery.zen.minimum_master_nodes設(shè)置一個(gè)集群中主節(jié)點(diǎn)的數(shù)量該值可在2-4之間
discovery.zen.ping.timeout設(shè)置ping其他節(jié)點(diǎn)時(shí)的超時(shí)時(shí)間
discovery.zen.ping.multicast.enabled禁止當(dāng)前節(jié)點(diǎn)發(fā)現(xiàn)多個(gè)集群節(jié)點(diǎn),默認(rèn)值為true

Gateway網(wǎng)關(guān),支持多種類型的Gateway,有本地文件系統(tǒng)(默認(rèn)),分布式文件系統(tǒng),Hadoop的HDFS和amazon的s3云存儲(chǔ)服務(wù)。

gateway.recover_after_nodes設(shè)置集群中N個(gè)節(jié)點(diǎn)啟動(dòng)時(shí)進(jìn)行數(shù)據(jù)恢復(fù),默認(rèn)為1
gateway.recover_after_time設(shè)置初始化恢復(fù)過程的超時(shí)時(shí)間
gateway.expected_nodes設(shè)置該集群中可存在的節(jié)點(diǎn)上限
gateway.type網(wǎng)關(guān)允許在所有集群重啟后持有集群狀態(tài),集群狀態(tài)的變更都會(huì)被保存下來,當(dāng)?shù)谝淮螁⒂眉簳r(shí),可以從網(wǎng)關(guān)中讀取到狀態(tài),默認(rèn)網(wǎng)關(guān)類型(也是推薦的)是local

Various其他

action.destructive_requires_name 在刪除索引時(shí),是否需要明確指定名稱,該值為false時(shí),將可以通過正則或_all進(jìn)行索引 刪除
node.max_local_storage_nodes在一個(gè)系統(tǒng)上禁用啟動(dòng)多個(gè)節(jié)點(diǎn)

詳細(xì)可以參考官方說明:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules.html

啟動(dòng)完在瀏覽器中輸入http://localhost:9200/

image.png

看到Elasticsearch的版本信息即啟動(dòng)成功

Elasticsearch的可視化界面

插件模式:推薦安裝Elasticsearch的head插件

前提:安裝好Node.js和配置好Npm,可以參考:http://www.lxweimin.com/p/96f2f01a4f3e
插件前端代碼的Github地址:https://github.com/mobz/elasticsearch-head
1.把代碼從Github拉到本地
2.用npm對elasticsearch-head進(jìn)行打包:npm install
3.安裝grunt包:npm install –g grunt–cli

image.png

如果發(fā)現(xiàn)grunt不是內(nèi)部命令的,在環(huán)境變量path中加入node_global文件夾的路徑

image.png

4.在Elasticsearch配置文件elasticsearch.yml中加入

http.cors.enabled: true
http.cors.allow-origin: "*"

5.用grunt命令啟動(dòng)elasticsearch-head插件:grunt server

image.png

在瀏覽器訪問http://localhost:9100/
image.png

注:如果顯示沒連接可以點(diǎn)擊連接

官方工具:Elasticsearch常用可視化管理工具kibana

簡介:

Kibana是一個(gè)為Elasticsearch平臺(tái)分析和可視化的開源平臺(tái)

特點(diǎn):

1.通過 Kibana,能夠?qū)?Elasticsearch 中的數(shù)據(jù)進(jìn)行可視化并在 Elastic Stack 進(jìn)行操作
2.一張圖片勝過千萬行日志,Kibana 能夠自由地選擇如何呈現(xiàn)數(shù)據(jù)。Kibana 核心產(chǎn)品搭載了一批經(jīng)典功能:柱狀圖、線狀圖、餅圖、旭日圖,等等。不僅如此,還可以使用 Vega 語法來設(shè)計(jì)獨(dú)屬于自己的可視化圖形。所有這些都利用 Elasticsearch 的完整聚合功能。
3.將地理數(shù)據(jù)融入任何地圖,利用 Elastic Maps Service 來實(shí)現(xiàn)地理空間數(shù)據(jù)的可視化,或者發(fā)揮創(chuàng)意,在地圖上實(shí)現(xiàn)自定義位置數(shù)據(jù)的可視化。
4.時(shí)間序列,借助精選的時(shí)序性 UI,對 Elasticsearch 中的數(shù)據(jù)執(zhí)行高級時(shí)間序列分析。也可以利用功能強(qiáng)大、簡單易學(xué)的表達(dá)式來描述查詢、轉(zhuǎn)換和可視化圖形
4.數(shù)據(jù)分析:憑借搜索引擎的相關(guān)性功能,結(jié)合 Graph 關(guān)聯(lián)分析,揭示 Elasticsearch 數(shù)據(jù)中極其常見的關(guān)系
5.異常情況分析:借助非監(jiān)督型 Machine Learning 功能來檢測隱藏在 Elasticsearch 數(shù)據(jù)中的異常情況并探索那些對它們有顯著影響的屬性。
6.通過 Canvas,發(fā)揮無限創(chuàng)意: Canvas 能夠基于實(shí)時(shí)數(shù)據(jù)發(fā)揮無限創(chuàng)意,而且此功能還支持 SQL

Github地址:https://github.com/elastic/kibana
官方網(wǎng)站:https://www.elastic.co/products/kibana
官方文檔:https://www.elastic.co/guide/en/kibana/current/index.html

下載地址:https://www.elastic.co/downloads/kibana
根據(jù)自己的操作系統(tǒng)進(jìn)行選擇

image.png

下載完解壓,到bin目錄雙擊kibana.bat啟動(dòng)kibana

image.png

監(jiān)聽端口為5601,在瀏覽器中訪問http://localhost:5601

image.png

進(jìn)行集群健康檢查

需要使用 _cat API,在Kibana控制臺(tái)中運(yùn)行命令 GET /_cat/health?v

image.png

可以看到名為 elasticsearch 的集群已經(jīng)處于綠色狀態(tài)。
每當(dāng)我們查詢集群健康情況時(shí),接口可能會(huì)返回greenyellowred狀態(tài)
green意味著一切良好(集群所有的功能都正常)。
yellow意味著所有的數(shù)據(jù)都是可用的,但是一些復(fù)制分片可能沒有正確分發(fā)(集群的所有功能還是正常的)
red意味著因?yàn)槟承┰驅(qū)е掠行?shù)據(jù)不能使用。注意,即使集群狀態(tài)是red,它仍然可以運(yùn)行一部分的功能

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,995評論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,991評論 2 374