parent-id-query.asciidoc 1.9 KB

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