赞
踩
在ES中使用的嵌套文档实现如下的功能:
1.场景是班级(class),学生(student)的数据存储
2.创建班级索引(class),班级的信息有 班级的名字(class_name)
3.学生的信息有姓名(student_name),学生的入学时间(start_date)
4.学生(student)是班级(class)的嵌套文档
要求:
1.创建班级索引
2.插入一个班级,三个学生信息
3.查询班级学生信息:查询条件是学生姓名(student_name),学生的入学时间(start_date)倒序进行排序
- PUT class_index
- {
- "mappings": {
- "properties": {
- "class_name": {
- "type": "text"
- },
- "students": {
- "type": "nested",
- "properties": {
- "student_name": {
- "type": "text"
- },
- "start_date": {
- "type": "date"
- }
- }
- }
- }
- }
- }
-
- PUT class_index/_doc/1
- {
- "class_name": "ClassA",
- "students": [
- {
- "student_name": "StudentA1",
- "start_date": "2022-01-01"
- },
- {
- "student_name": "StudentA2",
- "start_date": "2021-09-01"
- },
- {
- "student_name": "StudentA3",
- "start_date": "2020-08-01"
- }
- ]
- }
-
- PUT class_index/_doc/2
- {
- "class_name": "ClassB",
- "students": [
- {
- "student_name": "StudentA1",
- "start_date": "2022-02-01"
- },
- {
- "student_name": "StudentA2",
- "start_date": "2021-09-01"
- },
- {
- "student_name": "StudentA3",
- "start_date": "2020-08-01"
- }
- ]
- }
- GET class_index/_search
- {
- "from": 0,
- "size": 5,
- "query": {
- "match_all": {}
- }
- }
-
- GET class_index/_search
- {
- "query": {
- "match_all": {}
- }
- }
-
- GET class_index/_search
- {
- "query": {
- "nested": {
- "path": "students",
- "query": {
- "bool": {
- "must": [
- {
- "match": {
- "students.student_name": "StudentA1"
- }
- }
- ]
- }
- },
- "inner_hits": {},
- "score_mode": "none"
- }
- },
- "sort": [
- {
- "students.start_date": {
- "order": "desc",
- "mode": "max",
- "nested_path": "students",
- "nested_filter": {
- "match" :{
- "students.student_name": "StudentA1"
- }
- }
- }
- }
- ]
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。