parent-id-query.asciidoc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. [[query-dsl-parent-id-query]]
  2. === Parent Id Query
  3. The `parent_id` query can be used to find child documents which belong to a particular parent.
  4. Given the following mapping definition:
  5. [source,js]
  6. --------------------------------------------
  7. PUT my_index
  8. {
  9. "mappings": {
  10. "_doc": {
  11. "properties": {
  12. "my_join_field": {
  13. "type": "join",
  14. "relations": {
  15. "my_parent": "my_child"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. PUT my_index/_doc/1?refresh
  23. {
  24. "text": "This is a parent document",
  25. "my_join_field": "my_parent"
  26. }
  27. PUT my_index/_doc/2?routing=1&refresh
  28. {
  29. "text": "This is a child document",
  30. "my_join_field": {
  31. "name": "my_child",
  32. "parent": "1"
  33. }
  34. }
  35. --------------------------------------------
  36. // CONSOLE
  37. // TESTSETUP
  38. [source,js]
  39. --------------------------------------------------
  40. GET /my_index/_search
  41. {
  42. "query": {
  43. "parent_id": {
  44. "type": "my_child",
  45. "id": "1"
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. // CONSOLE
  51. ==== Parameters
  52. This query has two required parameters:
  53. [horizontal]
  54. `type`:: The **child** type name, as specified in the <<parent-join,`join` field>>.
  55. `id`:: The ID of the parent document.
  56. `ignore_unmapped`:: When set to `true` this will ignore an unmapped `type` and will not match any
  57. documents for this query. This can be useful when querying multiple indexes
  58. which might have different mappings. When set to `false` (the default value)
  59. the query will throw an exception if the `type` is not mapped.