Elasticsearch學習之快速入門案例

2022-09-15 04:27:14 字數 4665 閱讀 6531

1. document資料格式

面向文件的搜尋分析引擎

(1)應用系統的資料結構都是物件導向的,複雜的

(2)物件資料儲存到資料庫中,只能拆解開來,變為扁平的多張表,每次查詢的時候還得還原回物件格式,相當麻煩

(3)es是面向文件的,文件中儲存的資料結構,與物件導向的資料結構是一樣的,基於這種文件資料結構,es可以提供複雜的索引,全文檢索,分析聚合等功能

(4)es的document用json資料格式來表達

1

public

class

employee

1011

private

class

employeeinfo

1819 employeeinfo info = new

employeeinfo();

20 info.setbio("

curious and modest");

21 info.setage(30

);22 info.setinterests(new string);

2324 employee employee = new

employee();

25 employee.setemail("

[email protected]");

26 employee.setfirstname("

san"

);27 employee.setlastname("

zhang");

28employee.setinfo(info);

29 employee.setjoindate(new

date());

3031 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表

,

"join_date

": "

2017/01/01

"}

我們就明白了es的document資料格式和資料庫的關係型資料格式的區別

2. 電商**商品管理案例

(1)對商品資訊進行crud(增刪改查)操作

(2)執行簡單的結構化查詢

(3)可以執行簡單的全文檢索,以及複雜的phrase(短語)檢索

(4)對於全文檢索的結果,可以進行高亮顯示

(5)對資料進行簡單的聚合分析

3. 簡單的集群管理

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

es提供了一套api,叫做cat api,可以檢視es中各種各樣的資料

get /_cat/health?v

epoch 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 111

1001

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 222

1000

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 111

1001

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?v

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open .kibana rum9n9wmrqccrrdehqnebg 11

103.1kb 3.1kb

(3)簡單的索引操作

建立索引:put /test_index?pretty

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open test_index xms9dtatskszswwhhgekkq 51

00650b 650b

yellow open .kibana rum9n9wmrqccrrdehqnebg 11

103.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

put /ecommerce/product/1,

"created

": true

}put /ecommerce/product/2

put /ecommerce/product/3

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

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

get /index/type/id

get /ecommerce/product/1

}

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

put /ecommerce/product/1,  

"created

": true},

"created

": false

}put /ecommerce/product/1

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

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

post /ecommerce/product/1/_update

}}

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

delete /ecommerce/product/1

}

elasticsearch學習入門

由於es更新很快,本文這類快餐式的記錄僅供參考 es的官網有比較全面的api,但我看過以後感覺api的層次還是有點亂,至少沒有mongodb的文件那麼簡單易讀。從簡單的應用開始慢慢認識es的。比如要搭建個中文新聞資訊的搜尋引擎,新聞有 標題 內容 作者 型別 發布時間 這五個字段 我們要提供 標題和...

elasticsearch 入門學習

原文 1 思考 大規模資料如何檢索 當系統資料量上了10億 100億條的時候,我們在做系統架構的時候通常會從以下角度去考慮問題 2 傳統資料庫的應對解決方案 對於關係型資料,我們通常採用以下或類似架構去解決查詢瓶頸和寫瓶頸 3 非關係型資料庫的解決方案 對於nosql資料庫,以mongdb為例,其它...

Elasticsearch 學習筆記

參考 關係型資料庫 elasticsearch 資料庫database索引index,支援全文檢索 表table型別type 資料行row文件document,但不需要固定結構,不同文件可以具有不同字段集合 資料列column字段field 模式schema 索引字段型別numeric dataty...