ES父子查询

ES父子查询

项目上有父子文档的查询的需求,实现父查子,子查父,查看了一些大佬们学的文档,最终也没实现我的需求,先记录一下吧。
有哪个大佬给指导一下,索引和数据如下文,需求如下:
1、在页面中,默认按照班级排序,正常的顺序是一班、二班、三班。
2、页面中有按每个省的学生数量、或每个市的学生数量、或出生时间段的学生数量对班级进行排序
这个需求,研究了一天也没实现,希望大佬给出意见。万分感谢。

删除索引

DELETE classes2student

插入索引

PUT classes2student
{
	"settings": {
		"number_of_shards": "5",
		"number_of_replicas": "1"
	},
	"mappings": {
		"properties": {
			"classid": {
				"type": "keyword"
			},
			"class-name": {
				"type": "keyword"
			},
			"grade": {
				"type": "keyword"
			},
			"describe": {
				"type": "text",
				"analyzer": "ik_max_word"
			},
			"class-adviser": {
				"type": "keyword"
			},
            "student-classid": {
				"type": "keyword"
			},
            "student-provice": {
				"type": "keyword"
			},
            "student-city": {
				"type": "keyword"
			},
			"student-name": {
				"type": "keyword"
			},
			"student-gender": {
				"type": "keyword"
			},
			"student-address": {
				"type": "text",
				"analyzer": "ik_max_word"
			},
			"student-age": {
				"type": "integer"
			},
			"student-total-score": {
				"type": "double"
			},
			"student-birthday": {
				"type": "keyword"
			},
			"student-hobby": {
				"type": "text",
				"analyzer": "ik_max_word"
			},
			"relations-join-field": {
				"type": "join",
				"relations": {
					"classes": "student"
				}
			}
		}
	}
}

主要的参数说明:
relations-join-field:自定义使用关联关系字段
type:使用join
relations:父子文档关系,classes为父文档,student为子文档
官方文档

准备班级数据

# 插入班级数据
PUT /classes2student/_doc/1
{
	"classid": 1,
	"class-name": "一班",
	"grade": "初一",
	"describe": "初一一班是一个荣誉感、凝聚力很强的班级",
	"class-adviser": "李艳艳",
	"relations-join-field": {
		"name": "classes"
	}
}

# 插入班级数据
PUT /classes2student/_doc/2
{
	"classid": 2,
	"class-name": "二班",
	"grade": "初一",
	"describe": "初一二班是一个凝聚力很强的班级",
	"class-adviser": "马芹芹",
	"relations-join-field": {
		"name": "classes"
	}
}

# 插入班级数据
PUT /classes2student/_doc/3
{
	"classid": 3,
	"class-name": "三班",
	"grade": "初一",
	"describe": "初一三班是一个无私奉献的班级",
	"class-adviser": "刘媛媛",
	"relations-join-field": {
		"name": "classes"
	}
}

准备学生数据

#######################################################################################
#初一一班学生数据
PUT /classes2student/_doc/4?routing=1
{
	"student-classid": "1",
	"student-provice": "山东",
	"student-city": "菏泽",
	"student-name": "张海剑",
	"student-gender": "男",
	"student-address": "历下区111号",
	"student-age": 11,
	"student-total-score": 540.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事",
	"relations-join-field": {
		"name": "student",
		"parent": "1"
	}
}

PUT /classes2student/_doc/5?routing=1
{
	"student-classid": "1",
	"student-provice": "山东",
	"student-city": "威海",
	"student-name": "张小明",
	"student-gender": "男",
	"student-address": "历城区10001号",
	"student-age": 12,
	"student-total-score": 583.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、游泳、跑步",
	"relations-join-field": {
		"name": "student",
		"parent": "1"
	}
}

PUT /classes2student/_doc/6?routing=1
{
	"student-classid": "1",
	"student-provice": "山东",
	"student-city": "济宁",
	"student-name": "冯程程",
	"student-gender": "女",
	"student-address": "高新区10689号",
	"student-age": 10,
	"student-total-score": 603.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、跳舞、下棋",
	"relations-join-field": {
		"name": "student",
		"parent": "1"
	}
}

#######################################################################################
#初一二班学生数据
PUT /classes2student/_doc/7?routing=2
{
	"student-classid": "2",
	"student-provice": "山东",
	"student-city": "济南",
	"student-name": "程诚",
	"student-gender": "男",
	"student-address": "历下区2869号",
	"student-age": 11,
	"student-total-score": 559.5,
	"student-birthday": "2004-06-09",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事",
	"relations-join-field": {
		"name": "student",
		"parent": "2"
	}
}

PUT /classes2student/_doc/8?routing=2
{
	"student-classid": "2",
	"student-provice": "山东",
	"student-city": "聊城",
	"student-name": "刘英杰",
	"student-gender": "男",
	"student-address": "平阴县10001号",
	"student-age": 14,
	"student-total-score": 483.5,
	"student-birthday": "2003-02-13",
	"student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球",
	"relations-join-field": {
		"name": "student",
		"parent": "2"
	}
}

PUT /classes2student/_doc/9?routing=2
{
	"student-classid": "2",
	"student-provice": "山东",
	"student-city": "威海",
	"student-name": "宋丽君",
	"student-gender": "女",
	"student-address": "槐荫区10989号",
	"student-age": 12,
	"student-total-score": 596.5,
	"student-birthday": "2003-11-22",
	"student-hobby": "唱歌、跳舞、下棋",
	"relations-join-field": {
		"name": "student",
		"parent": "2"
	}
}

#######################################################################################
#初一三班学生数据
PUT /classes2student/_doc/10?routing=3
{
	"student-classid": "3",
	"student-provice": "河南",
	"student-city": "郑州",
	"student-name": "苏晓强",
	"student-gender": "男",
	"student-address": "商河县2869号",
	"student-age": 12,
	"student-total-score": 559.5,
	"student-birthday": "2004-05-11",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事",
	"relations-join-field": {
		"name": "student",
		"parent": "3"
	}
}

PUT /classes2student/_doc/11?routing=3
{
	"student-classid": "3",
	"student-name": "朱彤",
	"student-provice": "山东",
	"student-city": "菏泽",
	"student-gender": "女",
	"student-address": "济阳县10001号",
	"student-age": 14,
	"student-total-score": 683.5,
	"student-birthday": "2003-07-15",
	"student-hobby": "唱歌、游泳、篮球、乒乓球",
	"relations-join-field": {
		"name": "student",
		"parent": "3"
	}
}

PUT /classes2student/_doc/12?routing=3
{
	"student-classid": "3",
	"student-provice": "黑龙江",
	"student-city": "齐齐哈尔",
	"student-name": "刘玉花",
	"student-gender": "女",
	"student-address": "章丘区10989号",
	"student-age": 12,
	"student-total-score": 606.5,
	"student-birthday": "2003-09-21",
	"student-hobby": "唱歌、跳舞、下棋、篮球、乒乓球、足球",
	"relations-join-field": {
		"name": "student",
		"parent": "3"
	}
}

PUT /classes2student/_doc/13?routing=3
{
	"student-classid": "3",
	"student-provice": "山西",
	"student-city": "太原",
	"student-name": "李俊郎",
	"student-gender": "男",
	"student-address": "章丘区10989号",
	"student-age": 12,
	"student-total-score": 406.5,
	"student-birthday": "2003-08-28",
	"student-hobby": "唱歌",
	"relations-join-field": {
		"name": "student",
		"parent": "3"
	}
}
			

查询方法

# 查询所有文档(父子文档都能查出来)
GET classes2student/_search
{
  "query": {
    "match_all": {}
  }
}

# 根据父ID查询学生student 
# 这个地方官方文档的地址:https://www.elastic.co/guide/en/elasticsearch/reference/8.13/query-dsl-parent-id-query.html
GET classes2student/_search
{
  "query": {
    "parent_id": { 
      "type": "student",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "relations-join-field#classes", 
        "size": 10
      }
    }
  }
}

# Has child 根据子查父
# 这个地方官方文档的地址:https://www.elastic.co/guide/en/elasticsearch/reference/8.13/query-dsl-has-child-query.html
GET classes2student/_search
{
  "query": {
    "has_child": {
      "type": "student",
      "query": {
        "match_all": {}
      },
      "max_children": 10,
      "min_children": 3,
      "score_mode": "min"
    }
  }
}

# Has parent 根据父查子
# 这个地方官方文档的地址:https://www.elastic.co/guide/en/elasticsearch/reference/8.13/query-dsl-has-parent-query.html
GET classes2student/_search
{
  "query": {
    "has_parent": {
      "parent_type": "classes",
      "query": {
        "term": {
          "class-name": {
            "value": "一班"
          }
        }
      }
    }
  }
}

# Has parent 根据父查子(全文检索)
GET classes2student/_search
{
  "query": {
    "has_parent": {
      "parent_type": "classes",
      "query": {
        "match": {
          "describe": "无私奉献"
        }
      }
    }
  }
}

# Has child 根据子查父 聚合?size=0
GET classes2student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "student",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "student-city": "菏泽"
                    }
                  },
                  {
                    "match": {
                      "student-classid": "1"
                    }
                  },
                  {
                    "range": {
                      "student-birthday": {
                        "gte": "2003-05-11"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "child_aggs": {
      "children": {
        "type": "student"
      },
      "aggs": {
        "provice-aggs": {
          "terms": {
            "field": "student-provice",
            "size": 10,
            "order": {
              "_count": "asc"
            }
          },
          "aggs": {
            "city_aggs": {
              "terms": {
                "field": "student-city",
                "size": 10,
                "order": {
                  "_count": "asc"
                }
              }
            }
          }
        }
      }
    }
  }
}


  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Grain322

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值