doc es 中type_Elasticsearch(024):es常见的字段映射类型之 连接类型(join type)
join type概述
出现的背景
引出问题: “某头条新闻APP”新闻内容和新闻评论是1对多的关系?在ES6.X该如何存储、如何进行高效检索、聚合操作呢?
1. ES6.X 新类型join产生背景
Mysql中多表关联,我们可以通过left join 或者Join等实现
ES5.X版本,借助父子文档实现多表关联,类似数据库中Join的功能;实现的核心是借助于ES5.X支持1个索引(index)下多个类型(type)
ES6.X版本,由于每个索引下面只支持单一的类型(type)
所以,ES6.X版本如何实现Join成为关注点
ES6.X新推出了Join类型,主要解决类似Mysql中多表关联的问题。
2. join类型介绍
仍然是一个索引下,借助父子关系,实现类似Mysql中多表关联的操作
3. join类型的mapping定义
PUT my_index
{
"mappings": {
"docs": {
"properties": {
"id": {
"type": "long"
},
"my_join_field": { <1>
"type": "join",
"eager_global_ordinals": true,
"relations": {
"question": "answer" <2>
}
},
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
<1> 为join的名称
<2> 指question为answer的父类
4. 父文档数据插入
PUT my_index/docs/1?refresh
{
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}
PUT my_index/docs/2?refresh
{
"text": "This is a another question",
"my_join_field": {
"name": "question"
}
}
PUT my_index/docs/_bulk?refresh
{"index": {"_id": 3}}
{"id":3, "text": "question 3333", "my_join_field": {"name": "question"}}
{"index": {"_id": 4}}
{"id":4, "text": "question 4444", "my_join_field": {"name": "question"}}
文档类型为父类型: ”question”。
5. 子类型文档插入
PUT my_index/doc/5?routing=1&refresh <1>
{
"text": "This is an answer",
"my_join_field": {
"name": "answer", <2>
"parent": "1" <3>
}
}
PUT my_index/doc/6?routing=1&refresh
{
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
<1> 路由值是强制性的,因为父文件和子文件必须在相同的分片上建立索引。
<2> “answer”是此子文档的加入名称。代表其是一个子文档。
<3> 指定此子文档的父文档ID:1。
6. 使用join类型的其他约束
每个索引只允许一个Join类型Mapping定义
父文档和子文档必须在同一个分片上编入索引;这意味着,当进行删除、更新、查找子文档时候需要提供相同的路由值
一个文档可以有多个子文档,但只能有一个父文档
可以为已经存在的Join类型添加新的关系
当一个文档已经成为父文档后,可以为该文档添加子文档
7.join类型的搜索与聚合
7.1 搜索全部
GET my_index/docs/_search
结果数据为
{
"took": 145,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_type": "docs",
"_id": "4",
"_score": 1,
"_source": {
"id": 4,
"text": "question 4444",
"my_join_field": {
"name": "question"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "2",
"_score": 1,
"_source": {
"text": "This is a another question",
"my_join_field": {
"name": "question"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "1",
"_score": 1,
"_source": {
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "5",
"_score": 1,
"_routing": "1",
"_source": {
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "6",
"_score": 1,
"_routing": "1",
"_source": {
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "3",
"_score": 1,
"_source": {
"id": 3,
"text": "question 3333",
"my_join_field": {
"name": "question"
}
}
}
]
}
}
7.2 基于父文档查找子文档
GET my_index/docs/_search
{
"query": {
"has_parent": {
"parent_type": "question",
"query": {
"match": {
"text": "this is"
}
}
}
}
}
返回结果集
{
"took": 161,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_type": "docs",
"_id": "5",
"_score": 1,
"_routing": "1",
"_source": {
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "6",
"_score": 1,
"_routing": "1",
"_source": {
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
}
]
}
}
7.3 基于子文档查找父文档
GET my_index/docs/_search
{
"query": {
"has_child": {
"type": "answer",
"query": {
"match": {
"text": "this is"
}
}
}
}
}
返回结果集
{
"took": 286,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_type": "docs",
"_id": "1",
"_score": 1,
"_source": {
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}
}
]
}
}
7.4 查找指定父文档id的子文档集合
GET /my_index/docs/_search
{
"query": {
"parent_id": {
"type": "answer",
"id": "1"
}
}
}
结果集
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.13353139,
"hits": [
{
"_index": "my_index",
"_type": "docs",
"_id": "5",
"_score": 0.13353139,
"_routing": "1",
"_source": {
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
},
{
"_index": "my_index",
"_type": "docs",
"_id": "6",
"_score": 0.13353139,
"_routing": "1",
"_source": {
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
}
]
}
}
7.5 聚合操作
在这里不做过多介绍,详细的使用方法请在后面的聚合的章节进行分析。
doc es 中type_Elasticsearch(024):es常见的字段映射类型之 连接类型(join type)相关推荐
- doc es 中type_Elasticsearch基础——概念和基本API操作
Elasticsearch 版本为 7.1.0 ,本文的讲解都是基于该版本 文章中Elasticsearch将使用简称ES代替 一.基本概念 文档--Document ES是面向文档的搜索,文档是ES ...
- Elasticsearch(022):es常见的字段映射类型之地理形状类型(geo_shape、多边的复杂的地址形状)
概念 在上个小节,我们学习了geo_point的数据类型. 这一小节我们来学习geo_shape数据类型,它有助于索引和搜索 任意地理形状,例如矩形和多边形.当正在索引的数据或正在执行的查询包含除点以 ...
- doc es 中type_ElasticSearch: Index 和 Type 的区别
原文: Index vs. Type By Adrien Grand 译者: fengchang 对于 ES 的新用户来说,有一个常见的问题:要存储一批新的数据时,应该在已有 index 里新建一个 ...
- 可以储存照片的字段类型是_在数据库中可用来存储图片的字段对象是哪种类型的字段?...
展开全部 一. 把图片直接以二进制形式存储在数据库中 一般数据库提供一个e68a84e8a2ad3231313335323631343130323136353331333365633838二进制字段来 ...
- 把json数据导入linux,使用json文件给es中导入数据
使用json文件可以给es中导入数据,10万条左右的数据可以一次导入,数量太大时导入就会报错.大数量的到导入还是需要用bulk方式. accounts.json文件格式如下: {"index ...
- ES中mapping是什么,es中的数据类型
mapping解释 ES中的mapping有点类似与RDB中"表结构"的概念,在MySQL中,表结构里包含了字段名称,字段的类型还有索引信息等.在Mapping里也包含了一些属性, ...
- Elasticsearch和Hive整合,将hive数据同步到ES中
1 Elasticsearch整合Hive 1.1 软件环境 Hadoop软件环境 Hive软件环境 ES软件环境 1.2 ES-Hadoop介绍 1.2.1 官网 https://www.elast ...
- 千万级数据查询中CK、ES、RediSearch方案的优化
前言 在开发中遇到一个业务诉求,需要在千万量级的底池数据中筛选出不超过 10W 的数据,并根据配置的权重规则进行排序.打散(如同一个类目下的商品数据不能连续出现 3 次). 下面对该业务诉求的实现,设 ...
- 基于springboot项目中使用docker-compose+es+kibana+logstash+mysql 提高数据查询效率
基于springboot项目中使用docker-compose+es+kibana+logstash+mysql 提高数据查询效率 1.拉取logstash,kibana,es,mysql镜像 #命令 ...
最新文章
- alright alright alright
- pythonsklearn多元回归回归_用sklearn进行多元线性回归
- 24 MM配置-采购-配额管理-定义编码范围
- The file “XXX.app” couldn’t be opened because you don’t have permission to view it.问题修复...
- SQLSERVER查看阻塞和CPU占用的会话,存储过程执行性能排行
- 单机按钮来图片轮播_原生js如何实现轮播图效果?
- 操作系统和语言的关系(转载)
- solr 配置中文分析器/定义业务域/配置DataImport功能(测试用)
- 基于Docker swarm 集群搭建SSR 学习
- SAP 用户出口合集
- 【Freeswitch从入门到精通】一、常用总结
- 【UEFI基础】PCD
- lay-href页面不跳转
- 软件如何进行压力测试,软件如何进行压力测试?
- AFX_MANAGE_STATE(AfxGetStaticModuleState())
- 豪斯曼检验matlab,豪斯曼检验、空间面板模型选择等问题
- 【LearningChain】WhenMachineLearningMeetsBlockchainADecentralizedPrivacy-preserving and SecureDesign
- 你真的理解图像处理经典算法 SIFT 吗?最深入、最全面综述:尺度不变特征转换
- 《程序员面试金典(第6版)》 面试题 08.11. 硬币(动态规划,组合问题,C++)
- 实现一个完整的前后端交互
热门文章
- 开发IOT WiFi设备时,需要测试的几种情况
- Stanford Machine Learning
- PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名
- Java使用jmeter源码进行接口测试_jmeter用java代码怎样编写接口测试源码
- 如何把两个域控同步_同步带噪音大,怎么办?
- 12v60ah锂电池组装图_锂电池基本参数,结合电动自行车电池应用分析
- java 获取活动窗口_用Java获取活动窗口信息
- 国防科大天河计算机应用,国防科大计算机学院:让本科生进入“天河”团队
- Android CameraSurfaceView在SurfaceView上实现拍照,视频录像
- 同济大学计算机学院徐老师,第十八届同济大学程序设计竞赛暨高校网络友谊赛圆满落幕...