ES 父子文档查询
父子文档的特点
1. 父/子文档是完全独立的。
2. 父文档更新不会影响子文档。
3. 子文档更新不会影响父文档或者其它子文档。
父子文档的映射与索引
1. 父子关系 type 的建立必须在索引新建或 update-mapping 时候确定好
PUT /company
{
"mappings": {
"branch": {}, //父文档 type
"employee": {
"_parent": {
"type": "branch" //子文档 type
}
}
}
}
2. 父文档的索引和普通文档索引一样。
POST /company/branch/_bulk
{ "index": { "_id": "london" }}
{ "name": "London Westminster", "city": "London", "country": "UK" }
3. 子文档索引必须指定其对应的父文档 ID,作用:
- 建立父子文档之间的关联
- 确保子文档能够被索引到父文档所在分片(parent id 作为 route)
PUT /company/employee/?parent=london //指定 id = london 的父文档
{
"name": "Alice Smith",
"dob": "1970-10-24",
"hobby": "hiking"
}
4. 如果要更改文档的父文档,不能仅仅 update 或者 reindex 旧文档(新的父文档可能在不同分片上),需要先删除旧文档再重新索引。
父子关系的应用
看到 parent-child 关系,我们很容易想到的是像 SQL 那样的各种 JOIN 操作——比如查询某个文档并一并取回所有的父或子文档等。
然而,ES 中不支持类似的 JOIN 查询。即便 child aggregation 也不能做到像 SQL 那样的 JOIN 操作!
在 ES 中的 parent-child 关系基本可以理解为是一个过滤条件,如下:
//查询某文档,只有该文档有"父文档"且满足一定条件才算匹配
{"has_parent": { //文档是否有 parent
"type": "branch", //其 parent 所在 type 必须是 branch
"query": { //其 parent 必须满足以下 query 条件
"match": {
"country": "UK"
}
}
} //如果满足以上条件,hit 该文档
}
//查询某文档,只有该文档有"子文档"且满足一定条件才算匹配
{
"has_child": { //文档是否有 child
"type": "employee", //其 child所在 type 必须是 employee
"query": { //其 parent 必须满足以下 query 条件
"match": {
"name": "Alice Smith"
}
}
} //如果满足以上条件,hit 该文档
}
1. has_child:基于子文档的内容,查找父文档
//请求 GET /company/branch/_search
{
"query": {
"has_child": { //基于 child 的内容,查询满足条件的 parent 文档
"type": "employee",
"query": { //在 child 中执行 match query操作
"match": {
"name": "Alice Smith"
}
}
}
}
}
//结果
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "company",
"_type": "branch", //注意!!!返回的是 parent 的文档
"_id": "london",
"_score": ,
"_source": {
"name": "London Westminster",
"city": "London",
"country": "UK"
}
}
]
}
}
2. has_parent:基于父文档的内容,查找子文档
//请求 GET /company/employee/_search
{
"query": {
"has_parent": { //基于 parent 的内容,查询满足条件的 child 文档
"type": "branch",
"query": { //在 parent 中执行 match query 查询
"match": {
"country": "UK"
}
}
}
}
}
//结果
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "company",
"_type": "employee", //注意!!!返回的是 child 的文档
"_id": "",
"_score": ,
"_routing": "london",
"_parent": "london",
"_source": {
"name": "Alice Smith",
"dob": "1970-10-24",
"hobby": "hiking"
}
}
]
}
}
3. children aggregation:对关联的 child 文档进行聚合操作
//请求 GET /company/branch/_search
{
"size" : ,
"aggs": {
"country": {
"terms": {
"field": "country" //以不同的 country 来分组(桶分)
},
"aggs": {
"employees": {
"children": { //children aggregation,子 type 为 employee
"type": "employee"
},
"aggs": {
"hobby": {
"terms": {
"field": "hobby" //以不同的 hobby 来分组(桶分)
}
}
}
}
}
}
}
}
//结果
"aggregations": {
"country": {
"doc_count_error_upper_bound": ,
"sum_other_doc_count": ,
"buckets": [ //country 聚合结果
{
"key": "uk",
"doc_count": ,
"employees": { //children aggregation 聚合
"doc_count": ,
"hobby": {
"doc_count_error_upper_bound": ,
"sum_other_doc_count": ,
"buckets": [ //hobby 聚合结果
{
"key": "hiking",
"doc_count":
}
]
}
}
}
]
}
}
ES 父子文档查询的更多相关文章
- ElasticSearch 6.x 父子文档[join]分析
ES6.0以后,索引的type只能有一个,使得父子结构变的不那么清晰,毕竟对于java开发者来说,index->db,type->table的结构比较容易理解. 按照官方的说明,之前一个索 ...
- elasticsearch父子文档处理(join)
elasticsearch父子文档处理 join 一.背景 二.需求 三.前置知识 四.实现步骤 1.创建 mapping 2.添加父文档数据 3.添加子文档 4.查询文档 1.根据父文档id查询它下 ...
- 015-elasticsearch5.4.3【五】-搜索API【四】Joining 多文档查询、GEO查询、moreLikeThisQuery、script脚本查询、span跨度查询
一.Joining 多文档查询 joining query 像Elasticsearch这样的分布式系统中执行完整的SQL样式连接非常昂贵.相反,Elasticsearch提供两种形式的连接,旨在水平 ...
- elasticsearch 父子文档(十一)
说明 需求 一个产品多个区域销售 每个区域有自己的价格, 方式1冗余行,a 产品分别在 area1 area2 area3区域销售 a产品就会生成3条产品数据 搜索id去重就行了,但是问题就是 聚合 ...
- Elasticsearch必知必会的干货知识一:ES索引文档的CRUD
若在传统DBMS 关系型数据库中查询海量数据,特别是模糊查询,一般我们都是使用like %查询的值%,但这样会导致无法应用索引,从而形成全表扫描效率低下,即使是在有索引的字段精确值查找,面对海量数 ...
- Elasticsearch增删改查 之 —— mget多文档查询
之前说过了针对单一文档的增删改查,基本也算是达到了一个基本数据库的功能.本篇主要描述的是多文档的查询,通过这个查询语法,可以根据多个文档的查询条件,返回多个文档集合. 更多内容可以参考我整理的ELK文 ...
- Elasticsearch文档查询
简单数据集 到目前为止,已经了解了基本知识,现在我们尝试用更逼真的数据集,这儿已经准备好了一份虚构的JSON,关于客户银行账户信息的.每个文档的结构如下: { , , "firstname& ...
- SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)
一.简单介绍 Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...
随机推荐
- js获取服务端IP及端口及协议
alert("location:"+window.location); alert("href: "+window.location.href); alert( ...
- 修改eclipse默认编码方式
设置js文件的默认编码格式为UTF-8 在Windows->Preference页面中,选择General->Content Types ...
- Umbraco 上传文件到另一个文件夹,而不是media files
If you want to upload there media files to another place in the same instance of IIS, for example a ...
- Java常用类(String、StringBuffer、Math、Arrays)
1.String 操作对象时会重新分配堆内存,栈内存的引用会重新指向新的堆内存 2.StringBuffer(字符串缓存区) 操作的对象一直都是一个 3.Math Math.max(xx,xx); M ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- GDI+与图形编程研究
GDI+的基本概念 GDI+的常用对象,包括Graphics.Font.Brush.Pen等对象的创建和使用 常用图形的绘制 Color结构.Point结构和Rectangle结构 1.GDI+的概念 ...
- C# 制作外挂常用的API
C#做外挂的常用API,本人用了很久,基本没发现问题 using System; using System.Collections.Generic; using System.Text; using ...
- EasyUI改动DateBox和DateTimeBox的默认日期格式
近期整理Easyui控件的时候,对Easyui的DateBox控件和DateTimeBox控件进行了梳理,而我之所以将EasyUI的DateBox控件和DateTimeBox控件放在一起,归为一类,是 ...
- 3款强大的BootStrap的可视化制作工具推荐
http://www.25xt.com/html5css3/7342.html 25学堂看到最近很多朋友在学习Bootstrap前端主题框架.顾让25学堂的小编给大家找来了3款适合Bootstrap初 ...
- GitHub使用详解
1.GitHub是什么? GitHub这个名词既可以是那个流行的代码分享和协作网站 https://github.com/,也可以是指Git客户端工具(与其他的Git客户端工具如GitEye类似,只不 ...