doc es 中type_Elasticsearch(024):es常见的字段映射类型之 连接类型(join type)
join type概述
出現(xiàn)的背景
引出問(wèn)題: “某頭條新聞APP”新聞內(nèi)容和新聞評(píng)論是1對(duì)多的關(guān)系?在ES6.X該如何存儲(chǔ)、如何進(jìn)行高效檢索、聚合操作呢?
1. ES6.X 新類型join產(chǎn)生背景
Mysql中多表關(guān)聯(lián),我們可以通過(guò)left join 或者Join等實(shí)現(xiàn)
ES5.X版本,借助父子文檔實(shí)現(xiàn)多表關(guān)聯(lián),類似數(shù)據(jù)庫(kù)中Join的功能;實(shí)現(xiàn)的核心是借助于ES5.X支持1個(gè)索引(index)下多個(gè)類型(type)
ES6.X版本,由于每個(gè)索引下面只支持單一的類型(type)
所以,ES6.X版本如何實(shí)現(xiàn)Join成為關(guān)注點(diǎn)
ES6.X新推出了Join類型,主要解決類似Mysql中多表關(guān)聯(lián)的問(wèn)題。
2. join類型介紹
仍然是一個(gè)索引下,借助父子關(guān)系,實(shí)現(xiàn)類似Mysql中多表關(guān)聯(lián)的操作
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. 父文檔數(shù)據(jù)插入
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> 路由值是強(qiáng)制性的,因?yàn)楦肝募妥游募仨氃谙嗤姆制辖⑺饕?/p>
<2> “answer”是此子文檔的加入名稱。代表其是一個(gè)子文檔。
<3> 指定此子文檔的父文檔ID:1。
6. 使用join類型的其他約束
每個(gè)索引只允許一個(gè)Join類型Mapping定義
父文檔和子文檔必須在同一個(gè)分片上編入索引;這意味著,當(dāng)進(jìn)行刪除、更新、查找子文檔時(shí)候需要提供相同的路由值
一個(gè)文檔可以有多個(gè)子文檔,但只能有一個(gè)父文檔
可以為已經(jīng)存在的Join類型添加新的關(guān)系
當(dāng)一個(gè)文檔已經(jīng)成為父文檔后,可以為該文檔添加子文檔
7.join類型的搜索與聚合
7.1 搜索全部
GET my_index/docs/_search
結(jié)果數(shù)據(jù)為
{
"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"
}
}
}
}
}
返回結(jié)果集
{
"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"
}
}
}
}
}
返回結(jié)果集
{
"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"
}
}
}
結(jié)果集
{
"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 聚合操作
在這里不做過(guò)多介紹,詳細(xì)的使用方法請(qǐng)?jiān)诤竺娴木酆系恼鹿?jié)進(jìn)行分析。
總結(jié)
以上是生活随笔為你收集整理的doc es 中type_Elasticsearch(024):es常见的字段映射类型之 连接类型(join type)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 原理图连线有错误提醒_拔罐方法不对=缩短
- 下一篇: gis中dbf转为csv_Python中