parent-id-query.asciidoc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [[query-dsl-parent-id-query]]
  2. === Parent Id Query
  3. added[5.0.0]
  4. The `parent_id` query can be used to find child documents which belong to a particular parent.
  5. Given the following mapping definition:
  6. [source,js]
  7. --------------------------------------------
  8. PUT /my_index
  9. {
  10. "settings": {
  11. "mapping.single_type": false
  12. },
  13. "mappings": {
  14. "blog_post": {
  15. "properties": {
  16. "name": {
  17. "type": "keyword"
  18. }
  19. }
  20. },
  21. "blog_tag": {
  22. "_parent": {
  23. "type": "blog_post"
  24. },
  25. "_routing": {
  26. "required": true
  27. }
  28. }
  29. }
  30. }
  31. --------------------------------------------
  32. // CONSOLE
  33. // TESTSETUP
  34. [source,js]
  35. --------------------------------------------------
  36. GET /my_index/_search
  37. {
  38. "query": {
  39. "parent_id" : {
  40. "type" : "blog_tag",
  41. "id" : "1"
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // CONSOLE
  47. The above is functionally equivalent to using the following
  48. <<query-dsl-has-parent-query, `has_parent`>> query, but performs
  49. better as it does not need to do a join:
  50. [source,js]
  51. --------------------------------------------------
  52. GET /my_index/_search
  53. {
  54. "query": {
  55. "has_parent": {
  56. "parent_type": "blog_post",
  57. "query": {
  58. "term": {
  59. "_id": "1"
  60. }
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // CONSOLE
  67. ==== Parameters
  68. This query has two required parameters:
  69. [horizontal]
  70. `type`:: The **child** type. This must be a type with `_parent` field.
  71. `id`:: The required parent id select documents must referrer to.
  72. `ignore_unmapped`:: When set to `true` this will ignore an unmapped `type` and will not match any
  73. documents for this query. This can be useful when querying multiple indexes
  74. which might have different mappings. When set to `false` (the default value)
  75. the query will throw an exception if the `type` is not mapped.