ES6.X,父子孙三级查询

以下代码根据本地localhost举例,需要将localhost替换成自己的ip

一、创建映射关系

http://localhost:9200/test_join
{
  "mappings": {
    "_doc": {
      "properties": {
        "info": { 
          "type": "join",
          "relations": {
            "classes": "teacher",
            "teacher":"user"
          }
        }
      }
    }
  }
}

以上映射将classes设置为teacher的父文档,将teacher设置为user的父文档,同理user就是classes的孙文档

二、插入数据

1、插入classes数据

http://localhost:9200/test_join/_doc/c1?routing=1&refresh
{
          "classesName": "电信一班",
          "info": {
            "name": "classes"
          }
        }

2、插入teacher数据

http://localhost:9200/test_join/_doc/t1?routing=1&refresh
{
                    "teacherName": "刘备",
                    "info": {
                      "name": "teacher",
                      "parent": "c1"
                    }
                  }

3、插入user数据

http://localhost:9200/test_join/_doc/u1?routing=1&refresh
{
          "userName": "刘灿",
          "info": {
            "name": "user",
            "parent":"t1"
          }
        }

三、构建查询

1、根据classes的子文档teacher和teacher的子文档user查询classes文档的数据,并带上teacher和user的数据

http://localhost:9200/test_join/_search
{
    "query": {
        "has_child":{
        	"type":"teacher",
        	"query":{
        		"bool":{
        			"must":[{
		        		"has_child":{
		        			"type":"user",
		        			"query":{
		        				"match":{
		        					"userName":"刘灿"
		        				}
		        			},"inner_hits":{}
		        		}
        			},{
        				"match":{
        					"teacherName": "刘备"
        				}
        			}
        		]
        		}
        	},"inner_hits":{}
        }
    }
}

返回结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_join",
        "_type": "_doc",
        "_id": "c1",
        "_score": 1,
        "_routing": "1",
        "_source": {
          "classesName": "电信一班",
          "info": {
            "name": "classes"
          }
        },
        "inner_hits": {
          "teacher": {
            "hits": {
              "total": 1,
              "max_score": 1.287682,
              "hits": [
                {
                  "_index": "test_join",
                  "_type": "_doc",
                  "_id": "t1",
                  "_score": 1.287682,
                  "_routing": "1",
                  "_source": {
                    "teacherName": "刘备",
                    "info": {
                      "name": "teacher",
                      "parent": "c1"
                    }
                  },
                  "inner_hits": {
                    "user": {
                      "hits": {
                        "total": 1,
                        "max_score": 0.2876821,
                        "hits": [
                          {
                            "_index": "test_join",
                            "_type": "_doc",
                            "_id": "u1",
                            "_score": 0.2876821,
                            "_routing": "1",
                            "_source": {
                              "userName": "刘灿",
                              "info": {
                                "name": "user",
                                "parent": "t1"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

2、根据孙文档user的父文档teacher和teacher的父文档classes查询孙文档user

http://localhost:9200/test_join/_search
{
    "query": {
    	"has_parent":{
    		"parent_type":"teacher",
    		"query":{
        		"bool":{
        			"must":[{
		        		"has_parent":{
		        			"parent_type":"classes",
		        			"query":{
		        				"match":{
		        					"classesName.keyword":"电信一班"
		        				}
		        			},"inner_hits":{}
		        		}
        			},{
        				"match":{
        					"teacherName.keyword": "刘备"
        				}
        			}
        		]
        		}
        	},"inner_hits":{}
    	}
    
    }
}

返回结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_join",
        "_type": "_doc",
        "_id": "u1",
        "_score": 1,
        "_routing": "1",
        "_source": {
          "userName": "刘灿",
          "info": {
            "name": "user",
            "parent": "t1"
          }
        },
        "inner_hits": {
          "teacher": {
            "hits": {
              "total": 1,
              "max_score": 1.287682,
              "hits": [
                {
                  "_index": "test_join",
                  "_type": "_doc",
                  "_id": "t1",
                  "_score": 1.287682,
                  "_routing": "1",
                  "_source": {
                    "teacherName": "刘备",
                    "info": {
                      "name": "teacher",
                      "parent": "c1"
                    }
                  },
                  "inner_hits": {
                    "classes": {
                      "hits": {
                        "total": 1,
                        "max_score": 0.2876821,
                        "hits": [
                          {
                            "_index": "test_join",
                            "_type": "_doc",
                            "_id": "c1",
                            "_score": 0.2876821,
                            "_routing": "1",
                            "_source": {
                              "classesName": "电信一班",
                              "info": {
                                "name": "classes"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

3、根据teacher的父文档classes和teacher的子文档user查询teacher文档,并带出对应数据

http://localhost:9200/test_join/_search
{
    "query": {
    	"has_parent":{
    		"parent_type":"classes",
    		"query":{
        		"bool":{
        			"must":[{
		        		"has_child":{
		        			"type":"teacher",
		        			"query":{
		        				"bool":{
		        					"must":[
		        						{
		        							"has_child":{
		        								"type":"user",
		        								"query":{
		        										"match":{
								        					"userName.keyword":"刘灿"
								        				}
		        								},"inner_hits":{}
		        							}	
		        						}	
		        					]
		        				}
		        			
		        			},"inner_hits":{}
		        		}
		        		},
		        		{
		        			"match_all":{
		        		}
        			}
        		]
        		}
        	},"inner_hits":{}
    	}
    
    }
}

返回结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_join",
        "_type": "_doc",
        "_id": "t1",
        "_score": 1,
        "_routing": "1",
        "_source": {
          "teacherName": "刘备",
          "info": {
            "name": "teacher",
            "parent": "c1"
          }
        },
        "inner_hits": {
          "classes": {
            "hits": {
              "total": 1,
              "max_score": 2,
              "hits": [
                {
                  "_index": "test_join",
                  "_type": "_doc",
                  "_id": "c1",
                  "_score": 2,
                  "_routing": "1",
                  "_source": {
                    "classesName": "电信一班",
                    "info": {
                      "name": "classes"
                    }
                  },
                  "inner_hits": {
                    "teacher": {
                      "hits": {
                        "total": 1,
                        "max_score": 1,
                        "hits": [
                          {
                            "_index": "test_join",
                            "_type": "_doc",
                            "_id": "t1",
                            "_score": 1,
                            "_routing": "1",
                            "_source": {
                              "teacherName": "刘备",
                              "info": {
                                "name": "teacher",
                                "parent": "c1"
                              }
                            },
                            "inner_hits": {
                              "user": {
                                "hits": {
                                  "total": 1,
                                  "max_score": 0.2876821,
                                  "hits": [
                                    {
                                      "_index": "test_join",
                                      "_type": "_doc",
                                      "_id": "u1",
                                      "_score": 0.2876821,
                                      "_routing": "1",
                                      "_source": {
                                        "userName": "刘灿",
                                        "info": {
                                          "name": "user",
                                          "parent": "t1"
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ES)