Parent ID query

The parent_id query returns child documents whose parent document has the specified ID. You can establish parent/child relationships between documents in the same index by using a join field type.

Example

Before you can run a parent_id query, your index must contain a join field in order to establish parent/child relationships. The index mapping request uses the following format:

  1. PUT /example_index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "relationship_field": {
  6. "type": "join",
  7. "relations": {
  8. "parent_doc": "child_doc"
  9. }
  10. }
  11. }
  12. }
  13. }

copy

For this example, first configure an index that contains documents representing products and their brands as described in the has_child query example.

To search for child documents of a specific parent document, use a parent_id query. The following query returns child documents (products) whose parent document has the ID 1:

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "parent_id": {
  5. "type": "product",
  6. "id": "1"
  7. }
  8. }
  9. }

copy

The response returns the child product:

  1. {
  2. "took": 57,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.87546873,
  16. "hits": [
  17. {
  18. "_index": "testindex1",
  19. "_id": "3",
  20. "_score": 0.87546873,
  21. "_routing": "1",
  22. "_source": {
  23. "name": "Mechanical watch",
  24. "sales_count": 150,
  25. "product_to_brand": {
  26. "name": "product",
  27. "parent": "1"
  28. }
  29. }
  30. }
  31. ]
  32. }
  33. }

Parameters

The following table lists all top-level parameters supported by parent_id queries.

ParameterRequired/OptionalDescription
typeRequiredSpecifies the name of the child relationship as defined in the join field mapping.
idRequiredThe ID of the parent document. The query returns child documents associated with this parent document.
ignore_unmappedOptionalIndicates whether to ignore unmapped type fields and not return documents instead of throwing an error. You can provide this parameter when querying multiple indexes, some of which may not contain the type field. Default is false.