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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Elasticsearch6.X 新类型Join深入详解

發布時間:2024/1/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的父類。
PUT my_join_index {"mappings": {"_doc": {"properties": {"my_join_field": { "type": "join","relations": {"question": "answer" }}}}} }

3.2 ES6.X join類型定義父文檔

直接上以下簡化的形式,更好理解些。

如下,定義了兩篇父文檔。?
文檔類型為父類型:”question”。

PUT my_join_index/_doc/1?refresh {"text": "This is a question","my_join_field": "question" }PUT my_join_index/_doc/2?refresh {"text": "This is another question","my_join_field": "question" }

3.3 ES6.X join類型定義子文檔

  • 路由值是強制性的,因為父文件和子文件必須在相同的分片上建立索引。
  • “answer”是此子文檔的加入名稱。
  • 指定此子文檔的父文檔ID:1。
PUT my_join_index/_doc/3?routing=1&refresh {"text": "This is an answer","my_join_field": {"name": "answer", "parent": "1" } }PUT my_join_index/_doc/4?routing=1&refresh {"text": "This is another answer","my_join_field": {"name": "answer","parent": "1"} }

4、ES6.X Join類型約束

  • 每個索引只允許一個Join類型Mapping定義;
  • 父文檔和子文檔必須在同一個分片上編入索引;這意味著,當進行刪除、更新、查找子文檔時候需要提供相同的路由值。
  • 一個文檔可以有多個子文檔,但只能有一個父文檔。
  • 可以為已經存在的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處理。
    GET my_join_index/_search {"query": {"parent_id": { "type": "answer","id": "1"}},"aggs": {"parents": {"terms": {"field": "my_join_field#question", "size": 10}}},"script_fields": {"parent": {"script": {"source": "doc['my_join_field#question']" }}} }

    返回結果:

    {"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深入详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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