当前位置:   article > 正文

Elasticsearch处理表关联关系的N种方式_elasticsearch多表关联查询

elasticsearch多表关联查询

Elasticsearch处理表关联关系是比较复杂的问题,处理不好会出现性能问题、数据一致性问题等;

今天我们特意分享一下几种方式,对象类型(宽表)、嵌套类型、父子关联关系、应用端关联,每种方式都有特定的业务需求,具体可以根据业务场景选择,废话少数,现在开始。

一、对象类型

我们以博客为例,在每一博客的文档中都保留作者的信息,如果作者信息发生变化,需要修改相关的博客文档。

1、创建博客的索引

  1. PUT /nandao_blog_index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "content": {
  6. "type": "text"
  7. },
  8. "time": {
  9. "type": "date"
  10. },
  11. "user": {
  12. "properties": {
  13. "city": {
  14. "type": "text"
  15. },
  16. "userid": {
  17. "type": "long"
  18. },
  19. "username": {
  20. "type": "keyword"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

结果:

  1. {
  2. "acknowledged" : true,
  3. "shards_acknowledged" : true,
  4. "index" : "nandao_blog_index"
  5. }

 2、修改映射,添加博客的name

  1. POST /nandao_blog_index/_mapping
  2. {
  3. "properties": {
  4. "name": {
  5. "type": "text"
  6. }
  7. }
  8. }

结果:

  1. {
  2. "acknowledged" : true
  3. }

 3、插入两条条 blog信息

  1. PUT /nandao_blog_index/_doc/1
  2. {
  3. "content": "I like Elasticsearch",
  4. "time": "2022‐01‐01T00:00:00",
  5. "user": {
  6. "userid": 1,
  7. "username": "Nandao",
  8. "city": "Changsha"
  9. }
  10. }
  11. PUT /nandao_blog_index/_doc/2
  12. {
  13. "content": "I like Java",
  14. "time": "2022‐01‐01T00:00:00",
  15. "user": {
  16. "userid": 1,
  17. "username": "Nandao",
  18. "city": "Changsha"
  19. }
  20. }

4、查询 blog信息

  1. POST /nandao_blog_index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [{
  6. "match": {
  7. "content": "Elasticsearch"
  8. }
  9. },
  10. {
  11. "match": {
  12. "user.username": "Nandao"
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

结果就会查到一条信息 

 5、包含对象数组的文档,创建索引

  1. PUT /nandao_movies_index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "actors": {
  6. "properties": {
  7. "first_name": {
  8. "type": "keyword"
  9. },
  10. "last_name": {
  11. "type": "keyword"
  12. }
  13. }
  14. },
  15. "title": {
  16. "type": "text",
  17. "fields": {
  18. "keyword": {
  19. "type": "keyword",
  20. "ignore_above": 256
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

6、创建一条数据:

  1. POST /nandao_movies_index/_doc/1
  2. {
  3. "title": "Speed",
  4. "actors": [
  5. {
  6. "first_name": "Keanu",
  7. "last_name": "Reeves"
  8. },
  9. {
  10. "first_name": "Dennis",
  11. "last_name": "Hopper"
  12. }
  13. ]
  14. }

7、查询电影信息

  1. POST /nandao_movies_index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [{
  6. "match": {
  7. "actors.first_name": "Keanu"
  8. }
  9. },
  10. {
  11. "match": {
  12. "actors.last_name": "Hopper"
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

结果:

  1. {
  2. "took" : 2,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.723315,
  16. "hits" : [
  17. {
  18. "_index" : "erpx_test_order_array",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 0.723315,
  22. "_source" : {
  23. "title" : "Speed",
  24. "actors" : [
  25. {
  26. "first_name" : "Keanu",
  27. "last_name" : "Reeves"
  28. },
  29. {
  30. "first_name" : "Dennis",
  31. "last_name" : "Hopper"
  32. }
  33. ]
  34. }
  35. }
  36. ]
  37. }
  38. }

 搜到了不需要的结果,存储时,内部对象的边界并没有考虑在内,JSON格式被处理成扁平式键值对的结构。当对多个字段进行查询时,导致了意外的搜索结果。可以用Nested Data Type解决这个问题 ,下面我们会分析。

二、嵌套类型

1、场景索引

  1. PUT /nandao_movies_index_nested
  2. {
  3. "mappings": {
  4. "properties": {
  5. "actors": {
  6. "type": "nested",
  7. "properties": {
  8. "first_name": {
  9. "type": "keyword"
  10. },
  11. "last_name": {
  12. "type": "keyword"
  13. }
  14. }
  15. },
  16. "title": {
  17. "type": "text",
  18. "fields": {
  19. "keyword": {
  20. "type": "keyword",
  21. "ignore_above": 256
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

2、添加数据

  1. POST /nandao_movies_index_nested/_doc/1
  2. {
  3. "title": "Speed",
  4. "actors": [{
  5. "first_name": "Keanu",
  6. "last_name": "Reeves"
  7. },
  8. {
  9. "first_name": "Dennis",
  10. "last_name": "Hopper"
  11. }
  12. ]
  13. }

3、Nested方式 查询

  1. POST /nandao_movies_index_nested/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [{
  6. "match": {
  7. "title": "Speed"
  8. }
  9. },
  10. {
  11. "nested": {
  12. "path": "actors",
  13. "query": {
  14. "bool": {
  15. "must": [{
  16. "match": {
  17. "actors.first_name": "Keanu"
  18. }
  19. },
  20. {
  21. "match": {
  22. "actors.last_name": "Hopper"
  23. }
  24. }
  25. ]
  26. }
  27. }
  28. }
  29. }
  30. ]
  31. }
  32. }
  33. }

显然结果 没有查到数据:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. }
  18. }

 

4、Nested Aggregation 查询

  1. POST /nandao_movies_index_nested/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "actors": {
  6. "nested": {
  7. "path": "actors"
  8. },
  9. "aggs": {
  10. "actor_name": {
  11. "terms": {
  12. "field": "actors.first_name",
  13. "size": 10
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

5、普通 aggregation不工作查询

  1. POST /erpx_test_order_nested/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "NAME": {
  6. "terms": {
  7. "field": "actors.first_name",
  8. "size": 10
  9. }
  10. }
  11. }
  12. }

三、父子关系类型:即join 联合查询

对象和Nested对象的局限性: 每次更新,可能需要重新索引整个对象(包括根对象和嵌套对象)
ES提供了类似关系型数据库中Join 的实现。

使用Join数据类型实现,可以通过维护Parent/ Child的关系,从而分离两个对象父文档和子文档是两个独立的文档更新父文档无需重新索引子文档。

子文档被添加,更新或者删除也不会影响到父文档和其他的子文档。

1、创建父子索引

  1. PUT /nandao_relation_index
  2. {
  3. "settings": {
  4. "number_of_shards": 2
  5. },
  6. "mappings": {
  7. "properties": {
  8. "blog_comments_relation": {
  9. "type": "join",
  10. "relations": {
  11. "blog": "comment"
  12. }
  13. },
  14. "content": {
  15. "type": "text"
  16. },
  17. "title": {
  18. "type": "keyword"
  19. }
  20. }
  21. }
  22. }

解释:

 

 

2、创建两个父文档

  1. PUT /nandao_relation_index/_doc/blog1
  2. {
  3. "title": "Learning Elasticsearch",
  4. "content": "learning ELK ",
  5. "blog_comments_relation": {
  6. "name": "blog"
  7. }
  8. }
  9. PUT /nandao_relation_index/_doc/blog2
  10. {
  11. "title": "Learning Hadoop",
  12. "content": "learning Hadoop",
  13. "blog_comments_relation": {
  14. "name": "blog"
  15. }
  16. }

解释:

 

 3、创建三个子文档

  1. PUT /nandao_relation_index/_doc/comment1?routing=blog1
  2. {
  3. "comment": "I am learning ELK",
  4. "username": "Jack",
  5. "blog_comments_relation": {
  6. "name": "comment",
  7. "parent": "blog1"
  8. }
  9. }
  10. PUT /nandao_relation_index/_doc/comment2?routing=blog2
  11. {
  12. "comment":"I like Hadoop!!!!!",
  13. "username":"Jack",
  14. "blog_comments_relation":{
  15. "name":"comment",
  16. "parent":"blog2"
  17. }
  18. }
  19. PUT /nandao_relation_index/_doc/comment3?routing=blog2
  20. {
  21. "comment": "Hello Hadoop",
  22. "username": "Bob",
  23. "blog_comments_relation": {
  24. "name": "comment",
  25. "parent": "blog2"
  26. }
  27. }

4、查询所有文档

POST /nandao_relation_index/_search

显示父子五个文档:

  1. {
  2. "took" : 2,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 2,
  6. "successful" : 2,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 5,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "erpx_test_order_test",
  19. "_type" : "_doc",
  20. "_id" : "blog1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "title" : "Learning Elasticsearch",
  24. "content" : "learning ELK ",
  25. "blog_comments_relation" : {
  26. "name" : "blog"
  27. }
  28. }
  29. },
  30. {
  31. "_index" : "erpx_test_order_test",
  32. "_type" : "_doc",
  33. "_id" : "blog2",
  34. "_score" : 1.0,
  35. "_source" : {
  36. "title" : "Learning Hadoop",
  37. "content" : "learning Hadoop",
  38. "blog_comments_relation" : {
  39. "name" : "blog"
  40. }
  41. }
  42. },
  43. {
  44. "_index" : "erpx_test_order_test",
  45. "_type" : "_doc",
  46. "_id" : "comment1",
  47. "_score" : 1.0,
  48. "_routing" : "blog1",
  49. "_source" : {
  50. "comment" : "I am learning ELK",
  51. "username" : "Jack",
  52. "blog_comments_relation" : {
  53. "name" : "comment",
  54. "parent" : "blog1"
  55. }
  56. }
  57. },
  58. {
  59. "_index" : "erpx_test_order_test",
  60. "_type" : "_doc",
  61. "_id" : "comment2",
  62. "_score" : 1.0,
  63. "_routing" : "blog2",
  64. "_source" : {
  65. "comment" : "I like Hadoop!!!!!",
  66. "username" : "Jack",
  67. "blog_comments_relation" : {
  68. "name" : "comment",
  69. "parent" : "blog2"
  70. }
  71. }
  72. },
  73. {
  74. "_index" : "erpx_test_order_test",
  75. "_type" : "_doc",
  76. "_id" : "comment3",
  77. "_score" : 1.0,
  78. "_routing" : "blog2",
  79. "_source" : {
  80. "comment" : "Hello Hadoop",
  81. "username" : "Bob",
  82. "blog_comments_relation" : {
  83. "name" : "comment",
  84. "parent" : "blog2"
  85. }
  86. }
  87. }
  88. ]
  89. }
  90. }

 5、根据父文档ID查看

GET /nandao_relation_index/_doc/blog2

结果:

  1. {
  2. "_index" : "nandao_relation_index",
  3. "_type" : "_doc",
  4. "_id" : "blog2",
  5. "_version" : 1,
  6. "_seq_no" : 1,
  7. "_primary_term" : 1,
  8. "found" : true,
  9. "_source" : {
  10. "title" : "Learning Hadoop",
  11. "content" : "learning Hadoop",
  12. "blog_comments_relation" : {
  13. "name" : "blog"
  14. }
  15. }
  16. }

 6、根据Parent Id 查询

  1. POST /nandao_relation_index/_search
  2. {
  3. "query": {
  4. "parent_id": {
  5. "type": "comment",
  6. "id": "blog2"
  7. }
  8. }
  9. }

结果:

  1. {
  2. "took" : 6,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 2,
  6. "successful" : 2,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 2,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.53899646,
  16. "hits" : [
  17. {
  18. "_index" : "erpx_test_order_test",
  19. "_type" : "_doc",
  20. "_id" : "comment2",
  21. "_score" : 0.53899646,
  22. "_routing" : "blog2",
  23. "_source" : {
  24. "comment" : "I like Hadoop!!!!!",
  25. "username" : "Jack",
  26. "blog_comments_relation" : {
  27. "name" : "comment",
  28. "parent" : "blog2"
  29. }
  30. }
  31. },
  32. {
  33. "_index" : "erpx_test_order_test",
  34. "_type" : "_doc",
  35. "_id" : "comment3",
  36. "_score" : 0.53899646,
  37. "_routing" : "blog2",
  38. "_source" : {
  39. "comment" : "Hello Hadoop",
  40. "username" : "Bob",
  41. "blog_comments_relation" : {
  42. "name" : "comment",
  43. "parent" : "blog2"
  44. }
  45. }
  46. }
  47. ]
  48. }
  49. }

 7、 Has Child 查询,返回父文档

  1. POST /nandao_relation_index/_search
  2. {
  3. "query": {
  4. "has_child": {
  5. "type": "comment",
  6. "query": {
  7. "match": {
  8. "username": "Jack"
  9. }
  10. }
  11. }
  12. }
  13. }

结果:

  1. {
  2. "took" : 14,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 2,
  6. "successful" : 2,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 2,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "erpx_test_order_test",
  19. "_type" : "_doc",
  20. "_id" : "blog1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "title" : "Learning Elasticsearch",
  24. "content" : "learning ELK ",
  25. "blog_comments_relation" : {
  26. "name" : "blog"
  27. }
  28. }
  29. },
  30. {
  31. "_index" : "erpx_test_order_test",
  32. "_type" : "_doc",
  33. "_id" : "blog2",
  34. "_score" : 1.0,
  35. "_source" : {
  36. "title" : "Learning Hadoop",
  37. "content" : "learning Hadoop",
  38. "blog_comments_relation" : {
  39. "name" : "blog"
  40. }
  41. }
  42. }
  43. ]
  44. }
  45. }

 注意:
1)父文档和子文档必须存在相同的分片上,能够确保查询join 的性能。
2)当指定子文档时候,必须指定它的父文档ld。使用routing参数来保证,分配到
相同的分片。

 四、应用端关联

1、此方案比较好理解,就是多长查询,下一次查询依赖上一次查询结果。

常用的嵌套文档和父子文档对比:

 到此、es相关的DSL语句分享完毕,后期我们分享一下相关的javaAPI,也是实战的必经之路,敬请期待!

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

闽ICP备14008679号