parent-id-query.asciidoc 2.0 KB

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