parent-child 关系介绍
来源:互联网 发布:ubuntu 安装 sublime 编辑:程序博客网 时间:2024/05/16 04:30
parent-child可以将一个文档与其他文档相关联,类似于SQL中的一对多关系,一个parent可以有多个children,parent-child与nested相比有以下优点:
1.父文档的更新不需要重新为子文档重建索引
2.可以对子文档进行添加、更改或者删除操作而不会影响父文档或者其他的子文档
3.子文档可以被当做查询请求的结果返回
(不知道翻译的对不对)
parent-child映射
为了建立parent-child关系,需要在索引创建的时候指定父文档
假设一个公司company在各地有分公司branch,每个分公司的员工为employee,我们想将各个分公司的employee与对应的分公司brance相关联
首先来创建映射
创建company索引其中包含branch类型和employee类型
PUT /company{ "mappings": { "branch": {}, "employee": { "_parent": { "type": "branch" } } }}
employee中的_parent中的内容表示指定employee的parent为branch类型,也就是parent父文档为brance,它的child为employee
POST /company/branch/_bulk{ "index": { "_id": "London" }}{ "name": "London Company ", "city": "London", "country": "UK" }{ "index": { "_id": "Beijing" }}{ "name": " Beijing Company", "city": "Beijing", "country": "CN" }{ "index": { "_id": "Dalian" }}{ "name": " Dalian Company ", "city": "Dalian", "country": "CN" }
接着向employe中添加一条数据,在插入数据的时候需要指定parent,也就是与brance类型中的id相关联,此处parent设为london,表示该员工与branch中id为London的文档相关连
PUT /company/employee/1?parent=London { "name": "Alice Smith", "birth": "1970-10-24", "hobby": "hiking"}
向employee批量插入一些数据
POST /company/employee/_bulk{ "index": { "_id": 2, "parent": "Beijing" }}{ "name": "Mark Thomas", "birth": "1989-05-16", "hobby": "diving" }{ "index": { "_id": 3, "parent": "Beijing" }}{ "name": "Barry Smith", "birth": "1987-03-01", "hobby": "hiking" }{ "index": { "_id": 4, "parent": "Dalian" }}{ "name": "Adrien Grand", "birth": "1990-05-11", "hobby": "horses" }
has_child查询
has_child查询可以基于子文档的内容从父文档中查询
假如想查询员工birth在1980-01-01之后的公司都有哪些
GET /company/branch/_search{ "query": { "has_child": { "type": "employee", "query": { "range": { "birth": { "gte": "1980-01-01" } } } } }}查询结果如下,可以看出员工birth在1980-01-01之后的公司有Beijing 和Dalian
注:score_mode默认为none,此时将会忽略子文档的相关性得分并将父文档的得分设为1.0,我的理解就是为none的时候不计算相关性
has_parent查询
has_parent查询可以基于父文档的数据查询子文档
假如想查询country为CN的公司所有的员工信息
GET /company/employee/_search{ "query": { "has_parent": { "type": "branch", "query": { "match": { "country": "CN" } } } }}
查询结果如下:
来自官方文档
https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html
- parent-child 关系介绍
- 示例:父子关系(Parent Child Relationships)
- 【CoreData】parent-child关系ManagedObjectContext应用
- 理解elasticsearch的parent-child关系
- parent > child
- 关于窗口的Parent&Child关系与Owner&Owned关系
- Elasticsearch索引的父子关系(index parent-child)
- 【jQuery】parent > child选择器
- $(“parent > child”)选择器
- 【JQuery】parent > child选择器
- jQuery parent>child
- #200 – 窗体间的父子从属关系(Parent/Child Relationships Between Windows)
- Elasticsearch——利用Parent-Child关系解决大数据场景下的实时查询
- Python child, parent, super CLASS
- parent > child选择器(jQuery)
- Angular-Parent/Child Controller Communication
- 2-7parent > child选择器
- parent-childRecursive sum in parent-child hierarchy T-SQL
- 装载问题
- maven的继承和聚合关系
- 算法 符号三角形问题
- android 生成jar包里面包含资源文件
- opencv学习笔记(四)高斯滤波
- parent-child 关系介绍
- 杭电2548两军交锋
- Scikit-learn实战之最近邻算法
- springMVC版本和jdk版本不匹配造成的问题
- hdfs java操作
- SE复习日记【三】Life is short, you need Python
- Dev-C++ “to_string is not a member of std” error- 已解决
- 使用 getopt() 、getopt_long()、getopt_long_only()进行命令行处理
- React-Native填坑之TextInput value属性