Elasticsearch6.X 新类型Join深入详解
生活随笔
收集整理的這篇文章主要介紹了
Elasticsearch6.X 新类型Join深入详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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、ES6.X Join類型介紹
仍然是一個索引下,借助父子關系,實現類似Mysql中多表關聯的操作。
3、ES6.X Join類型實戰
3.1 ES6.X Join類型 Mapping定義
Join類型的Mapping如下:
核心?
- 1) “my_join_field”為join的名稱。
- 2)”question”: “answer” 指:qustion為answer的父類。
3.2 ES6.X join類型定義父文檔
直接上以下簡化的形式,更好理解些。
如下,定義了兩篇父文檔。?
文檔類型為父類型:”question”。
3.3 ES6.X join類型定義子文檔
- 路由值是強制性的,因為父文件和子文件必須在相同的分片上建立索引。
- “answer”是此子文檔的加入名稱。
- 指定此子文檔的父文檔ID:1。
4、ES6.X Join類型約束
5、ES6.X Join類型檢索與聚合
5.1 ES6.X Join全量檢索
GET my_join_index/_search {"query": {"match_all": {}},"sort": ["_id"] }返回結果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 4,"max_score": null,"hits": [{"_index": "my_join_index","_type": "_doc","_id": "1","_score": null,"_source": {"text": "This is a question","my_join_field": "question"},"sort": ["1"]},{"_index": "my_join_index","_type": "_doc","_id": "2","_score": null,"_source": {"text": "This is another question","my_join_field": "question"},"sort": ["2"]},{"_index": "my_join_index","_type": "_doc","_id": "3","_score": null,"_routing": "1","_source": {"text": "This is an answer","my_join_field": {"name": "answer","parent": "1"}},"sort": ["3"]},{"_index": "my_join_index","_type": "_doc","_id": "4","_score": null,"_routing": "1","_source": {"text": "This is another answer","my_join_field": {"name": "answer","parent": "1"}},"sort": ["4"]}]} }5.2 ES6.X 基于父文檔查找子文檔
GET my_join_index/_search {"query": {"has_parent" : {"parent_type" : "question","query" : {"match" : {"text" : "This is"}}}} }返回結果:
{"took": 0,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "my_join_index","_type": "_doc","_id": "3","_score": 1,"_routing": "1","_source": {"text": "This is an answer","my_join_field": {"name": "answer","parent": "1"}}},{"_index": "my_join_index","_type": "_doc","_id": "4","_score": 1,"_routing": "1","_source": {"text": "This is another answer","my_join_field": {"name": "answer","parent": "1"}}}]} }5.3 ES6.X 基于子文檔查找父文檔
GET my_join_index/_search { "query": {"has_child" : {"type" : "answer","query" : {"match" : {"text" : "This is question"}}}} }返回結果:
{"took": 0,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "my_join_index","_type": "_doc","_id": "1","_score": 1,"_source": {"text": "This is a question","my_join_field": "question"}}]} }5.4 ES6.X Join聚合操作實戰
以下操作含義如下:
- 1)parent_id是特定的檢索方式,用于檢索屬于特定父文檔id=1的,子文檔類型為answer的文檔的個數。
- 2)基于父文檔類型question進行聚合;
- 3)基于指定的field處理。
返回結果:
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 0.13353139,"hits": [{"_index": "my_join_index","_type": "_doc","_id": "3","_score": 0.13353139,"_routing": "1","fields": {"parent": ["1"]}},{"_index": "my_join_index","_type": "_doc","_id": "4","_score": 0.13353139,"_routing": "1","fields": {"parent": ["1"]}}]},"aggregations": {"parents": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "1","doc_count": 2}]}} }6、ES6.X Join 一對多實戰
6.1 一對多定義
如下,一個父文檔question與多個子文檔answer,comment的映射定義。
PUT join_ext_index {"mappings": {"_doc": {"properties": {"my_join_field": {"type": "join","relations": {"question": ["answer", "comment"] }}}}} }6.2 一對多對多定義
實現如下圖的祖孫三代關聯關系的定義。
question/ \/ \ comment answer||vote PUT join_multi_index {"mappings": {"_doc": {"properties": {"my_join_field": {"type": "join","relations": {"question": ["answer", "comment"], "answer": "vote" }}}}} }孫子文檔導入數據,如下所示:
PUT join_multi_index/_doc/3?routing=1&refresh {"text": "This is a vote","my_join_field": {"name": "vote","parent": "2" } }注意:
- 孫子文檔所在分片必須與其父母和祖父母相同 - 孫子文檔的父代號(必須指向其父親answer文檔)7、小結
雖然ES官方文檔已經很詳細了,詳見:?
http://t.cn/RnBBLgp
總結
以上是生活随笔為你收集整理的Elasticsearch6.X 新类型Join深入详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Elasticsearch实现类Goog
- 下一篇: Elasticsearch检索分类详解