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)行選擇
下載完解壓即可
在bin
目錄中雙擊elasticsearch.bat
即可
Elasticsearch啟動(dòng)監(jiān)聽兩個(gè)端口,9300和9200
9300
端口是使用tcp
客戶端連接使用的端口
9200
端口是通過http
協(xié)議連接Elasticsearch
使用的端口
讀取的配置文件是config
目錄下的elasticsearch.yml
建議注冊成系統(tǒng)服務(wù),執(zhí)行命令elasticsearch-service.bat install
配置文件目錄config
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地址,可以是ipv4
或ipv6
的,默認(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/
看到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
如果發(fā)現(xiàn)grunt不是內(nèi)部命令的,在環(huán)境變量path中加入node_global文件夾的路徑
4.在Elasticsearch
配置文件elasticsearch.yml
中加入
http.cors.enabled: true
http.cors.allow-origin: "*"
5.用grunt命令啟動(dòng)elasticsearch-head
插件:grunt server
在瀏覽器訪問http://localhost:9100/
注:如果顯示沒連接可以點(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)行選擇
下載完解壓,到bin
目錄雙擊kibana.bat
啟動(dòng)kibana
監(jiān)聽端口為5601,在瀏覽器中訪問http://localhost:5601
進(jìn)行集群健康檢查
需要使用 _cat
API,在Kibana控制臺(tái)中運(yùn)行命令 GET /_cat/health?v
可以看到名為 elasticsearch
的集群已經(jīng)處于綠色狀態(tài)。
每當(dāng)我們查詢集群健康情況時(shí),接口可能會(huì)返回green
,yellow
或red
狀態(tài)
green
意味著一切良好(集群所有的功能都正常)。
yellow
意味著所有的數(shù)據(jù)都是可用的,但是一些復(fù)制分片可能沒有正確分發(fā)(集群的所有功能還是正常的)
red
意味著因?yàn)槟承┰驅(qū)е掠行?shù)據(jù)不能使用。注意,即使集群狀態(tài)是red,它仍然可以運(yùn)行一部分的功能