ElasticSearch基礎普及篇

本文將分為三大部分來講es相關的基礎知識點

一、ElasticSearch 基礎概念和組成部分

1)ElasticSearch概念一句話總結:

ES是一個基於Lucene並採用Restful API 標準的高可擴展性和高可用性的實時數據分析的全文檢索工具

2)ElasticSearch涉及到的一些概念:

NRT(準實時): Elasticsearch是一個接近實時的搜索平臺。這意味著,從索引一個文檔直到這 個文檔能夠被搜索到有一個輕微的延遲(通常是1秒)

Node(節點):單個的裝有ElasticSearch服務並且提供故障轉移和擴展的服務器

Cluster(集群):一個集群就是由一個或多個Node組織在一起共同工作,共同分享整個數據具有負載均衡功能的集群,集群名稱是唯一標識,因為一個節點只能通過指定某個集群的名字,來加入這個集群

Document(文檔):一個文檔是一個可以被索引的基礎信息單元

Index(索引):索引就是一個擁有幾分相似特徵的文檔的集合

Type(類型):一個索引中,你可以定義一種或者多種類型

Field(列):Field是ElasticSearch中最小單位,相當於數據的某一列

Shards(分片):一個索引可以存儲超出單個結點硬件限制的大量數據。比如,一個具有10億文檔的索引佔據1TB的磁盤空間,而任一節點都沒有這樣大的磁盤空間;或者單個節點處理搜索請求,響應太慢。因此ElasticSearch將索引分成若干份,每個部分就是一個shard ,當你創建一個索引的時候,你可以指定你想要的分片的數量。每個分片本身也是一個功能完善並且獨立的“索引”,這個“索引”可以被放置到集群中的任何節點上。 分片好處就是可以對數據進行水平分割,擴展內容容量,提高查詢性能和吞吐量

Replicas(複製):Replicas是索引的一份或者多份拷貝 提供高可用

分片和複製的數量可以在索引創建的時候指定。在索引創建之後,你可以在任何時候動態地改變複製數量,但是不能改變分片的數量

3)Es跟關係型數據庫對照如下圖:

ElasticSearch基礎普及篇

4) ElasticSearch架構圖如下:

ElasticSearch基礎普及篇

二、ElastSearch 簡單安裝

1)ElasticSearch安裝:

ES最佳實踐安裝方法就是下載壓縮包直接解壓 官網地址:https://www.elastic.co/

1)安裝ES首先需要安裝jdk,必須是1.7以上的版本。

[root@localhost ~]# yum -y install java-1.8.0-openjdk*

2)下載壓縮包到下載目錄下

[root@localhost ~] # cd /opt/elasticsearch && wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.5/elasticsearch-2.3.5.tar.gz

[root@localhost elasticsearch]# tar zxvf elasticsearch-2.3.5.tar.gz

3)創建ES安裝目錄 將解壓後的東西移動到安裝目錄

[root@localhost elasticsearch]# cd elasticsearch-2.3.5

[root@localhost elasticsearch-2.3.5]# mkdir /usr/local/elasticsearch

[root@localhost elasticsearch-2.3.5]# mv * /usr/local/elasticsearch

[root@localhost elasticsearch-2.3.5]# cd /usr/local/elasticsearch/bin/

[root@localhost bin]# ./elasticsearch

Exceptioninthread"main"java.lang.RuntimeException:don'trunelasticsearchasroot.

atorg.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93)

atorg.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144)

atorg.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)

atorg.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)

Refertothelogforcompleteerrordetails.

報錯,提示不能用root用戶運行。

vim編輯elasticsearch,添加ES_JAVA_OPTS="-Des.insecure.allow.root=true"

我添加在了註釋說明下面,一定在變量ES_JAVA_OPTS使用前添加。

再次運行./elasticsearch

[root@localhost bin]# ./elasticsearch

[2016-08-2016:11:52,021][WARN][bootstrap]runningasROOTuser.thisisabadidea!

[2016-08-2016:11:52,034][WARN] [bootstrap]unabletoinstallsyscallfilter:seccompunavailable:requireskernel3.5+withCONFIG_SECCOMPandCONFIG_SECCOMP_FILTERcompiledin

[2016-08-2016:11:52,431][INFO][node] [JohnnyStorm]version[2.3.5],pid[28122],build[90f439f/2016-07-27T10:36:52Z]

[2016-08-2016:11:52,431][INFO][node][JohnnyStorm]initializing...

[2016-08-2016:11:53,390][INFO][plugins][JohnnyStorm]modules[lang-groovy,reindex,lang-expression],plugins[],sites[]

[2016-08-2016:11:53,474][INFO][env] [JohnnyStorm]using[1]datapaths,mounts/(/dev/mapper/VolGroup-lv_root),netusable_space[22.3gb],nettotal_space[30.5gb],spins?[possibly],types[ext4]

[2016-08-2016:11:53,475][INFO][env][JohnnyStorm]heapsize[1007.3mb],compressedordinaryobjectpointers[true]

[2016-08-2016:11:53,475][WARN][env][JohnnyStorm]maxfiledescriptors[65535]forelasticsearchprocesslikelytoolow,considerincreasingtoatleast[65536]

[2016-08-2016:11:55,811][INFO][node][JohnnyStorm]initialized

[2016-08-2016:11:55,811][INFO][node][JohnnyStorm]starting...

[2016-08-2016:11:56,031][INFO][transport][JohnnyStorm]publish_address{127.0.0.1:9300},bound_addresses{127.0.0.1:9300},{[::1]:9300}

[2016-08-2016:11:56,038][INFO][discovery][JohnnyStorm]elasticsearch/VDX23-BvQX-2saLUKUSLww

[2016-08-2016:11:59,138][INFO][cluster.service][JohnnyStorm]new_master{JohnnyStorm}{VDX23-BvQX-2saLUKUSLww}{127.0.0.1}{127.0.0.1:9300},reason:zen-disco-join(elected_as_master,[0]joinsreceived)

[2016-08-2016:11:59,166][INFO][http][JohnnyStorm]publish_address{127.0.0.1:9200},bound_addresses{127.0.0.1:9200},{[::1]:9200}

[2016-08-2016:11:59,166][INFO][node][JohnnyStorm]started

[2016-08-2016:11:59,201][INFO][gateway][JohnnyStorm]recovered[0]indicesintocluster_state

出現上面信息表示已經運行成功,到此正式完成安裝。

外網訪問

上面的提示中有一句:publish_address{127.0.0.1:9200},bound_addresses{127.0.0.1:9200},{[::1]:9200},通過瀏覽器訪問發現無法訪問。

修改配置文件,

vim /usr/local/elasticsearch/config/elasticsearch.yml

添加或修改:network.host:0.0.0.0,表示不限制IP訪問

重啟服務後瀏覽器就能訪問。

三、ElasticSearch 插件部分簡介

ES安裝插件是非常簡單的 都是一句命令搞定 插件一般都是存放在github上面 命令會自動去找相應目錄下的源碼 下載並安裝到本地的plugin目錄下 也可以使用下載並解壓

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.9.5/elasticsearch-analysis-ik-1.9.5.zip

mkdir /usr/local/elasticsearch/plugin/ik

cp elasticsearch-analysis-ik-1.9.5.zip /usr/local/elasticsearch/plugin/ik

unzip elasticsearch-analysis-ik-1.9.5.zip

4) 驗證 創建索引 創建mapping 添加數據

創建索引:curl -X PUT http://localhost:9200/index

創建mapping:

curl -X POST http: //localhost: 9200/index/fulltext/_mapping-d'  {

"fulltext": {

"_all": {

"analyzer": "ik_max_word",

"search_analyzer": "ik_max_word",

"term_vector": "no",

"store": "false"

},

"properties": {

"content": {

"type": "string",

"store": "no",

"term_vector": "with_positions_offsets",

"analyzer": "ik_max_word",

"search_analyzer": "ik_max_word",

"include_in_all": "true",

"boost": 8

}

}

}

}'

插入數據:

curl -X POST http://localhost:9200/index/fulltext/1 -d '{"content":"美國留給伊拉克的是個爛攤子嗎"}'

curl -X POST http://localhost:9200/index/fulltext/2 -d '{"content":"公安部:各地校車將享最高路權"}'

curl -X POST http://localhost:9200/index/fulltext/3 -d '{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}'

curl -X POST http://localhost:9200/index/fulltext/4 -d '{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊嫌犯已自首"}'

查詢數據並高亮顯示:

curl -X POST http://localhost:9200/index/fulltext/_search-d'

{

"query":{"term":{"content":"中國"}},

"highlight":{

"pre_tags":["",""], //默認高亮是em標籤

"post_tags":["",""],

"fields":{  "content":{}  }

}

}'

結果:

{  "took":14,  "timed_out":false,  "_shards":{  "total":5,  "successful":5,  "failed":0  },  "hits":{  "total":2,  "max_score":2,  "hits":[  {  "_index":"index",  "_type":"fulltext",  "_id":"4",  "_score":2,  "_source":{  "content":"中國駐洛杉磯領事館遭亞裔男子槍擊嫌犯已自首"  },  "highlight":{  "content":[  "中國駐洛杉磯領事館遭亞裔男子槍擊嫌犯已自首"  ]  }  },  {  "_index":"index",  "_type":"fulltext",  "_id":"3",  "_score":2,  "_source":{  "content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"  },  "highlight":{  "content":[  "均每天扣1艘中國漁船"  ]  }  }  ]  }  }

2)JDBC插件安裝 該插件主要是同步數據用的 後面會有篇幅去詳細介紹此部分

[root@localhost bin]# cd /opt/elasticsearch

[root@localhost elasticsearch]# wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.4.0/elasticsearch-jdbc-2.3.4.0-dist.zip

[root@localhostelasticsearch]# cp elasticsearch-jdbc-2.3.4.0-dist /usr/local/elasticsearch/elasticsearch-jdbc-2.3.4.0

3) head插件安裝 下圖摘自網上

[root@localhost bin]# ./plugin -install mobz/elasticsearch-head

會根據配置文件配置的插件存放位置進行安裝 打開http://localhost:9200/_plugin/head即可查看

在地址欄輸入es服務器的ip地址和端口點connect就可以連接到集群。下面是連接後的視圖。這是主界面,在這裡可以看到es集群的基本信息(如:節點情況,索引情況)。

ElasticSearch基礎普及篇

界面的右邊有些按鈕,如:node stats, cluster nodes,這些是直接請求es的相關狀態的api,返回結果為json,如下圖:

ElasticSearch基礎普及篇

在索引下面有info和action兩個按鈕。info是可以查看索引的狀態和mapping的定義。action是對索引進行操作,如:添加別名、刷新、關閉索引,刪除索引等。

ElasticSearch基礎普及篇

ElasticSearch基礎普及篇

browser瀏覽界面,這個界面可以同時查看多個索引的數據,也可以查詢指定字段的數據。

ElasticSearch基礎普及篇

Structured Query查詢界面,這個界面可以對某個索引進行一些複雜查詢,如下面這個例子是查詢product索引,構造boolquery,title字段裡查詢“產品”關鍵詞,price範圍為10到100的記錄。

ElasticSearch基礎普及篇

Any Request任意請求界面,這個界面可以說是個rest的客戶端,可以通過它來對es進行一些請求操作或測試api接口,下面這個例子是把product索引的副本數設置為1,更多的api可以到es官網查詢。

ElasticSearch基礎普及篇


作者:meng_philip123
鏈接:https://www.jianshu.com/p/2203e100e83c
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。


分享到:


相關文章: