当前位置:   article > 正文

Elasticsearch索引的父子关系(index parent-child)

elasticsearch5 通过父文档查询子文档

Elasticsearch允许给文档建立父子关系,这篇博客介绍文档的父子关系是如何映射的(Parent-Child Mapping)、如何索引父子文档(Indexing Parents and Children)、如何通过子文档查询父文档 
(Finding Parents by Their Children)、如何通过父文档查询子文档(Finding Children by Their Parents)。

一、父子文档的mapping

建立文档的父子关系最重要的一步是在创建索引的时候在mapping中声明哪个是父文档哪个是子文档。官网文档给了一个公司在很多城市的分支(branch)和每个分支有相关员工(employee)的例子,如果想把员工和他们工作的分公司关联起来,我们需要告诉Elasticsearch文档之间的父子关系,这里employee是child type,branch是parent type,在mapping中声明:

  1. curl -XPUT "http://192.168.0.224:9200/company" -d '
  2. {
  3. "mappings": {
  4. "branch": {},
  5. "employee": {
  6. "_parent": {
  7. "type": "branch"
  8. }
  9. }
  10. }
  11. }
  12. '

 

这样我们创建了一个索引,并制定了2个type和它们之间的父子关系.

二、父子文档的索引

2.1索引父文档

索引父文档和索引一般的文档没有任何区别。 
准备几条公司的数据,存在company.json文件中:

  1. { "index": { "_id": "london" }}
  2. { "name": "London Westminster", "city": "London", "country": "UK" }
  3. { "index": { "_id": "liverpool" }}
  4. { "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }
  5. { "index": { "_id": "paris" }}
  6. { "name": "Champs Élysées", "city": "Paris", "country": "France" }

 

使用Bulk端点批量导入:

  1. curl -XPOST "http://192.168.0.224:9200/company/branch/_bulk?pretty" --data-binary @company.json

 

2.2索引子文档

索引子文档需要制定子文档的父ID,给子文档的每条文档设置parent属性的value为父文档id即可:

  1. curl -XPUT "http://192.168.0.224:9200/company/employee/1?parent=london&pretty" -d '
  2. {
  3. "name": "Alice Smith",
  4. "dob": "1970-10-24",
  5. "hobby": "hiking"
  6. }
  7. '

 

执行bulk命令:

curl -XPOST "http://192.168.0.224:9200/company/employee/_bulk?pretty" --data-binary @employee.json

 

三、通过子文档查询父文档

搜索含有1980年以后出生的employee的branch:

  1. curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '
  2. {
  3. "query": {
  4. "has_child": {
  5. "type": "employee",
  6. "query": {
  7. "range": {
  8. "dob": {
  9. "gte": "1980-01-01"
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. '
  17. {
  18. "took" : 19,
  19. "timed_out" : false,
  20. "_shards" : {
  21. "total" : 5,
  22. "successful" : 5,
  23. "failed" : 0
  24. },
  25. "hits" : {
  26. "total" : 2,
  27. "max_score" : 1.0,
  28. "hits" : [ {
  29. "_index" : "company",
  30. "_type" : "branch",
  31. "_id" : "paris",
  32. "_score" : 1.0,
  33. "_source" : {
  34. "name" : "Champs Élysées",
  35. "city" : "Paris",
  36. "country" : "France"
  37. }
  38. }, {
  39. "_index" : "company",
  40. "_type" : "branch",
  41. "_id" : "london",
  42. "_score" : 1.0,
  43. "_source" : {
  44. "name" : "London Westminster",
  45. "city" : "London",
  46. "country" : "UK"
  47. }
  48. } ]
  49. }
  50. }

 

查询name中含有“Alice Smith”的branch:

  1. curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '
  2. {
  3. "query": {
  4. "has_child": {
  5. "type": "employee",
  6. "score_mode": "max",
  7. "query": {
  8. "match": {
  9. "name": "Alice Smith"
  10. }
  11. }
  12. }
  13. }
  14. }'
  15. {
  16. "took" : 20,
  17. "timed_out" : false,
  18. "_shards" : {
  19. "total" : 5,
  20. "successful" : 5,
  21. "failed" : 0
  22. },
  23. "hits" : {
  24. "total" : 2,
  25. "max_score" : 1.2422675,
  26. "hits" : [ {
  27. "_index" : "company",
  28. "_type" : "branch",
  29. "_id" : "london",
  30. "_score" : 1.2422675,
  31. "_source" : {
  32. "name" : "London Westminster",
  33. "city" : "London",
  34. "country" : "UK"
  35. }
  36. }, {
  37. "_index" : "company",
  38. "_type" : "branch",
  39. "_id" : "liverpool",
  40. "_score" : 0.30617762,
  41. "_source" : {
  42. "name" : "Liverpool Central",
  43. "city" : "Liverpool",
  44. "country" : "UK"
  45. }
  46. } ]
  47. }
  48. }

 

搜索最少含有2个employee的branch:

  1. curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '{
  2. "query": {
  3. "has_child": {
  4. "type": "employee",
  5. "min_children": 2,
  6. "query": {
  7. "match_all": {}
  8. }
  9. }
  10. }
  11. }
  12. '
  13. {
  14. "took" : 17,
  15. "timed_out" : false,
  16. "_shards" : {
  17. "total" : 5,
  18. "successful" : 5,
  19. "failed" : 0
  20. },
  21. "hits" : {
  22. "total" : 1,
  23. "max_score" : 1.0,
  24. "hits" : [ {
  25. "_index" : "company",
  26. "_type" : "branch",
  27. "_id" : "london",
  28. "_score" : 1.0,
  29. "_source" : {
  30. "name" : "London Westminster",
  31. "city" : "London",
  32. "country" : "UK"
  33. }
  34. } ]
  35. }
  36. }

 

四、通过父文档查询子文档

搜搜工作在UK的employee:

  1. curl -XGET "http://192.168.0.224:9200/company/employee/_search?pretty" -d '{
  2. "query": {
  3. "has_parent": {
  4. "type": "branch",
  5. "query": {
  6. "match": {
  7. "country": "UK"
  8. }
  9. }
  10. }
  11. }
  12. }'
  13. {
  14. "took" : 15,
  15. "timed_out" : false,
  16. "_shards" : {
  17. "total" : 5,
  18. "successful" : 5,
  19. "failed" : 0
  20. },
  21. "hits" : {
  22. "total" : 3,
  23. "max_score" : 1.0,
  24. "hits" : [ {
  25. "_index" : "company",
  26. "_type" : "employee",
  27. "_id" : "3",
  28. "_score" : 1.0,
  29. "_parent" : "liverpool",
  30. "_routing" : "liverpool",
  31. "_source" : {
  32. "name" : "Barry Smith",
  33. "dob" : "1979-04-01",
  34. "hobby" : "hiking"
  35. }
  36. }, {
  37. "_index" : "company",
  38. "_type" : "employee",
  39. "_id" : "1",
  40. "_score" : 1.0,
  41. "_parent" : "london",
  42. "_routing" : "london",
  43. "_source" : {
  44. "name" : "Alice Smith",
  45. "dob" : "1970-10-24",
  46. "hobby" : "hiking"
  47. }
  48. }, {
  49. "_index" : "company",
  50. "_type" : "employee",
  51. "_id" : "2",
  52. "_score" : 1.0,
  53. "_parent" : "london",
  54. "_routing" : "london",
  55. "_source" : {
  56. "name" : "Mark Thomas",
  57. "dob" : "1982-05-16",
  58. "hobby" : "diving"
  59. }
  60. } ]
  61. }
  62. }

 

 

转载于:https://my.oschina.net/u/727883/blog/794028

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

闽ICP备14008679号