日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

doc es 中type_Elasticsearch(024):es常见的字段映射类型之 连接类型(join type)

發布時間:2025/4/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。