1 document数据格式
面向文档的搜素引擎
- 应用系统的数据结构都是面向对象的,复杂
- 对象数据存储到数据库中,只能拆解开来,变成多张表,每次查询的时候,需要定义相应的model或者bean格式,比较麻烦。
- Es是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档数据结构,es可以提供复杂的索引,全文索引,分析聚合等功能。
- Es的document是json数据格式表示的。
如下是一个document:
{
"email": "111111@qq.com ",
"firstname": "san",
"lastname": "zhang",
"info": {
"bio": "modest",
"age": 20,
"interests": [
"books",
"coding"
]
}
}
定义成Java类,面向对象。
Public class Employee{
Private String eamil;
Private String firstname;
Private String lastname;
Private EmployeeInfo info;
}
Public class EmployeeInfo{
Private String bio;
Private Integer age;
Private String [] interests;
}
在面向对象的数据存储中,需要将一个员工的个人信息拆分成两张表: Employee 和EmployeeInfo 表。
2 电商案例背景介绍
假设有一个电商网站,需要基于es构建一个后台管理系统,提供一下功能:
- 对商品信息进行crud操作
- 执行简单的结构化查询(之后的博客中更新)
3 简单的集群管理
3.1 快速检查集群的健康状况
Es提供了一套cat api ,可以查看es集群中的一些数据
GET /_cat/health?v
如何快速了解集群的健康状况 green yellow red
Green :每个索引的primary shrad 和 replica shard 都是active状态
Yellow : 每个索引的primary shard 都是active状态,但是部分replica shard 不是active状态,即不可用状态
Red : 不是所有的索引的primary shard 都处于active状态,即会有部分索引丢失数据
示例:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1547992424 13:53:44 elasticsearch green 1 1 1 1 0 0 0 0 - 100.0%
3.2 快速检查集群中的索引
命令:GET /_cat/indices?v
示例:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 EZp8Su8GTuyvLMVCIUEwTA 1 0 3 0 11.9kb 11.9kb
Kibana自己会创建一个默认的索引,上面显示primary shard有1个,replica shard 没有,document存在3个
3.3 简单的索引操作
3.1 创建索引
PUT /test_Index?pretty
示例:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test_index"
}
然后我们快速查看节点中的索引:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index 1SmmnEkZRfKOWf2XdsRiOA 5 1 0 0 1.1kb 1.1kb
green open .kibana_1 EZp8Su8GTuyvLMVCIUEwTA 1 0 3 0 11.9kb 11.9kb
3.2 删除索引
DELETE /test_index?pretty
返回示例:
{
"acknowledged" : true
}
查看索引:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 EZp8Su8GTuyvLMVCIUEwTA 1 0 3 0 11.9kb 11.9kb
查看es日志打印如下:
4 商品的crud操作
4.1新增商品: 新增文档,建立索引
命令:
PUT /ecommerce/product/1{
“name” : “gaolujie yagao”,
“desc” : “gaoxiao meibai”,
“price” : 30,
“producter”: “gaolujie producter”,
“tags” :[“meibai” ,”fangzhu”]
}
Es会自动创建index和type,不担心提前创建,而且es默认会对每一个field都建立倒排索引,让其可以被搜索。
返回示例
# PUT /ecommerce/product/3
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
4.2 查询商品 : 检索文档
GET /index/type/id
GET/ecommerce/product/1
返回示例:
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producter" : "gaolujie producter",
"tags" : [
"meibai",
"fangzhu"
]
}
}
4.3 修改商品
4.3.1替换文档方式
PUT /ecommerce/product/1
{
"name": "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producter": "gaolujie producter",
"tags" :["meibai" ,"fangzhu"]
}
返回示例:
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
查看修改后的值:
命令:GET /ecommerce/product/1
返回示例:
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"name" : "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producter" : "gaolujie producter",
"tags" : [
"meibai",
"fangzhu"
]
}
}
这种方式不好的地方:就是必须保留多有的filed
6.4.3.2 更新文档方式
命令:
POST /ecommerce/product/1/_update
{
"doc" :{
"name" : "gaoljie yagao"
}
}
返回示例:
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 2
}
4.4 删除商品 : 删除文档
命令:DELETE /ecommerce/product/1/?pretty
返回示例:
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_version" : 6,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 2
}