ES-实战入门

Posted TGB-Earnest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ES-实战入门相关的知识,希望对你有一定的参考价值。

1、document数据格式

面向文档的搜索分析引擎
(1)应用系统的数据结构都是面相对象的,复杂的。

public class Employee
    private String email;
    private String firstName;
    private EmployeeInfo info;

public class EmployeeInfo
    private String bio;
    private Integer age;
    private String[] interes;


EmployeeInfo info = new EmployeeInfo();
info.setBio("curios and modest")
    info.setage(30);
info.setInterests(new String[]"bike","climb");

Employee employee = new Employee();
employee.setEmail("zhangsna@164.com")
    employee.setFirstName("san")
        empolyee.setInfo(info)

employee对象;包含了Employee类自己的属性还有一个info对象,
当我们存到关系型数据库的时候,还需要拆开到两个表中,但是ES不用,所以是面相对象。
(2)对象数据存储到数据库中,只能拆解开来,变为扁平的多个表,每次查询的时候还得还原回原来对象格式,相当麻烦
(3) ES 是面相文档的,文档中存储的数据结构,与面向对象的数据结构是一样的。
(4)ES的document用JSON数据格式表达

2、电商网站商品管理案例背景介绍

(1)对商品CRUD
(2) 执行简单结构化查询
(3)可以执行简单的全文检索,以及复杂的短语检索

3、简单集群操作

(1)快速检查集群的健康状况

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
1663923665 09:01:05  elasticsearch green           1         1      3   3    0    0        0             0                  -                100.0%

如何快速了解集群的健康状况?green、yellow、red?
status

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

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

Get /_cat/indices?v

(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

PUT /ecommerce/product/1

  "name":"gaolujie yagao",
  "desc":"gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer"


返回的值为:

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

PUT /ecommerce/product/1

  "name":"gaolujie yagao",
  "desc":"gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer"

PUT /ecommerce/product/2

  "name":"jiajieshi yagao",
  "desc":"youxiaofangzhu",
  "price": 25,
  "producer": "jiajieshi producer"

PUT /ecommerce/product/3

  "name":"zhonghua yagao",
  "desc":"caobenzhiwu",
  "price": 40,
  "producer": "zhonghua producer"

es 会自动创建index和type ,不需要提前创建,而且es默认会对ducument每个field都建立倒排索引

(2) 查询商品:检索文档

GET /index/type/id
GET /ecommerce/product/1

  "_index" : "ecommerce",
  "_type" : "product",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : 
    "name" : "gaolujie yagao",
    "desc" : "gaoxiao meibai",
    "price" : 30,
    "producer" : "gaolujie producer"
  

(3) 修改商品:替换文档

PUT /ecommerce/product/1

  "name":"jiajiehsi yagao",
  "desc":"gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer"



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

这里变成了updated

替换方式有一个不好,必须带上所有的filed,才能去进行信息的修改。

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

POST /ecommerce/product/1/_update (API)

  "doc":
    "name":"jiaqiangban gaolujieyagao"
  



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

(5) 删除商品

DELETE /ecommerce/product/1


  "_index" : "ecommerce",
  "_type" : "product",
  "_id" : "1",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : 
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  ,
  "_seq_no" : 5,
  "_primary_term" : 1



GET /ecommerce/product/1



  "_index" : "ecommerce",
  "_type" : "product",
  "_id" : "1",
  "found" : false

以上是关于ES-实战入门的主要内容,如果未能解决你的问题,请参考以下文章

ES-实战入门

ElasticSearch分布式搜索引擎从入门到实战应用(入门篇)

ES6常用五大特性

【ES从入门到实战】五、全文检索-ElasticSearch-入门-put&post新增数据

JavaSE入门学习12: Java面相对象之static用法

【ES从入门到实战】十七、全文检索-ElasticSearch-进阶-aggregations聚合分析