ES 6.6 X、親子孫三級の照会

13241 ワード

以下のコードはローカルlocal hostの例によって、local hostを自分のipに置き換える必要があります。
一、マッピング関係を作成する
http://localhost:9200/test_join
{
  "mappings": {
    "_doc": {
      "properties": {
        "info": { 
          "type": "join",
          "relations": {
            "classes": "teacher",
            "teacher":"user"
          }
        }
      }
    }
  }
}
以上のマップでは、clasesをteacherの親文書に設定し、teacherをアメリカの親文書に設定しました。同じアメリカはclasesの孫文書です。
二、データの挿入
1、clasesデータを挿入する
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、clasesのサブドキュメントteacherとteacherのサブドキュメントuserによってclases文書のデータを調べ、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の父文書clasesによって孫文書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の親文書clasesと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"
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}