当前位置:   article > 正文

ES的嵌套文档的使用_es嵌套文档

es嵌套文档

在ES中使用的嵌套文档实现如下的功能:
1.场景是班级(class),学生(student)的数据存储
2.创建班级索引(class),班级的信息有 班级的名字(class_name)
3.学生的信息有姓名(student_name),学生的入学时间(start_date) 
4.学生(student)是班级(class)的嵌套文档
要求:
1.创建班级索引
2.插入一个班级,三个学生信息
3.查询班级学生信息:查询条件是学生姓名(student_name),学生的入学时间(start_date)倒序进行排序

  1. PUT class_index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "class_name": {
  6. "type": "text"
  7. },
  8. "students": {
  9. "type": "nested",
  10. "properties": {
  11. "student_name": {
  12. "type": "text"
  13. },
  14. "start_date": {
  15. "type": "date"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. PUT class_index/_doc/1
  23. {
  24. "class_name": "ClassA",
  25. "students": [
  26. {
  27. "student_name": "StudentA1",
  28. "start_date": "2022-01-01"
  29. },
  30. {
  31. "student_name": "StudentA2",
  32. "start_date": "2021-09-01"
  33. },
  34. {
  35. "student_name": "StudentA3",
  36. "start_date": "2020-08-01"
  37. }
  38. ]
  39. }
  40. PUT class_index/_doc/2
  41. {
  42. "class_name": "ClassB",
  43. "students": [
  44. {
  45. "student_name": "StudentA1",
  46. "start_date": "2022-02-01"
  47. },
  48. {
  49. "student_name": "StudentA2",
  50. "start_date": "2021-09-01"
  51. },
  52. {
  53. "student_name": "StudentA3",
  54. "start_date": "2020-08-01"
  55. }
  56. ]
  57. }
  58. GET class_index/_search
  59. {
  60. "from": 0,
  61. "size": 5,
  62. "query": {
  63. "match_all": {}
  64. }
  65. }
  66. GET class_index/_search
  67. {
  68. "query": {
  69. "match_all": {}
  70. }
  71. }
  72. GET class_index/_search
  73. {
  74. "query": {
  75. "nested": {
  76. "path": "students",
  77. "query": {
  78. "bool": {
  79. "must": [
  80. {
  81. "match": {
  82. "students.student_name": "StudentA1"
  83. }
  84. }
  85. ]
  86. }
  87. },
  88. "inner_hits": {},
  89. "score_mode": "none"
  90. }
  91. },
  92. "sort": [
  93. {
  94. "students.start_date": {
  95. "order": "desc",
  96. "mode": "max",
  97. "nested_path": "students",
  98. "nested_filter": {
  99. "match" :{
  100. "students.student_name": "StudentA1"
  101. }
  102. }
  103. }
  104. }
  105. ]
  106. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/441950
推荐阅读
相关标签
  

闽ICP备14008679号