1、document數(shù)據(jù)格式
面向文檔的搜索分析引擎
(1)應(yīng)用系統(tǒng)的數(shù)據(jù)結(jié)構(gòu)都是面向?qū)ο蟮?,?fù)雜的
(2)對(duì)象數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中,只能拆解開(kāi)來(lái),變?yōu)楸馄降亩鄰埍?,每次查詢的時(shí)候還得還原回對(duì)象格式,相當(dāng)麻煩
(3)ES是面向文檔的,文檔中存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),與面向?qū)ο蟮臄?shù)據(jù)結(jié)構(gòu)是一樣的,基于這種文檔數(shù)據(jù)結(jié)構(gòu),es可以提供復(fù)雜的索引,全文檢索,分析聚合等功能
(4)es的document用json數(shù)據(jù)格式來(lái)表達(dá)
public class Employee {
private String email;
private String firstName;
private String lastName;
private EmployeeInfo info;
private Date joinDate;
}
private class EmployeeInfo {
private String bio; // 性格
private Integer age;
private String[] interests; // 興趣愛(ài)好
}
EmployeeInfo info = new EmployeeInfo();
info.setBio("curious and modest");
info.setAge(30);
info.setInterests(new String[]{"bike", "climb"});
Employee employee = new Employee();
employee.setEmail("zhangsan@sina.com");
employee.setFirstName("san");
employee.setLastName("zhang");
employee.setInfo(info);
employee.setJoinDate(new Date());
employee對(duì)象:里面包含了Employee類自己的屬性,還有一個(gè)EmployeeInfo對(duì)象
數(shù)據(jù)庫(kù)的關(guān)系型數(shù)據(jù)格式
兩張表:employee表,employee_info表,將employee對(duì)象的數(shù)據(jù)重新拆開(kāi)來(lái),變成Employee數(shù)據(jù)和EmployeeInfo數(shù)據(jù)
employee表:email,first_name,last_name,join_date,4個(gè)字段
employee_info表:bio,age,interests,3個(gè)字段;此外還有一個(gè)外鍵字段,比如employee_id,關(guān)聯(lián)著employee表
es的document數(shù)據(jù)格式
{
"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數(shù)據(jù)格式和數(shù)據(jù)庫(kù)的關(guān)系型數(shù)據(jù)格式的區(qū)別
2、電商網(wǎng)站商品管理案例背景介紹
有一個(gè)電商網(wǎng)站,需要為其基于ES構(gòu)建一個(gè)后臺(tái)系統(tǒng),提供以下功能:
(1)對(duì)商品信息進(jìn)行CRUD(增刪改查)操作
(2)執(zhí)行簡(jiǎn)單的結(jié)構(gòu)化查詢
(3)可以執(zhí)行簡(jiǎn)單的全文檢索,以及復(fù)雜的phrase(短語(yǔ))檢索
(4)對(duì)于全文檢索的結(jié)果,可以進(jìn)行高亮顯示
(5)對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單的聚合分析
(6)現(xiàn)在全部都是用es的restful api來(lái)實(shí)現(xiàn)es的所有知識(shí)點(diǎn)和功能點(diǎn),后續(xù)會(huì)使用java api
3、簡(jiǎn)單的集群管理
(1)快速檢查集群的健康狀況,es提供了一套api,叫做cat api,可以查看es中各種各樣的數(shù)據(jù)
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
1522762745 21:39:05 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%
如何快速了解集群的健康狀況?green、yellow、red?
green:每個(gè)索引的primary shard和replica shard都是active狀態(tài)的
yellow:每個(gè)索引的primary shard都是active狀態(tài)的,但是部分replica shard不是active狀態(tài),處于不可用的狀態(tài)
red:不是所有索引的primary shard都是active狀態(tài)的,部分索引有數(shù)據(jù)丟失了
為什么現(xiàn)在會(huì)處于一個(gè)yellow狀態(tài)?
我們現(xiàn)在就一個(gè)筆記本電腦,就啟動(dòng)了一個(gè)es進(jìn)程,相當(dāng)于就只有一個(gè)node。現(xiàn)在es中有一個(gè)index,就是kibana自己內(nèi)置建立的index。由于默認(rèn)的配置是給每個(gè)index分配5個(gè)primary shard和5個(gè)replica shard,而且primary shard和replica shard不能在同一臺(tái)機(jī)器上(為了容錯(cuò))?,F(xiàn)在kibana自己建立的index是1個(gè)primary shard和1個(gè)replica shard。當(dāng)前就一個(gè)node,所以只有1個(gè)primary shard被分配了和啟動(dòng)了,但是一個(gè)replica shard沒(méi)有第二臺(tái)機(jī)器去啟動(dòng)。
做一個(gè)小實(shí)驗(yàn):此時(shí)只要啟動(dòng)第二個(gè)es進(jìn)程,就會(huì)在es集群中有2個(gè)node,然后那1個(gè)replica shard就會(huì)自動(dòng)分配過(guò)去,然后cluster status就會(huì)變成green狀態(tài)。
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1522763492 21:51:32 elasticsearch green 2 2 2 1 0 0 0 0 - 100.0%
(2) 快速查看集群中有哪些索引
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana sny6Sj_JS6CaYEV079oAQg 1 1 1 0 3.1kb 3.1kb
index:索引
pri:primary shard
rep:replica shard
docs.count:document數(shù)量
docs.deleted:document刪除數(shù)量
store.size:總數(shù)據(jù)占用大小
pri.store.size:primary shard存儲(chǔ)數(shù)據(jù)大小
(3) 簡(jiǎn)單的索引操作
創(chuàng)建索引:PUT /test_index?prett
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana sny6Sj_JS6CaYEV079oAQg 1 1 1 0 3.1kb 3.1kb
yellow open test_index xev5PyVMTwCNt_LxwqFZWA 5 1 0 0 650b 650b
刪除索引:DELETE /test_index?pretty
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana sny6Sj_JS6CaYEV079oAQg 1 1 1 0 3.1kb 3.1kb
4、商品的CRUD操作
(1)新增商品:新增文檔,建立索引
#模板:
PUT /index/type/id
{
"json數(shù)據(jù)"
}
#實(shí)操:
PUT /ecommerce/product/1
{
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
#返回結(jié)果:
{
"_index": "ecommerce", //索引
"_type": "product", //類型
"_id": "1",
"_version": 1, //版本號(hào)
"result": "created", //操作類型
"_shards": { //寫(xiě)到shard中
"total": 2, //寫(xiě)到2個(gè)shard中
"successful": 1, //成功寫(xiě)到1個(gè)shard中
"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會(huì)自動(dòng)建立index和type,不需要提前創(chuàng)建,而且es默認(rèn)會(huì)對(duì)document每個(gè)field都建立倒排索引,讓其可以被搜索
(2)查詢商品:檢索文檔
#模板
GET /index/type/id
#實(shí)操
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)修改商品:替換文檔
#實(shí)操
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": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
#錯(cuò)誤的方式
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao"
}
替換方式有一個(gè)不好,即使必須帶上所有的field,才能去進(jìn)行信息的修改
(4)修改商品:更新文檔
#實(shí)操
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)刪除商品:刪除文檔
#實(shí)操
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
}