elasticsearch联合查询

联合查询

  • 情景:祖孙三代 question->answer->vote
    question是answer父节点
    answer是vote的父节点
    
  • 建索引结构
    PUT join_multi_index
    {
      "mappings": {
        "_doc":{
          "properties":{
            "my_join_field":{
              "type":"join",
              "relations":{
                "question":"answer",
                "answer":"vote"
              }
            }
          }
        }
      }
    }
    
  • 查询索引情况
    GET join_multi_index
    
  • 插入数据
    # 插入question数据
    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"
      }
    }
    
    # 孙子节点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"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    

多条件联合查询

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"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

最终版整理

# 三层数据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"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,884评论 0 23
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 我是日记星球345号星宝宝,正在参加第十三期21天蜕变之旅,这是我的第十五篇原创日记。 今年夏天儿子收到录...
    重塑牛仔阅读 382评论 0 0
  • 地铁、公交车、手机上那么多APP,都在刷《延禧宫略》、《如懿传》、《天盛长歌》。不知道从什么时候开始,很多爆款电视...
    白玉京的长生剑阅读 492评论 0 1