Elasticsearch學習系列一
Elasticsearch
Elasticsearch(以下簡稱ES)是一款Java語言開發的基於Lucene的高效全文搜索引擎。它提供了一個分布式多用戶能力的基於RESTful web接口的全文搜索和分析服務,並作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠實現實時搜索,可以搜索日誌或者交易數據,用來分析商業趨勢、搜集日誌、分析系統瓶頸或者運行發展等等,可以提供預警功能(持續的查詢分析某個數據,如果超過一定的值,就進行警告),分析商業信息,在百萬級的大數據中輕松的定位關鍵信息,等等..
PS:Lucene不是一個完整的全文索引應用,而是一個用Java語言開發的全文索引引擎工具包
基本概念
想了解ES首先就要弄清楚下面的幾個概念,這樣可以更加方便的學習ES,也不會對ES產生一些誤解:
近實時(NRT)
ES並不是一個標準的數據庫,它不像MongoDB,它側重於對存儲的數據進行搜索。因此要註意到它 不是 實時讀寫 的,這也就意味著,剛剛存儲的數據,並不能馬上查詢到。當然這裏還要區分查詢的方式,ES也有數據的查詢以及搜索,這裏的近實時強調的是搜索....
集群(Cluster)
在ES中,對用戶來說集群是很透明的。你只需要指定一個集群的名字
註意,如果群集中只有一個節點,那麽它是完全正常的。此外,您還可以擁有多個獨立的集群,每個集群都有自己唯一的集群名稱。
節點(Node)
節點是作為群集一部分的單個服務器,存儲數據並參與群集的索引和搜索功能。就像集群一樣,節點由名稱標識,默認情況下,該名稱是在啟動時分配給節點的隨機通用唯一標識符(UUID)。可以將節點配置為按群集名稱加入特定群集。默認情況下,每個節點都設置為加入一個名為cluster的集群elasticsearch
elasticsearch
。
註意,在單個群集中,您可以擁有任意數量的節點。此外,如果您的網絡上當前沒有其他Elasticsearch節點正在運行,則默認情況下啟動單個節點將形成一個名為的新單節點集群elasticsearch
。
索引(Index)
索引是具有某些類似特征的文檔集合。索引由名稱標識(必須全部小寫),此名稱用於在對其中的文檔執行索引,搜索,更新和刪除操作時引用索引。在單個群集中,您可以根據需要定義任意數量的索引。
類型(Type)
類型可以理解成一個索引的邏輯分區,用於標識不同的文檔字段信息的集合。但是由於ES還是以索引為粗粒度的單位,因此一個索引下的所有的類型,都存放在一個索引下。這也就導致不同類型相同字段名字的字段會存在類型定義沖突的問題。在6.0.0中已棄用。
文檔(Document)
文檔是存儲數據信息的基本單元,使用json來表示。在索引/類型中,您可以根據需要存儲任意數量的文檔。
註意,盡管文檔實際上駐留在索引中,但實際上必須將文檔編入索引/分配給索引中的類型。
分片與副本(Share & Replicas)
在ES中,索引會備份成分片,每個分片是獨立的lucene索引,可以完成搜索分析存儲等工作。創建索引時,只需定義所需的分片數即可。每個分片本身都是一個功能齊全且獨立的“索引”,可以托管在集群中的任何節點上。在可以隨時發生故障的網絡/雲環境中,非常有用,強烈建議使用故障轉移機制,以防分片/節點以某種方式脫機或因任何原因消失。為此,Elasticsearch允許您將索引的分片的一個或多個副本制作成所謂的副本分片或簡稱副本。
總而言之,每個索引可以拆分為多個分片。索引也可以復制為零(表示沒有副本)或更多次。復制後,每個索引都將具有主分片(從中復制的原始分片)和副本分片(主分片的副本)。可以在創建索引時為每個索引定義分片和副本的數量。創建索引後,您可以隨時動態更改副本數,但不能在事後更改分片數。默認情況下,Elasticsearch中的每個索引都分配了5個主分片和1個副本,這意味著如果群集中至少有兩個節點,則索引將包含5個主分片和另外5個副本分片(1個完整副本),總計為每個索引10個分片。
安裝
ES在開發整合時至少需要Java 8,所以在安裝之前ES之前,需要確保安裝了Java 8 並配置好環境變量。這裏我不多介紹,可以查看我往期的博客Linux服務部署--Java(一)
本文介紹的是單機版,Linux環境常用的wget下載elasticsearch-5.5.1.tar.gz,其他環境或方式可以參考官方文檔。目前官方推薦使用的是ES 6.X,目前還不清楚是否有插件兼容問題,所以還是選擇的市場主流的ES 5.5.X。
下載
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.tar.gz
解壓
tar -xvf elasticsearch-5.5.1.tar.gz
啟動
cd /elasticsearch-5.5.1
./bin/elasticsearch
如果這時報錯"max virtual memory areas vm.maxmapcount [65530] is too low",運行下面的命令。
sudo sysctl -w vm.max_map_count=262144
修改集群和節點名稱
./elasticsearch -Ecluster.name=my_cluster -Enode.name=my_node
默認情況下,Elasticsearch使用port 9200
來提供對其REST API的訪問。啟動完成後,打開另一個命令行窗口,請求該端口,會得到說明信息。
curl localhost:9200 { "name" : "my_node", "cluster_name" : "my_cluster", "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA", "version" : { "number" : "5.5.1", "build_hash" : "19c13d0", "build_date" : "2018-10-24T20:44:24.823Z", "build_snapshot" : false, "lucene_version" : "6.6.0" }, "tagline" : "You Know, for Search" }
PS:默認情況下,ES 只允許本機訪問,如果需要遠程訪問,可以修改 Elastic 安裝目錄的config/elasticsearch.yml
文件,去掉network.host
的註釋,將它的值改成0.0.0.0,讓任何人都可以訪問(線上環境別這麽設置哦),
然後重新啟動 ES。當然,也可以在這裏修改集群和節點名稱。
# ======================== 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_cluster # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node 節點名稱: # node.name: my_node # # 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)綁定的Ip地址: # network.host: 0.0.0.0 # # 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: 3 # # 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
當然,ES啟動會出現各種各樣的錯誤,比如內存分配不足,無法使用root用戶啟動,啟動直接Killed等等..可以查看Elasticsearch啟動錯誤 和 Centos7下避坑安裝elasticsearch。
Elasticsearch學習系列一