ES的嵌套文档的使用

在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"
           }
         }
      }
    }
  ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值