elasticsearch聯(lián)合查詢

聯(lián)合查詢

  • 情景:祖孫三代 question->answer->vote
    question是answer父節(jié)點
    answer是vote的父節(jié)點
    
  • 建索引結(jié)構(gòu)
    PUT join_multi_index
    {
      "mappings": {
        "_doc":{
          "properties":{
            "my_join_field":{
              "type":"join",
              "relations":{
                "question":"answer",
                "answer":"vote"
              }
            }
          }
        }
      }
    }
    
  • 查詢索引情況
    GET join_multi_index
    
  • 插入數(shù)據(jù)
    # 插入question數(shù)據(jù)
    PUT join_multi_index/_doc/1?refresh
    {
      "text": "This is a question",
      "my_join_field": "question" 
    }
    
    PUT join_multi_index/_doc/2?refresh
    {
      "text": "This is another question",
      "my_join_field": "question"
    }
    
    # 添加answer記錄
    PUT join_multi_index/_doc/3?routing=1&refresh
    {
      "text": "This is a answer",
      "my_join_field": {
        "name": "answer",
        "parent": "1"
      }
    }
    
    PUT join_multi_index/_doc/4?routing=2&refresh
    {
      "text": "This is another answer",
      "my_join_field": {
        "name": "answer",
        "parent": "2"
      }
    }
    
    # 孫子節(jié)點vote記錄
    PUT join_multi_index/_doc/5?routing=2&refresh
    {
      "text": "This is a vote",
      "my_join_field": {
        "name": "vote",
        "parent": "4"
      }
    }
    
    # 由簡入繁的查詢
    GET join_multi_index/_search
    {
      "query": {
        "has_child": {
          "type": "answer",
          "query": {
            "match": {
              "text": "This is answer"
            }
          }
        }
      }
    }
    
    
    GET join_multi_index/_search
    {
      "query": {
        "has_child": {
          "type": "answer",
          "query": {
            "has_child": {
              "type": "vote",
              "query": {
                "match": {
                  "text": "This is vote"
                }
              }
            }
          }
        }
      }
    }
    
    
    GET join_multi_index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "text": "This is question"
              }
            },
            {
              "has_child": {
                "type": "answer",
                "query": {
                  "match": {
                    "text": "This is answer"
                  }
                }
              }
            },
            {
              "has_child": {
                "type": "answer",
                "query": {
                  "has_child": {
                    "type": "vote",
                    "query": {
                      "match": {
                        "text": "This is vote"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    

多條件聯(lián)合查詢

GET join_multi_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "text": "This is answer"
              }
            }
          }
        },
        {
          "has_child": {
            "type": "answer",
            "query": {
              "has_child": {
                "type": "vote",
                "query": {
                  "match": {
                    "text": "This is vote"
                  }
                }
              }
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "text": "This is question"
          }
        }
      ]
    }
  }
}

inner_hits的使用

GET join_multi_index/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "has_child": {
          "type": "vote",
          "query": {
            "match": {
              "text": "This is vote"
            }
          },
          "inner_hits": {}
        }
      },
      "inner_hits": {}
    }
  }
}

返回值

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "join_multi_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "text" : "This is another question",
          "my_join_field" : "question"
        },
        "inner_hits" : {
          "answer" : {
            "hits" : {
              "total" : 1,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "join_multi_index",
                  "_type" : "_doc",
                  "_id" : "4",
                  "_score" : 1.0,
                  "_routing" : "2",
                  "_source" : {
                    "text" : "This is another answer",
                    "my_join_field" : {
                      "name" : "answer",
                      "parent" : "2"
                    }
                  },
                  "inner_hits" : {
                    "vote" : {
                      "hits" : {
                        "total" : 1,
                        "max_score" : 1.2478919,
                        "hits" : [
                          {
                            "_index" : "join_multi_index",
                            "_type" : "_doc",
                            "_id" : "5",
                            "_score" : 1.2478919,
                            "_routing" : "2",
                            "_source" : {
                              "text" : "This is a vote",
                              "my_join_field" : {
                                "name" : "vote",
                                "parent" : "4"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

最終版整理

# 三層數(shù)據(jù)question->answer->vote

GET three_tree_index/_search

PUT three_tree_index
{
  "mappings": {
    "_doc":{
      "properties":{
        "my_join_field":{
          "type":"join",
          "relations":{
            "question":"answer",
            "answer":"vote"
          }
        }
      }
    }
  }
}

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

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

PUT three_tree_index/_doc/3?routing=1&refresh
{
  "text":"This is a answer",
  "my_join_field":{
    "name":"answer",
    "parent":"1"
  }
}

PUT three_tree_index/_doc/4?routing=2&refresh
{
  "text":"This is another answer",
  "my_join_field":{
    "name":"answer",
    "parent":"2"
  }
}

PUT three_tree_index/_doc/5?routing=1&refresh
{
  "text":"This is a vote",
  "my_join_field":{
    "name":"vote",
    "parent":"3"
  }
}

PUT three_tree_index/_doc/6?routing=2&refresh
{
  "text":"This is another vote",
  "my_join_field":{
    "name":"vote",
    "parent":"4"
  }
}

#-----------------------------------開始查詢

GET three_tree_index/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "match": {
          "text": "This is answer"
        }
      }
    }
  }
}


GET three_tree_index/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "text":  "This is answer" }},
        { "match": { "texts": "This is answer"   }}
      ]
    }
  }
}

# question->answer->vote

GET three_tree_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "text": "this is question"
          }
        },
        {
          "match": {
            "text": "this is question"
          }
        },
        {
          "has_child": {
            "type": "answer",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "text": "this is answer"
                    }
                  },
                  {
                    "match": {
                      "text": "this is answer"
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must": [
        {
          "has_child": {
            "type": "answer",
            "query": {
              "has_child": {
                "type": "vote",
                "query": {
                  "bool": {
                    "should": [
                      {
                        "term": {
                          "text": "vote"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市妇多,隨后出現(xiàn)的幾起案子昙篙,更是在濱河造成了極大的恐慌悦即,老刑警劉巖屡穗,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绘闷,死亡現(xiàn)場離奇詭異藐守,居然都是意外死亡浪藻,警方通過查閱死者的電腦和手機(jī)浙芙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門登刺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嗡呼,你說我怎么就攤上這事纸俭。” “怎么了南窗?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵揍很,是天一觀的道長。 經(jīng)常有香客問我万伤,道長窒悔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任敌买,我火速辦了婚禮简珠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘虹钮。我一直安慰自己聋庵,他們只是感情好荐操,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著珍策,像睡著了一般托启。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上攘宙,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天屯耸,我揣著相機(jī)與錄音,去河邊找鬼蹭劈。 笑死疗绣,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的铺韧。 我是一名探鬼主播多矮,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼哈打!你這毒婦竟也來了塔逃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤料仗,失蹤者是張志新(化名)和其女友劉穎湾盗,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體立轧,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡格粪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了氛改。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帐萎。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖胜卤,靈堂內(nèi)的尸體忽然破棺而出疆导,到底是詐尸還是另有隱情,我是刑警寧澤瑰艘,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布是鬼,位于F島的核電站,受9級特大地震影響紫新,放射性物質(zhì)發(fā)生泄漏均蜜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一芒率、第九天 我趴在偏房一處隱蔽的房頂上張望囤耳。 院中可真熱鬧,春花似錦、人聲如沸充择。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽椎麦。三九已至宰僧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間观挎,已是汗流浹背琴儿。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留嘁捷,地道東北人造成。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像雄嚣,于是被迫代替她去往敵國和親晒屎。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,511評論 0 23
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,457評論 0 13
  • 我是日記星球345號星寶寶缓升,正在參加第十三期21天蛻變之旅鼓鲁,這是我的第十五篇原創(chuàng)日記。 今年夏天兒子收到錄...
    重塑牛仔閱讀 357評論 0 0
  • 地鐵仔沿、公交車坐桩、手機(jī)上那么多APP,都在刷《延禧宮略》封锉、《如懿傳》、《天盛長歌》膘螟。不知道從什么時候開始成福,很多爆款電視...
    白玉京的長生劍閱讀 472評論 0 1