parent-id-query.asciidoc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. {
  51. "has_parent": {
  52. "type": "blog",
  53. "query": {
  54. "term": {
  55. "_id": "1"
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. ==== Parameters
  62. This query has two required parameters:
  63. [horizontal]
  64. `type`:: The **child** type. This must be a type with `_parent` field.
  65. `id`:: The required parent id select documents must referrer to.
  66. `ignore_unmapped`:: When set to `true` this will ignore an unmapped `type` and will not match any
  67. documents for this query. This can be useful when querying multiple indexes
  68. which might have different mappings. When set to `false` (the default value)
  69. the query will throw an exception if the `type` is not mapped.