在通常的關係型數據庫中,都支持鏈接操做。html
在ES這種分佈式方案中進行鏈接操做,代價是十分昂貴的。數據庫
不過ES也提供了相相似的操做,支持水平任意擴展,實現鏈接的效果。數組
其餘內容,參考Elasticsearch官方指南整理app
在ES中支持兩種鏈接方式:嵌套查詢 和 has_child、has_parent父子查詢elasticsearch
嵌套查詢:分佈式
文檔中包含嵌套的字段,這些字段以數組的形式保存對象,這樣每一個嵌套的子對象均可以被搜索。ide
has_child、has_parent父子查詢:ui
父子文檔是存儲在同一個索引中的不一樣類型,在索引數據前定義父子關係。在父子查詢中,父子關係經過類型引用。spa
嵌套類型須要實現定義好mapping:code
{ "type1" : { "properties" : { "obj1" : { "type" : "nested" } } } }
定義好後,type1中就有了obj1這個子對象,而後就能夠經過嵌套查詢查詢相關的內容:
{ "nested" : { "path" : "obj1", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "match" : {"obj1.name" : "blue"} }, { "range" : {"obj1.count" : {"gt" : 5}} } ] } } } }
注意其中幾個參數:
1 path 定義了嵌套的對象
2 score_mode 定義裏嵌套對象計算的分數與當前查詢分數的處理方式,有avg,sum,max,min以及none。none就是不作任何處理,其餘的看字面意思就好理解。
3 query/filter是查詢的方式,內部定義了針對嵌套對象的查詢,注意內部的查詢必定要是用全路徑,即針對obj1的name字段的查詢,要寫obj1.name。
嵌套查詢會在執行子查詢的時候自動觸發,而後把結果返回給當前文檔的查詢。
父子關係也須要在以前定義mapping,不過與通常的映射不一樣,它的定義方式以下:
PUT my_index { "mappings": { "my_parent": {}, "my_child": { "_parent": { "type": "my_parent" } } } } PUT my_index/my_parent/1 { "text": "This is a parent document" } PUT my_index/my_child/2?parent=1 { "text": "This is a child document" } PUT my_index/my_child/3?parent=1 { "text": "This is another child document" } GET my_index/my_parent/_search { "query": { "has_child": { "type": "my_child", "query": { "match": { "text": "child document" } } } } }
這樣就表明,my_child這個類型的父類型是my_parent,這樣就聲明瞭一種父子關係。而後再索引數據時,指定父子對應的關係。
has_child查詢
這個查詢會檢查子文檔,若是子文檔知足查詢條件,則返回父文當。
{ "has_child" : { "type" : "blog_tag", "query" : { "term" : { "tag" : "something" } } } }
經過score_mode字段,能夠指定子文檔返回的分值的處理方式。與嵌套相似,它也有avg,sum,max,min和none幾種方式。
{ "has_child" : { "type" : "blog_tag", "score_mode" : "sum", "query" : { "term" : { "tag" : "something" } } } }
另外,也能夠指定子文檔匹配的最小數目和最大數目。
{ "has_child" : { "type" : "blog_tag", "score_mode" : "sum", "min_children": 2, "max_children": 10, "query" : { "term" : { "tag" : "something" } } } }
has_parent查詢
has_parent查詢與has_child相似,它是去檢查父文檔那個是否匹配,而後返回父文檔對應的子文檔。
{ "has_parent" : { "parent_type" : "blog", "query" : { "term" : { "tag" : "something" } } } }
1 如何定義父子關係:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html
2 鏈接查詢:https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
3 Nested查詢:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
4 Has_Child查詢:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html
5 Has_Parent查詢:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html