寫在前面的話:讀書破萬卷,編碼若有神
--------------------------------------------------------------------html
參考內容:java
《Elasticsearch頂尖高手系列-快速入門篇》,中華石杉node
--------------------------------------------------------------------數據庫
主要內容包括:json
--------------------------------------------------------------------數據結構
一、es的document數據格式和數據庫的關係型數據格式的區別elasticsearch
好比有以下的json:網站
1 { 2 "email":"zhangsan@sina.com", 3 "first_name":"san", 4 "last_name":"zhang", 5 "info":{ 6 "bio":"curious and modest", 7 "age":30, 8 "interests":["bike","climb"] 9 "join_date":"2017/01/01" 10 } 11 }
es的docuement能夠直接用上面的json數據格式來表達。ui
可是在java中須要兩個類來表達:編碼
1 public class Employee { 2 3 private String email; 4 private String firstName; 5 private String lastName; 6 private EmployeeInfo info; 7 8 } 9 10 private class EmployeeInfo { 11 12 private String bio; // 性格 13 private Integer age; 14 private String[] interests; // 興趣愛好 15 private Date joinDate; 16 }
能夠看出employee對象裏面包含了Employee類本身的屬性,並且還有一個EmployeeInfo對象。
在數據庫中的話,就須要兩張表:employee表、employee_info表,將employee對象的數據從新拆開來,變成Employee數據和EmployeeInfo數據
employee表:email,first_name,last_name,join_date,4個字段
employee_info表:bio,age,interests,3個字段;此外還有一個外鍵字段,好比employee_id,關聯着employee表。
咱們就明白了es的document數據格式和數據庫的關係型數據格式的區別:
--------------------------------------------------------------------
二、簡單的集羣管理
(2.1)快速檢查集羣的健康情況
在Kibana中執行以下命令: GET _cat/health?v
1 epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 2 1518657893 09:24:53 huobaopaocai-es-cluster yellow 1 1 1 1 0 0 1 0 - 50.0%
如何快速的瞭解集羣的健康狀態? green、yellow、red
爲何如今咱們的是處於yellow狀態?
咱們如今就一個筆記本電腦,就啓動了一個es進程,至關於就只有一個node。如今es中有一個index,就是kibana本身內置創建的index。因爲默認的配置是給每一個index分配5個primary shard和5個replica shard,並且primary shard和replica shard不能在同一臺機器上(爲了容錯)。如今kibana本身創建的index是1個primary shard和1個replica shard。當前就一個node,因此只有1個primary shard被分配了和啓動了,可是一個replica shard沒有第二臺機器去啓動。
(2.2)快速查看集羣中有些索引
在Kibana中執行以下命令: GET _cat/indices?v
1 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 2 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb
(2.3)簡單的索引操做
建立索引: PUT /test_index?pretty
1 { 2 "acknowledged": true, 3 "shards_acknowledged": true 4 }
再次查看索引:
1 GET _cat/indices?v 2 3 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 4 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb 5 yellow open test_index i2LdlSIqRXCZQCLauVBiRw 5 1 0 0 650b 650b
刪除索引: DELETE /test_index?pretty
{ "acknowledged": true }
再次查看索引:
1 GET _cat/indices?v 2 3 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 4 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb
--------------------------------------------------------------------
三、電商網站商品管理案例背景介紹
有一個電商網站,須要爲其基於ES構建一個後臺系統,提供如下功能:
--------------------------------------------------------------------
四、商品的CRUD操做(document 的CRUD操做 )
(4.1)新增商品:新增文檔、創建索引
基本語法格式:
1 PUT /index/type/id 2 { 3 "json數據" 4 }
準備三條數據:
1 PUT /ecommerce/product/1 2 { 3 "name" : "gaolujie yagao", 4 "desc" : "gaoxiao meibai", 5 "price" : 30, 6 "producer" : "gaolujie producer", 7 "tags": [ "meibai", "fangzhu" ] 8 } 9 10 PUT /ecommerce/product/2 11 { 12 "name" : "jiajieshi yagao", 13 "desc" : "youxiao fangzhu", 14 "price" : 25, 15 "producer" : "jiajieshi producer", 16 "tags": [ "fangzhu" ] 17 } 18 19 PUT /ecommerce/product/3 20 { 21 "name" : "zhonghua yagao", 22 "desc" : "caoben zhiwu", 23 "price" : 40, 24 "producer" : "zhonghua producer", 25 "tags": [ "qingxin" ] 26 }
執行每條新增語句的結果:
1 { 2 "_index": "ecommerce", 3 "_type": "product", 4 "_id": "1", 5 "_version": 1, 6 "result": "created", 7 "_shards": { 8 "total": 2, 9 "successful": 1, 10 "failed": 0 11 }, 12 "created": true 13 }
(ps:es會自動創建index和type,不須要提早建立,並且es默認會對document每一個field創建倒排索引,讓其能夠被搜索。)
用java來實現往es中添加docuemnt操做
@Autowired private ElasticsearchConstant elasticsearchConstant; /** * 新增es的docuemnt */ @Test public void createDocumentTest() throws IOException { TransportClient client = elasticsearchConstant.getClient(); IndexResponse response = client.prepareIndex(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(),"1") .setSource(XContentFactory.jsonBuilder() .startObject() .field("name","gaolujie yagao") .field("desc", "gaoxiao meibai") .field("price", 30) .field("producer", "gaolujie producer") .endObject()) .get(); LOG.info(String.format("新增es的docuemnt結果: %s",response.toString())); } 執行結果: 2018-02-15 11:37:24 INFO [main] (EcommerceTest.java:56) createDocumentTest - 新增es的docuemnt結果: IndexResponse[index=ecommerce,type=product,id=1,version=1,result=created,shards={"total":2,"successful":1,"failed":0}]
(4.2)檢索索引:檢索文檔
基本語法格式:
1 GET /index/type/id
用java來實現查詢es中docuemnt操做
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 查詢es的document 6 */ 7 @Test 8 public void getDocumentTest() throws IOException{ 9 TransportClient client = elasticsearchConstant.getClient(); 10 GetResponse response = client.prepareGet(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get(); 11 LOG.info(String.format("查詢es的docuemnt結果: %s",response.toString())); 12 } 13 14 執行結果: 15 2018-02-15 11:42:19 INFO [main] (EcommerceTest.java:68) getDocumentTest - 查詢es的docuemnt結果: {"_index":"ecommerce","_type":"product","_id":"1","_version":1,"found":true,"_source":{"name":"gaolujie yagao","desc":"gaoxiao meibai","price":30,"producer":"gaolujie producer"}}
(4.3)修改商品:替換文檔
(ps:替換方式有一個很差:必須帶上全部的field才能去進行信息的修改。)
(4.4)修改商品:更新文檔
用java來實現編輯es中的docuement:
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 更新es的docuemnt 6 * @throws IOException 7 */ 8 @Test 9 public void updateDocument() throws IOException { 10 TransportClient client = elasticsearchConstant.getClient(); 11 UpdateResponse updateResponse = client.prepareUpdate(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1") 12 .setDoc(jsonBuilder() 13 .startObject() 14 .field("name", "jiaqiang gaolujie yagao") 15 .endObject()) 16 .get(); 17 LOG.info(String.format("更新es的docuemnt結果: %s",updateResponse.toString())); 18 } 19 20 運行結果: 21 2018-02-15 15:55:13 INFO [main] (EcommerceTest.java:73) updateDocument - 更新es的docuemnt結果: UpdateResponse[index=ecommerce,type=product,id=1,version=2,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]
(4.5)刪除商品:刪除文檔
用java來實現刪除es中的docuement:
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 刪除es中的document 6 */ 7 @Test 8 public void deleteDocument(){ 9 TransportClient client = elasticsearchConstant.getClient(); 10 DeleteResponse response = client.prepareDelete(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get(); 11 LOG.info(String.format("刪除es的docuemnt結果: %s",response.toString())); 12 } 13 14 運行結果: 15 2018-02-15 16:00:04 INFO [main] (EcommerceTest.java:84) deleteDocument - 刪除es的docuemnt結果: DeleteResponse[index=ecommerce,type=product,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]