ElasticSearch入门实战1

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: document数据格式电商网站商品管理案例背景介绍简单的集群管理商品的CRUD操作(document curd)1. Document数据格式面向文档的搜索分析引擎应用系统的数据结构都是面向对象的,复杂的对象数据存储到数据库中,只能拆解开来,变成扁平的多张表,每次查询的时候还有还原成对象格式,相当麻烦ES是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档数据结构。
  1. document数据格式
  2. 电商网站商品管理案例背景介绍
  3. 简单的集群管理
  4. 商品的CRUD操作(document curd)

1. Document数据格式

面向文档的搜索分析引擎

  • 应用系统的数据结构都是面向对象的,复杂的
  • 对象数据存储到数据库中,只能拆解开来,变成扁平的多张表,每次查询的时候还有还原成对象格式,相当麻烦
  • ES是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档数据结构。ES可以提供复杂的索引,全文检索,分析聚合等功能。
  • ES的document用json数据格式来表示的。
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[] interesets;
}

EmployeeInfo info = new EmployeeInfo();
info.SetBio("youdi");
info.setAge(24);
info.setInteresets(new String[]{"dance", "code"});

Employee employee = new Employee();
employee.setEmail("@");
employee.setInfo(info);

employee对象:里面包含了Employee类自己属性。需要保存在两张表。
使用ORM.数据库是扁平化的,不能体现出面向对象的结构。

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

提供功能如下:

  1. 对商品信息进行CRUD
  2. 执行简单的全文检索,以及复杂的phrase检索
  3. 对于全文检索结果,可以进行高亮显示
  4. 对数据进行简单的聚合分析

3. 简单的集群管理

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

    ES提供了一套cat api,可以查看es中各种各样的数据

    GET /_cat/health?pretty
    epoch      timestamp cluster             status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1524842572 23:22:52  elasticsearch_youdi yellow          1         1     10  10    0    0       10             0                  -                 50.0%
    

    查看status

    Green: 每个索引的primary shard和replica shard 都是active状态

    yellow: 每个索引的primary shard是active状态,但是部分replica shard不是active状态,处于不可用的状态

    red: 不是所有的索引的primary shard都是active状态,部分缩影有数据丢失

为什么现在会处于yellow状态?

primary shard replica shard没有第二个节点

  1. 快速查看集群中所有索引?
GET /_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   website  -NVtisruRUO1buiENwh7Vw   5   1          1            0      5.5kb          5.5kb
yellow open   megacorp 3YlG1FRPTDynSETmZBDIhg   5   1          3            0     17.5kb         17.5kb
  1. 简单的索引操作
PUT /test_index?pretty


DELETE /test_index?pretty

商品的CURD

  1. 新增商品
PUT /index/type/id

PUT /products/goods/1
{
  "name": "test",
  "desc": "hello world",
  "price": 11.0,
  "producer": "hello",
  "tags": ["youdi", "haha"]
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
  1. 查询商品
GET /products/goods/1

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "test",
    "desc": "hello world",
    "price": 11,
    "producer": "hello",
    "tags": [
      "youdi",
      "haha"
    ]
  }
}
  1. 更新操作
替换方式: 必须提交所有的信息
PUT /products/goods/1
{
  "name": "test3",
  "desc": "hello world",
  "price": 11.5,
  "producer": "hello",
  "tags": ["youdi", "haha"]
}


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



PUT /products/goods/1
{
  "name": "test3"
}

GET /products/goods/1
{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "name": "test3"
  }
}


POST products/goods/1/_update
{
  "doc": {
  "name": "test6"
  }
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 5,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}
  1. 删除文档
DELETE /products/goods/1
{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 6,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "found": false
}

商品的搜索方式

  1. query string search
  2. Query DSL
  3. query filter
  4. Full-text search

1. query string search

搜索全部商品

GET /products/goods/_search

{
  "took": 104, //耗时
  "timed_out": false, //是否超时
  "_shards": { //_shards 
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1, // 查询结果的数量 1条数据
    "max_score": 1, //ES对相关度的匹配分数
    "hits": [  //包含了搜索的详细数据
      {
        "_index": "products",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "tes2",
          "desc": "hello world",
          "price": 14,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      }
    ]
  }
}



{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "products",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "tes2",
          "desc": "hello world",
          "price": 14,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      },
      {
        "_index": "products",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "test3",
          "desc": "hello world",
          "price": 11.5,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      }
    ]
  }
}


      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      },
  1. 搜索账号名字中有ber,而且按照年龄排序
GET /bank/_doc/_search?q=firstname:Virginia&sort=age:des

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 4.882802,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 4.882802,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      }
    ]
  }
}
  1. query DSL

DSL:Domain specified Language 特定领域的语言

查询所有的account

http request body :请求体,可以使用json的格式构建查询语法,比较方便,可以构建各种复杂的语法。

GET /bank/_doc/_search
{
  "query": {
    "match_all": {}
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "_source": {
          "account_number": 44,
          "balance": 34487,
          "firstname": "Aurelia",
          "lastname": "Harding",
          "age": 37,
          "gender": "M",
          "address": "502 Baycliff Terrace",
          "employer": "Orbalix",
          "email": "aureliaharding@orbalix.com",
          "city": "Yardville",
          "state": "DE"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "_source": {
          "account_number": 99,
          "balance": 47159,
          "firstname": "Ratliff",
          "lastname": "Heath",
          "age": 39,
          "gender": "F",
          "address": "806 Rockwell Place",
          "employer": "Zappix",
          "email": "ratliffheath@zappix.com",
          "city": "Shaft",
          "state": "ND"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "_source": {
          "account_number": 119,
          "balance": 49222,
          "firstname": "Laverne",
          "lastname": "Johnson",
          "age": 28,
          "gender": "F",
          "address": "302 Howard Place",
          "employer": "Senmei",
          "email": "lavernejohnson@senmei.com",
          "city": "Herlong",
          "state": "DC"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "_source": {
          "account_number": 126,
          "balance": 3607,
          "firstname": "Effie",
          "lastname": "Gates",
          "age": 39,
          "gender": "F",
          "address": "620 National Drive",
          "employer": "Digitalus",
          "email": "effiegates@digitalus.com",
          "city": "Blodgett",
          "state": "MD"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "_source": {
          "account_number": 145,
          "balance": 47406,
          "firstname": "Rowena",
          "lastname": "Wilkinson",
          "age": 32,
          "gender": "M",
          "address": "891 Elton Street",
          "employer": "Asimiline",
          "email": "rowenawilkinson@asimiline.com",
          "city": "Ripley",
          "state": "NH"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "_source": {
          "account_number": 183,
          "balance": 14223,
          "firstname": "Hudson",
          "lastname": "English",
          "age": 26,
          "gender": "F",
          "address": "823 Herkimer Place",
          "employer": "Xinware",
          "email": "hudsonenglish@xinware.com",
          "city": "Robbins",
          "state": "ND"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "_source": {
          "account_number": 190,
          "balance": 3150,
          "firstname": "Blake",
          "lastname": "Davidson",
          "age": 30,
          "gender": "F",
          "address": "636 Diamond Street",
          "employer": "Quantasis",
          "email": "blakedavidson@quantasis.com",
          "city": "Crumpler",
          "state": "KY"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "_source": {
          "account_number": 208,
          "balance": 40760,
          "firstname": "Garcia",
          "lastname": "Hess",
          "age": 26,
          "gender": "F",
          "address": "810 Nostrand Avenue",
          "employer": "Quiltigen",
          "email": "garciahess@quiltigen.com",
          "city": "Brooktrails",
          "state": "GA"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "_source": {
          "account_number": 222,
          "balance": 14764,
          "firstname": "Rachelle",
          "lastname": "Rice",
          "age": 36,
          "gender": "M",
          "address": "333 Narrows Avenue",
          "employer": "Enaut",
          "email": "rachellerice@enaut.com",
          "city": "Wright",
          "state": "AZ"
        }
      }
    ]
  }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
1月前
|
存储 Java API
Elasticsearch 7.8.0从入门到精通
这篇文章详细介绍了Elasticsearch 7.8.0的安装、核心概念(如正排索引和倒排索引)、RESTful风格、各种索引和文档操作、条件查询、聚合查询以及在Spring Boot中整合Elasticsearch的步骤和示例。
112 1
Elasticsearch 7.8.0从入门到精通
|
2月前
|
数据可视化 Java Windows
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
本文介绍了如何在Windows环境下安装Elasticsearch(ES)、Elasticsearch Head可视化插件和Kibana,以及如何配置ES的跨域问题,确保Kibana能够连接到ES集群,并提供了安装过程中可能遇到的问题及其解决方案。
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
|
2月前
|
存储 关系型数据库 MySQL
浅谈Elasticsearch的入门与实践
本文主要围绕ES核心特性:分布式存储特性和分析检索能力,介绍了概念、原理与实践案例,希望让读者快速理解ES的核心特性与应用场景。
|
3月前
|
JSON 搜索推荐 数据挖掘
ElasticSearch的简单介绍与使用【入门篇】
这篇文章是Elasticsearch的入门介绍,涵盖了Elasticsearch的基本概念、特点、安装方法以及如何进行基本的数据操作,包括索引文档、查询、更新、删除和使用bulk API进行批量操作。
ElasticSearch的简单介绍与使用【入门篇】
|
2月前
|
JSON 监控 Java
Elasticsearch 入门:搭建高性能搜索集群
【9月更文第2天】Elasticsearch 是一个分布式的、RESTful 风格的搜索和分析引擎,基于 Apache Lucene 构建。它能够处理大量的数据,提供快速的搜索响应。本教程将指导你如何从零开始搭建一个基本的 Elasticsearch 集群,并演示如何进行简单的索引和查询操作。
191 3
|
3月前
|
JSON 测试技术 API
黑马商城 Elasticsearch从入门到部署 RestClient操作文档
这篇文章详细介绍了如何使用Java的RestHighLevelClient客户端与Elasticsearch进行文档操作,包括新增、查询、删除、修改文档以及批量导入文档的方法,并提供了相应的代码示例和操作步骤。
|
3月前
|
JSON 自然语言处理 Java
Elasticsearch从入门到部署 文档操作 RestAPI
这篇文章详细介绍了Elasticsearch中文档的增删改查操作,并通过Java的RestHighLevelClient客户端演示了如何通过REST API与Elasticsearch进行交云,包括初始化客户端、索引库的创建、删除和存在性判断等操作。
|
3月前
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
4月前
|
存储 数据采集 数据处理
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
数据处理神器Elasticsearch_Pipeline:原理、配置与实战指南
171 12
|
5月前
|
缓存 数据处理 数据安全/隐私保护
Elasticsearch索引状态管理实战指南
Elasticsearch索引状态管理实战指南