Elasticsearch进阶笔记第三十三篇

103 阅读3分钟

Elasticsearch高手进阶篇(70)

数据建模实战_根据员工信息和研发中心互相搜索父子数据

我们已经建立了父子关系的数据模型之后,就要基于这个模型进行各种搜索和聚合了

搜索有1980年以后出生的员工的研发中心

 GET /waws520_company/rd_center/_search
 {
   "query": {
     "has_child": {
       "type": "employee",
       "query": {
         "range": {
           "birthday": {
             "gte": "1980-01-01"
           }
         }
       }
     }
   }
 }
 
 {
   "took": 58,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws520_company",
         "_type": "rd_center",
         "_id": "1",
         "_score": 1,
         "_source": {
           "name": "北京研发总部",
           "city": "北京",
           "country": "中国"
         }
       },
       {
         "_index": "waws520_company",
         "_type": "rd_center",
         "_id": "3",
         "_score": 1,
         "_source": {
           "name": "硅谷人工智能实验室",
           "city": "硅谷",
           "country": "美国"
         }
       }
     ]
   }
 }

搜索有名叫张三的员工的研发中心

 GET /waws520_company/rd_center/_search
 {
   "query": {
     "has_child": {
       "type":"employee",
       "query": {
         "match": {
           "name": "张三"
         }
       }
     }
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws520_company",
         "_type": "rd_center",
         "_id": "1",
         "_score": 1,
         "_source": {
           "name": "北京研发总部",
           "city": "北京",
           "country": "中国"
         }
       }
     ]
   }
 }

搜索有至少2个以上员工的研发中心

 GET /waws520_company/rd_center/_search
 {
   "query": {
     "has_child": {
       "type":"employee",
       "min_children": 2, 
       "query": {
         "match_all": {}
       }
     }
   }
 }
 ​
 {
   "took": 4,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws520_company",
         "_type": "rd_center",
         "_id": "1",
         "_score": 1,
         "_source": {
           "name": "北京研发总部",
           "city": "北京",
           "country": "中国"
         }
       }
     ]
   }
 }

搜索在中国的研发中心的员工

 GET /waws520_company/employee/_search 
 {
   "query": {
     "has_parent": {
       "parent_type": "rd_center",
       "query": {
         "term": {
           "country.keyword": "中国"
         }
       }
     }
   }
 }
 
 {
   "took": 41,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws520_company",
         "_type": "employee",
         "_id": "3",
         "_score": 1,
         "_routing": "2",
         "_parent": "2",
         "_source": {
           "name": "王二",
           "birthday": "1979-04-01",
           "hobby": "爬山"
         }
       },
       {
         "_index": "waws520_company",
         "_type": "employee",
         "_id": "1",
         "_score": 1,
         "_routing": "1",
         "_parent": "1",
         "_source": {
           "name": "张三",
           "birthday": "1970-10-24",
           "hobby": "爬山"
         }
       },
       {
         "_index": "waws520_company",
         "_type": "employee",
         "_id": "2",
         "_score": 1,
         "_routing": "1",
         "_parent": "1",
         "_source": {
           "name": "李四",
           "birthday": "1982-05-16",
           "hobby": "游泳"
         }
       }
     ]
   }
 }

Elasticsearch高手进阶篇(71)

数据建模实战_对每个国家的员工兴趣爱好进行聚合统计

统计每个国家的喜欢每种爱好的员工有多少个

 GET /waws520_company/rd_center/_search 
 {
   "size": 0,
   "aggs": {
     "group_by_country": {
       "terms": {
         "field": "country.keyword"
       },
       "aggs": {
         "group_by_child_employee": {
           "children": {
             "type": "employee"
           },
           "aggs": {
             "group_by_hobby": {
               "terms": {
                 "field": "hobby.keyword"
               }
             }
           }
         }
       }
     }
   }
 }
 
 {
   "took": 25,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 0,
     "hits": []
   },
   "aggregations": {
     "group_by_country": {
       "doc_count_error_upper_bound": 0,
       "sum_other_doc_count": 0,
       "buckets": [
         {
           "key": "中国",
           "doc_count": 2,
           "group_by_child_employee": {
             "doc_count": 3,
             "group_by_hobby": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": [
                 {
                   "key": "爬山",
                   "doc_count": 2
                 },
                 {
                   "key": "游泳",
                   "doc_count": 1
                 }
               ]
             }
           }
         },
         {
           "key": "美国",
           "doc_count": 1,
           "group_by_child_employee": {
             "doc_count": 1,
             "group_by_hobby": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": [
                 {
                   "key": "骑马",
                   "doc_count": 1
                 }
               ]
             }
           }
         }
       ]
     }
   }
 }

Elasticsearch高手进阶篇(72)

数据建模实战_祖孙三层数据关系建模以及搜索实战

父子关系,祖孙三层关系的数据建模,搜索

 PUT /waws520_company
 {
   "mappings": {
     "country": {},
     "rd_center": {
       "_parent": {
         "type": "country" 
       }
     },
     "employee": {
       "_parent": {
         "type": "rd_center" 
       }
     }
   }
 }

country -> rd_center -> employee,祖孙三层数据模型

  • country层
 POST /waws520_company/country/_bulk
 { "index": { "_id": "1" }}
 { "name": "中国" }
 { "index": { "_id": "2" }}
 { "name": "美国" }
  • rd_center层
 POST /waws520_company/rd_center/_bulk
 { "index": { "_id": "1", "parent": "1" }}
 { "name": "北京研发总部" }
 { "index": { "_id": "2", "parent": "1" }}
 { "name": "上海研发中心" }
 { "index": { "_id": "3", "parent": "2" }}
 { "name": "硅谷人工智能实验室" }
  • employee层
 PUT /waws520_company/employee/1?parent=1&routing=1
 {
   "name":  "张三",
   "dob":   "1970-10-24",
   "hobby": "爬山"
 }

routing参数的讲解,必须跟grandparent相同,否则有问题

  • country,用的是自己的id去路由; rd_center,parent,用的是country的id去路由; employee,如果也是仅仅指定一个parent,那么用的是rd_center的id去路由,这就导致祖孙三层数据不会在一个shard上

  • 孙子辈儿,要手动指定routing,指定为爷爷辈儿的数据的id

搜索有爬山爱好的员工所在的国家

 GET /waws520_company/country/_search
 {
   "query": {
     "has_child": {
       "type": "rd_center",
       "query": {
         "has_child": {
           "type": "employee",
           "query": {
             "match": {
               "hobby": "爬山"
             }
           }
         }
       }
     }
   }
 }
 
 {
   "took": 3,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws520_company",
         "_type": "country",
         "_id": "1",
         "_score": 1,
         "_source": {
           "name": "中国"
         }
       }
     ]
   }
 }