博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文档CURD操作
阅读量:6243 次
发布时间:2019-06-22

本文共 3966 字,大约阅读时间需要 13 分钟。

hot3.png

文档CURD操作

1、document数据格式

面向文档的搜索分析引擎,其实就是json格式(1)应用系统的数据结构都是面向对象的,复杂的(2)对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得还原回对象格式,相当麻烦(3)ES是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档数据结构,es可以提供复杂的索引,全文检索,分析聚合等功能(4)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[] 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("zhangsan@sina.com");employee.setFirstName("san");employee.setLastName("zhang");employee.setInfo(info);employee.setJoinDate(new Date());

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表{    "email":      "zhangsan@sina.com",    "first_name": "san",    "last_name": "zhang",    "info": {        "bio":         "curious and modest",        "age":         30,        "interests": [ "bike", "climb" ]    },    "join_date": "2017/01/01"}

我们就明白了es的document数据格式和数据库的关系型数据格式的区别

2、商品的CRUD操作(document CRUD操作)

(1)新增商品:新增文档,建立索引

PUT /index/type/id{  "json数据"}PUT /ecommerce/product/1{	"name" : "gaolujie yagao",	"desc" : "gaolujie meibai",	"price" : 30,	"producer" : "gaolujie producer",	"tags" : ["meibai","fangzhu"]}结果:{  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "_version": 1,  "result": "created",  "_shards": {    "total": 2,    "successful": 1,    "failed": 0  },  "created": true}

PUT /ecommerce/product/2{  "name" : "jiajieshi yagao",  "desc" : "youxiao fangzhu",  "price" : 25,  "producer" : "jiajieshi producer",  "tags" : ["fangzhu"]}PUT /ecommerce/product/3{  "name" : "zhonghua yagao",  "desc" : "caoben zhiwu",  "price" : 40,  "producer" : "zhonghua producer",  "tags" : ["qinxin"]}***ES会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索

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

GET /index/type/id

GET /ecommerce/product/3结果:{  "_index": "ecommerce",  "_type": "product",  "_id": "3",  "_version": 1,  "found": true,  "_source": {    "name": "zhonghua yagao",    "desc": "caoben zhiwu",    "price": 40,    "producer": "zhonghua producer",    "tags": [      "qinxin"    ]  }}

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

PUT /ecommerce/product/1{  "name" : "jiaqiangban gaolujie yagao",  "desc" : "gaolujie meibai",  "price" : 30,  "producer" : "gaolujie producer",  "tags" : ["meibai","fangzhu"]}{  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "_version": 2,  "result": "updated",  "_shards": {    "total": 2,    "successful": 1,    "failed": 0  },  "created": false}替换方式的缺点:即使必须带上所有的field,才能去进行信息的修改

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

POST /ecommerce/product/1/_update{  "doc" : {    "name" : "gaolujie yagao"  }}{  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "_version": 3,  "result": "updated",  "_shards": {    "total": 2,    "successful": 1,    "failed": 0  }}GET /ecommerce/product/1{  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "_version": 3,  "found": true,  "_source": {    "name": "gaolujie yagao",    "desc": "gaolujie meibai",    "price": 30,    "producer": "gaolujie producer",    "tags": [      "meibai",      "fangzhu"    ]  }}

(5)删除商品:删除文档

DELETE /ecommerce/product/1{  "found": true,  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "_version": 4,  "result": "deleted",  "_shards": {    "total": 2,    "successful": 1,    "failed": 0  }}GET /ecommerce/product/1{  "_index": "ecommerce",  "_type": "product",  "_id": "1",  "found": false}

最后: 补回去,后面学习有用

PUT /ecommerce/product/1{  "name" : "gaolujie yagao",  "desc" : "gaoxiao meibai",  "price" : 30,  "producer" : "gaolujie producer",  "tags" : ["meibai","fangzhu"]}

转载于:https://my.oschina.net/grittan/blog/3018047

你可能感兴趣的文章
市场营销进入大数据时代
查看>>
sudo命令:解决使用Linux命令行时出现的错误提示
查看>>
Linux的IRQ中断子系统分析
查看>>
使用FreeMarker替换JSP的10个理由
查看>>
阿里云创建E-MapReduce 2 创建集群
查看>>
白帽子认为2017年网络安全的头号威胁是大规模监控
查看>>
前端JS如何获取主域名(根域名)
查看>>
VR技术行业应用前景初探:技术创新定义精彩未来
查看>>
知识产权攻击是从哪冒出来的?
查看>>
宽带服务商设局,美国法律这么治
查看>>
混合IT架构的最佳实践
查看>>
一文详解神经网络 BP 算法原理及 Python 实现
查看>>
高通与联想达成新专利许可协议
查看>>
阿里科学家王刚、吴翰清同时入选MIT2017年度TR35 开创中国互联网企业先河
查看>>
继SDS之后又迎来CDM,存储行业真要变天了?
查看>>
美媒:联想电脑威胁五角大楼网络安全
查看>>
绿色智慧城市|城市增长边界的几个误区
查看>>
美国土安全部发警告:尽快卸载QuickTime for Windows
查看>>
《SEO的艺术(原书第2版)》——3.9 为客户挖掘和直接营销开展SEO
查看>>
运营商造梦“管道+内容”三十载不言放弃
查看>>