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

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

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

GET /company/rd_center/_search

{

  "query": {

    "has_child": {//使用的这个搜索

      "type": "employee",//type 对应的是query  (查询他的child 的类型是employee 的)

      "query": {

        "range": {

          "birthday": {

            "gte": "1980-01-01"

          }

        }

      }

    }

  }

}

结果:

{

  "took": 33,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 2,

    "max_score": 1,

    "hits": [

      {

        "_index": "company",

        "_type": "rd_center",

        "_id": "1",

        "_score": 1,

        "_source": {

          "name": "北京研发总部",

          "city": "北京",

          "country": "中国"

        }

      },

      {

        "_index": "company",

        "_type": "rd_center",

        "_id": "3",

        "_score": 1,

        "_source": {

          "name": "硅谷人工智能实验室",

          "city": "硅谷",

          "country": "美国"

        }

      }

    ]

  }

}

 

 

根据父的id去查询子的数据(子根据父的parent进行相对应的关联)

2、搜索有名叫李四的员工的研发中心

GET /company/rd_center/_search

{

  "query": {

    "has_child": {

      "type":       "employee",

      "query": {

        "match": {

          "name": "李四"

        }

      }

    }

  }

}

结果:

{

  "took": 1,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 1,

    "hits": [

      {

        "_index": "company",

        "_type": "rd_center",

        "_id": "1",

        "_score": 1,

        "_source": {

          "name": "北京研发总部",

          "city": "北京",

          "country": "中国"

        }

      }

    ]

  }

}

 

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

GET /company/rd_center/_search

{

  "query": {

    "has_child": {

      "type":         "employee",

      "min_children": 2,

      "query": {

        "match_all": {}

      }

    }

  }

}

结果:

{

  "took": 3,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 0,

    "max_score": null,

    "hits": []

  }

}

结果:

 

 

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

GET /company/employee/_search

{

  "query": {

    "has_parent": {

      "parent_type": "rd_center",

      "query": {

        "term": {

          "country.keyword": "中国"

        }

      }

    }

  }

}

结果:

{

  "took": 2,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 2,

    "max_score": 1,

    "hits": [

      {

        "_index": "company",

        "_type": "employee",

        "_id": "3",

        "_score": 1,

        "_routing": "2",

        "_parent": "2",

        "_source": {

          "name": "王二",

          "birthday": "1979-04-01",

          "hobby": "爬山"

        }

      },

      {

        "_index": "company",

        "_type": "employee",

        "_id": "2",

        "_score": 1,

        "_routing": "1",

        "_parent": "1",

        "_source": {

          "name": "李四",

          "birthday": "1982-05-16",

          "hobby": "游泳"

        }

      }

    ]

  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值