ES是一款面向文档的数据搜索、分析引擎.
document结构说明:
(1) 基于面向对象的开发思想, 应用系统中的数据结构都是很复杂的: 对象中嵌套对象, 如CRM系统中的客户对象中, 还会嵌入客户相关的企业对象.
(2) 对象数据存储到数据库中, 需要分解, 将嵌套对象分解为扁平的多张表数据, 每次操作时需要还原回对象格式, 过程繁琐.
(3) ES存储的是JSON格式的文档, 基于此, ES可以提供复杂的索引, 全文检索, 分析聚合等功能.
(4) document格式示例:
{ "id": "5220", "name": "张三", "sex": "男", "age": 25, "phone": 13312345678, "email": "zhangsan@163.com", "company": { "name": "Alibaba", "location": "杭州" }, "join_date": "2018/11/01" } 2 document的常见CRUD操作背景说明: 以电商系统的搜索子系统为例, 说明常用操作.
2.1 添加商品: 添加文档并建立索引
添加API:
PUT /index/type/id { "JSON格式的文档数据" }说明: ES会自动创建PUT API中指定的index和type, 不需要提前创建;
而且ES默认对document的每个field都建立倒排索引, 保证它们都可以被检索.
添加示例:
PUT /shop/it_book/1 { "name": "Java编程思想", "author": "[美] Bruce Eckel", "desc": "Java学习必读经典,殿堂级著作!", "price": 109, "date": "2007-06", "publisher": "机械工业出版社", "tags": [ "Java", "编程语言" ] }
响应信息:
{ "_index": "shop", "_type": "it_book", "_id": "1", "_version": 1, "result": "created", # 操作结果: created(创建)了索引 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true # 是否为创建操作 }
再添加如下数据:
PUT /shop/it_book/2 { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "author": "周志明", "desc": "Java图书领域公认的经典著作", "price": 79, "date": "2013-06", "publisher": "机械工业出版社", "tags": [ "Java", "虚拟机", "最佳实践" ] } PUT /shop/it_book/3 { "name": "Java并发编程的艺术", "author": "方腾飞,魏鹏,程晓明", "desc": "阿里系工程师的并发编程实践", "price": 59, "date": "2015-07", "publisher": "机械工业出版社", "tags": [ "Java", "并发编程" ] } 2.2 查询商品: 检索文档
检索API:
GET /index/type/id
检索示例:
GET /shop/it_book/2
响应信息:
{ "_index": "shop", "_type": "it_book", "_id": "2", "_version": 1, "found": true, "_source": { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "author": "周志明", "desc": "Java图书领域公认的经典著作", "price": 79, "date": "2013-06", "publisher": "机械工业出版社", "tags": [ "Java", "虚拟机", "最佳实践" ] } } 2.3 修改商品: 替换文档
替换API - 与添加API相同, 只不过文档id要存在, 此时系统将判定为修改操作:
—— 无论文档数据是否存在修改, 对同一id的文档执行1次以上的PUT操作, 都是修改操作.
PUT /index/type/id { "JSON格式的文档数据" }注意: 替换方式的不便之处: 必须填写要修改文档的所有field, 如果缺少, 修改后的文档中将丢失相关field.
替换示例 - 为name添加了"(第4版)"
PUT /shop/it_book/1 { "name": "Java编程思想(第4版)", "author": "[美] Bruce Eckel", "desc": "Java学习必读经典,殿堂级著作!", "price": 109, "date": "2007-06", "publisher": "机械工业出版社", "tags": [ "Java", "编程语言" ] }
响应信息:
{ "_index": "shop", "_type": "it_book", "_id": "1", "_version": 2, "result": "updated", // 操作结果: updated(修改)了索引 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false // 是否为创建操作 } 2.4 修改商品: 更新文档
更新API:
—— 指定更新文档的某些field, 其他的不予理会.
POST /index/type/id/_update { "doc": { "field u want to update": "new value of ur update's field" } }注意: 与替换方式相比, 更新方式的好处: 可以更新指定文档的指定field, 未指定的field也不会丢失.
更新示例 - 将name改为英文:
POST /shop/it_book/1/_update { "doc": { "name": "Thinking in Java(4th Edition) " } }
响应信息:
{ "_index": "shop", "_type": "it_book", "_id": "1", "_version": 4, "result": "updated", // 操作结果: updated(修改)了索引 "_shards": { "total": 2, "successful": 1, "failed": 0 } }此时查看该文档 GET /shop/it_book/1 即可发现更新成功.
2.5 删除商品: 删除文档
删除API:
—— 指定更新文档的某些field, 其他的不予理会.
DELETE /index/type/id
删除示例:
DELETE /shop/it_book/1
响应信息:
{ "found": true, "_index": "shop", "_type": "it_book", "_id": "1", "_version": 5, "result": "deleted", // 操作结果: deleted(删除)了索引 "_shards": { "total": 2, "successful": 1, "failed": 0 } }
再次查看删除的文档:
GET /shop/it_book/1
响应信息:
{ "_index": "shop", "_type": "it_book", "_id": "1", "found": false // 没有查找到相关文档 }