日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch学习之快速入门案例

發布時間:2024/1/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch学习之快速入门案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. document數據格式

面向文檔的搜索分析引擎

(1)應用系統的數據結構都是面向對象的,復雜的
(2)對象數據存儲到數據庫中,只能拆解開來,變為扁平的多張表,每次查詢的時候還得還原回對象格式,相當麻煩
(3)ES是面向文檔的,文檔中存儲的數據結構,與面向對象的數據結構是一樣的,基于這種文檔數據結構,es可以提供復雜的索引,全文檢索,分析聚合等功能
(4)es的document用json數據格式來表達

1 public class Employee { 2 3 private String email; 4 private String firstName; 5 private String lastName; 6 private EmployeeInfo info; 7 private Date joinDate; 8 9 } 10 11 private class EmployeeInfo { 12 13 private String bio; // 性格 14 private Integer age; 15 private String[] interests; // 興趣愛好 16 17 } 18 19 EmployeeInfo info = new EmployeeInfo(); 20 info.setBio("curious and modest"); 21 info.setAge(30); 22 info.setInterests(new String[]{"bike", "climb"}); 23 24 Employee employee = new Employee(); 25 employee.setEmail("zhangsan@sina.com"); 26 employee.setFirstName("san"); 27 employee.setLastName("zhang"); 28 employee.setInfo(info); 29 employee.setJoinDate(new Date()); 30 31 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表

?

{"email": "zhangsan@sina.com","first_name": "san","last_name": "zhang","info": {"bio": "curious and modest","age": 30,"interests": [ "bike", "climb" ]},"join_date": "2017/01/01" }

?

我們就明白了es的document數據格式和數據庫的關系型數據格式的區別

?

2. 電商網站商品管理案例

?

有一個電商網站,需要為其基于ES構建一個后臺系統,提供以下功能:

?

(1)對商品信息進行CRUD(增刪改查)操作
(2)執行簡單的結構化查詢
(3)可以執行簡單的全文檢索,以及復雜的phrase(短語)檢索
(4)對于全文檢索的結果,可以進行高亮顯示
(5)對數據進行簡單的聚合分析

?

3. 簡單的集群管理

?

(1)快速檢查集群的健康狀況

?

es提供了一套api,叫做cat api,可以查看es中各種各樣的數據

?

GET /_cat/health?vepoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1488006741 15:12:21 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1488007113 15:18:33 elasticsearch green 2 2 2 1 0 0 0 0 - 100.0%epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1488007216 15:20:16 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%

?

如何快速了解集群的健康狀況?green、yellow、red?

  green:每個索引的primary shard和replica shard都是active狀態的
  yellow:每個索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處于不可用的狀態
  red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了

為什么現在會處于一個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沒有第二臺機器去啟動。做一個小實驗:此時只要啟動第二個es進程,就會在es集群中有2個node,然后那1個replica shard就會自動分配過去,然后cluster status就會變成green狀態。

(2)快速查看集群中有哪些索引

?

GET /_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb

?

(3)簡單的索引操作

創建索引:PUT /test_index?prettyhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open test_index XmS9DTAtSkSZSwWhhGEKkQ 5 1 0 0 650b 650b yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb
刪除索引:DELETE /test_index?pretty

 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
? yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb

4. 商品的CRUD操作

(1)新增商品:新增文檔,建立索引

PUT /index/type/id {"json數據" }PUT /ecommerce/product/1 {"name" : "gaolujie yagao","desc" : "gaoxiao meibai","price" : 30,"producer" : "gaolujie producer","tags": [ "meibai", "fangzhu" ] }{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true }PUT /ecommerce/product/2 {"name" : "jiajieshi yagao","desc" : "youxiao fangzhu","price" : 25,"producer" : "jiajieshi producer","tags": [ "fangzhu" ] }PUT /ecommerce/product/3 {"name" : "zhonghua yagao","desc" : "caoben zhiwu","price" : 40,"producer" : "zhonghua producer","tags": [ "qingxin" ] }

es會自動建立index和type,不需要提前創建,而且es默認會對document每個field都建立倒排索引,讓其可以被搜索

(2)查詢商品:檢索文檔

GET /index/type/id GET /ecommerce/product/1{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"found": true,"_source": {"name": "gaolujie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]} }

(3)修改商品:替換文檔

PUT /ecommerce/product/1 {"name" : "jiaqiangban gaolujie yagao","desc" : "gaoxiao meibai","price" : 30,"producer" : "gaolujie producer","tags": [ "meibai", "fangzhu" ] }{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true }{"_index": "ecommerce","_type": "product","_id": "1","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"created": false }PUT /ecommerce/product/1 {"name" : "jiaqiangban gaolujie yagao" }

替換方式有一個不好,即使必須帶上所有的field,才能去進行信息的修改

(4)修改商品:更新文檔

POST /ecommerce/product/1/_update {"doc": {"name": "jiaqiangban gaolujie yagao"} }{"_index": "ecommerce","_type": "product","_id": "1","_version": 8,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0} }

(5)刪除商品:刪除文檔

DELETE /ecommerce/product/1{"found": true,"_index": "ecommerce","_type": "product","_id": "1","_version": 9,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0} }{"_index": "ecommerce","_type": "product","_id": "1","found": false }

?

轉載于:https://www.cnblogs.com/sunfie/p/7016697.html

總結

以上是生活随笔為你收集整理的Elasticsearch学习之快速入门案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。