写在前面的话:读书破万卷,编码若有神
--------------------------------------------------------------------html
参考内容:java
《Elasticsearch顶尖高手系列-快速入门篇》,中华石杉node
--------------------------------------------------------------------数据库
主要内容包括:json
--------------------------------------------------------------------数据结构
一、es的document数据格式和数据库的关系型数据格式的区别elasticsearch
好比有以下的json:网站
1 { 2 "email":"zhangsan@sina.com", 3 "first_name":"san", 4 "last_name":"zhang", 5 "info":{ 6 "bio":"curious and modest", 7 "age":30, 8 "interests":["bike","climb"] 9 "join_date":"2017/01/01" 10 } 11 }
es的docuement能够直接用上面的json数据格式来表达。ui
可是在java中须要两个类来表达:编码
1 public class Employee { 2 3 private String email; 4 private String firstName; 5 private String lastName; 6 private EmployeeInfo info; 7 8 } 9 10 private class EmployeeInfo { 11 12 private String bio; // 性格 13 private Integer age; 14 private String[] interests; // 兴趣爱好 15 private Date joinDate; 16 }
能够看出employee对象里面包含了Employee类本身的属性,并且还有一个EmployeeInfo对象。
在数据库中的话,就须要两张表: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的document数据格式和数据库的关系型数据格式的区别:
--------------------------------------------------------------------
二、简单的集群管理
(2.1)快速检查集群的健康情况
在Kibana中执行以下命令: GET _cat/health?v
1 epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 2 1518657893 09:24:53 huobaopaocai-es-cluster yellow 1 1 1 1 0 0 1 0 - 50.0%
如何快速的了解集群的健康状态? green、yellow、red
为何如今咱们的是处于yellow状态?
咱们如今就一个笔记本电脑,就启动了一个es进程,至关于就只有一个node。如今es中有一个index,就是kibana本身内置创建的index。因为默认的配置是给每一个index分配5个primary shard和5个replica shard,并且primary shard和replica shard不能在同一台机器上(为了容错)。如今kibana本身创建的index是1个primary shard和1个replica shard。当前就一个node,因此只有1个primary shard被分配了和启动了,可是一个replica shard没有第二台机器去启动。
(2.2)快速查看集群中有些索引
在Kibana中执行以下命令: GET _cat/indices?v
1 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 2 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb
(2.3)简单的索引操做
建立索引: PUT /test_index?pretty
1 { 2 "acknowledged": true, 3 "shards_acknowledged": true 4 }
再次查看索引:
1 GET _cat/indices?v 2 3 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 4 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb 5 yellow open test_index i2LdlSIqRXCZQCLauVBiRw 5 1 0 0 650b 650b
删除索引: DELETE /test_index?pretty
{ "acknowledged": true }
再次查看索引:
1 GET _cat/indices?v 2 3 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 4 yellow open .kibana CaxZ5uJGSJy3rCzv_3RIzQ 1 1 1 0 3.1kb 3.1kb
--------------------------------------------------------------------
三、电商网站商品管理案例背景介绍
有一个电商网站,须要为其基于ES构建一个后台系统,提供如下功能:
--------------------------------------------------------------------
四、商品的CRUD操做(document 的CRUD操做 )
(4.1)新增商品:新增文档、创建索引
基本语法格式:
1 PUT /index/type/id 2 { 3 "json数据" 4 }
准备三条数据:
1 PUT /ecommerce/product/1 2 { 3 "name" : "gaolujie yagao", 4 "desc" : "gaoxiao meibai", 5 "price" : 30, 6 "producer" : "gaolujie producer", 7 "tags": [ "meibai", "fangzhu" ] 8 } 9 10 PUT /ecommerce/product/2 11 { 12 "name" : "jiajieshi yagao", 13 "desc" : "youxiao fangzhu", 14 "price" : 25, 15 "producer" : "jiajieshi producer", 16 "tags": [ "fangzhu" ] 17 } 18 19 PUT /ecommerce/product/3 20 { 21 "name" : "zhonghua yagao", 22 "desc" : "caoben zhiwu", 23 "price" : 40, 24 "producer" : "zhonghua producer", 25 "tags": [ "qingxin" ] 26 }
执行每条新增语句的结果:
1 { 2 "_index": "ecommerce", 3 "_type": "product", 4 "_id": "1", 5 "_version": 1, 6 "result": "created", 7 "_shards": { 8 "total": 2, 9 "successful": 1, 10 "failed": 0 11 }, 12 "created": true 13 }
(ps:es会自动创建index和type,不须要提早建立,并且es默认会对document每一个field创建倒排索引,让其能够被搜索。)
用java来实现往es中添加docuemnt操做
@Autowired private ElasticsearchConstant elasticsearchConstant; /** * 新增es的docuemnt */ @Test public void createDocumentTest() throws IOException { TransportClient client = elasticsearchConstant.getClient(); IndexResponse response = client.prepareIndex(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(),"1") .setSource(XContentFactory.jsonBuilder() .startObject() .field("name","gaolujie yagao") .field("desc", "gaoxiao meibai") .field("price", 30) .field("producer", "gaolujie producer") .endObject()) .get(); LOG.info(String.format("新增es的docuemnt结果: %s",response.toString())); } 执行结果: 2018-02-15 11:37:24 INFO [main] (EcommerceTest.java:56) createDocumentTest - 新增es的docuemnt结果: IndexResponse[index=ecommerce,type=product,id=1,version=1,result=created,shards={"total":2,"successful":1,"failed":0}]
(4.2)检索索引:检索文档
基本语法格式:
1 GET /index/type/id
用java来实现查询es中docuemnt操做
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 查询es的document 6 */ 7 @Test 8 public void getDocumentTest() throws IOException{ 9 TransportClient client = elasticsearchConstant.getClient(); 10 GetResponse response = client.prepareGet(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get(); 11 LOG.info(String.format("查询es的docuemnt结果: %s",response.toString())); 12 } 13 14 执行结果: 15 2018-02-15 11:42:19 INFO [main] (EcommerceTest.java:68) getDocumentTest - 查询es的docuemnt结果: {"_index":"ecommerce","_type":"product","_id":"1","_version":1,"found":true,"_source":{"name":"gaolujie yagao","desc":"gaoxiao meibai","price":30,"producer":"gaolujie producer"}}
(4.3)修改商品:替换文档
(ps:替换方式有一个很差:必须带上全部的field才能去进行信息的修改。)
(4.4)修改商品:更新文档
用java来实现编辑es中的docuement:
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 更新es的docuemnt 6 * @throws IOException 7 */ 8 @Test 9 public void updateDocument() throws IOException { 10 TransportClient client = elasticsearchConstant.getClient(); 11 UpdateResponse updateResponse = client.prepareUpdate(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1") 12 .setDoc(jsonBuilder() 13 .startObject() 14 .field("name", "jiaqiang gaolujie yagao") 15 .endObject()) 16 .get(); 17 LOG.info(String.format("更新es的docuemnt结果: %s",updateResponse.toString())); 18 } 19 20 运行结果: 21 2018-02-15 15:55:13 INFO [main] (EcommerceTest.java:73) updateDocument - 更新es的docuemnt结果: UpdateResponse[index=ecommerce,type=product,id=1,version=2,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]
(4.5)删除商品:删除文档
用java来实现删除es中的docuement:
1 @Autowired 2 private ElasticsearchConstant elasticsearchConstant; 3 4 /** 5 * 删除es中的document 6 */ 7 @Test 8 public void deleteDocument(){ 9 TransportClient client = elasticsearchConstant.getClient(); 10 DeleteResponse response = client.prepareDelete(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get(); 11 LOG.info(String.format("删除es的docuemnt结果: %s",response.toString())); 12 } 13 14 运行结果: 15 2018-02-15 16:00:04 INFO [main] (EcommerceTest.java:84) deleteDocument - 删除es的docuemnt结果: DeleteResponse[index=ecommerce,type=product,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]