elasticsearch学习笔记(五)——快速入门案例实战电商网站商品管理:集群健康检查,文档的CRUD

elasticsearch和kibana都已经安装和启动了,下就开始进行实战了

1、document数据格式

首先来讲一下ES为什么面向文档以及面向文档的好处。
(1)一般应用系统的数据结构都是面向对象的,结构复杂,操作起来特别不方便。如果将对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得还原回对象格式,相当的麻烦。
(2)ES是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档的数据结构,es可以提供复杂的索引,全文检索,分析聚合等的功能。
(3)es的document底层是用json数据格式来表达的,json的优势就用说了,附上一篇文章来说明 https://blog.csdn.net/it_drea...

对象的数据结构:

public class Employee {
    private String email;
    private String firstName;
    private String lastName;
    private EmployeeInfo info;
    private Date joinDate;
}

public class EmployeeInfo {
    private String bio;
    private Integer age;
    private String[] interests;
}

EmployeeInfo info = new EmployeeInfo();
info.setBio("curious and modest");
info.setAge(30);
info.setInterests(new String[]{"bike", "climb"});

Employee employee = new Employee();
employee.setEmail("[email protected]");
employee.setFirstName("san");
employee.setLastName("zhang");
employee.setInfo(info);
employee.setJoinDate(new Date());

两张表: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面向文档的json数据结构:

{
    "email":"[email protected]",
    "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?v

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1555412142 10:55:42  elasticsearch green           1         1      2   2    0    0        0             0                  -                100.0%

快速了解集群的健康状况,查看status参数值即可

  • green: 每个索引的primary shard和replica shard都是active状态
  • yellow: 每个索引的primary shard都是active状态,但是部分的replica shard不是active状态,处于不可用的状态
  • red: 不是所有的索引的primary shard都是active状态,部分索引有数据的丢失

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

GET /_cat/indices?v

health status index                uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_task_manager q25yU7fCQlKw5PnMwe-IPA   1   0          2            0     45.5kb         45.5kb
green  open   .kibana_1            u3ZsZEtUQCiIFpng4Z-Mww   1   0          3            0     14.2kb         14.2kb

(3)简单的索引操作

创建索引

PUT /test_index?pretty

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

删除索引

DELETE /test_index?pretty

{
  "acknowledged" : true
}

(4)商品的CRUD操作

1、新增商品:新增文档,建立索引
格式

PUT /{index}/{type}/{id}
{
    "json数据"
}
PUT /product/_doc/1
{
  "name":"gaolujie yagao",
  "desc": "gaoxiao meibai",
  "price":30,
  "producer":"gaolujie producer",
  "tags":["meibai", "fangzhu"]
}

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

PUT /product/_doc/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}

PUT /product/_doc/3
{
  "name":"zhonghua yagao",
  "desc": "caoben zhiwu",
  "price":40,
  "producer"  :"zhonghua producer",
  "tags":["qingxin"]
}

这里不用事先创建好索引index和类型type,ES会默认对document每个field都建立倒排索引,让其可以被搜索
2、查询商品:检索文档
格式:

GET /{index}/{type}/{id}
GET /product/_doc/1

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "gaolujie yagao",
    "desc" : "gaoxiao meibai",
    "price" : 30,
    "producer" : "gaolujie producer",
    "tags" : [
      "meibai",
      "fangzhu"
    ]
  }
}

3、修改商品:替换文档
格式:

PUT /{index}/{type}/{id}
{
    "json数据"
}
PUT /product/_doc/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

替换方式有一个不好,替换时必须带上所有的fields,才能达到我们想要的修改效果
举个例子,如果执行

PUT /product/_doc/1
{
    "name" : "jiaqiangban gaolujie yagao"
}
GET /product/_doc/1
{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "jiaqiangban gaolujie yagao"
  }
}

就不是我们想要的了
4、修改商品:更新文档
格式

POST /{index}/_update/{id}

虽然本质还是一样的,但是进行替换处理的操作全部放在了ES内部,我们传输的数据只需要传需要修改的字段即可,大大降低了在批量处理时的网路带宽,提高了性能。
下面是展示的例子:

GET /product/_doc/1
{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "_seq_no" : 5,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "jiaqiangban gaolujie yagao",
    "desc" : "gaoxiao meibai",
    "price" : 30,
    "producer" : "gaolujie producer",
    "tags" : [
      "meibai",
      "fangzhu"
    ]
  }
}
POST /product/_update/1
{
  "doc":{
    "name": "jiajieshi yagao"
  }
}
GET /product/_doc/1
{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "_seq_no" : 6,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "jiajieshi yagao",
    "desc" : "gaoxiao meibai",
    "price" : 30,
    "producer" : "gaolujie producer",
    "tags" : [
      "meibai",
      "fangzhu"
    ]
  }
}

从这个例子就可以看出update操作成功了
5、删除商品:删除文档
格式:

DELETE /{index}/{type}/{id}
DELETE /product/_doc/1
{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 6,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 1
}

你可能感兴趣的:(elasticsearch)