Has parent query

Has parent query

Returns child documents whose joined parent document matches a provided query. You can create parent-child relationships between documents in the same index using a join field mapping.

Because it performs a join, the has_parent query is slow compared to other queries. Its performance degrades as the number of matching parent documents increases. Each has_parent query in a search can increase query time significantly.

Example request

Index setup

To use the has_parent query, your index must include a join field mapping. For example:

  1. resp = client.indices.create(
  2. index="my-index-000001",
  3. mappings={
  4. "properties": {
  5. "my-join-field": {
  6. "type": "join",
  7. "relations": {
  8. "parent": "child"
  9. }
  10. },
  11. "tag": {
  12. "type": "keyword"
  13. }
  14. }
  15. },
  16. )
  17. print(resp)
  1. response = client.indices.create(
  2. index: 'my-index-000001',
  3. body: {
  4. mappings: {
  5. properties: {
  6. "my-join-field": {
  7. type: 'join',
  8. relations: {
  9. parent: 'child'
  10. }
  11. },
  12. tag: {
  13. type: 'keyword'
  14. }
  15. }
  16. }
  17. }
  18. )
  19. puts response
  1. const response = await client.indices.create({
  2. index: "my-index-000001",
  3. mappings: {
  4. properties: {
  5. "my-join-field": {
  6. type: "join",
  7. relations: {
  8. parent: "child",
  9. },
  10. },
  11. tag: {
  12. type: "keyword",
  13. },
  14. },
  15. },
  16. });
  17. console.log(response);
  1. PUT /my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "my-join-field": {
  6. "type": "join",
  7. "relations": {
  8. "parent": "child"
  9. }
  10. },
  11. "tag": {
  12. "type": "keyword"
  13. }
  14. }
  15. }
  16. }

Example query

  1. resp = client.search(
  2. index="my-index-000001",
  3. query={
  4. "has_parent": {
  5. "parent_type": "parent",
  6. "query": {
  7. "term": {
  8. "tag": {
  9. "value": "Elasticsearch"
  10. }
  11. }
  12. }
  13. }
  14. },
  15. )
  16. print(resp)
  1. const response = await client.search({
  2. index: "my-index-000001",
  3. query: {
  4. has_parent: {
  5. parent_type: "parent",
  6. query: {
  7. term: {
  8. tag: {
  9. value: "Elasticsearch",
  10. },
  11. },
  12. },
  13. },
  14. },
  15. });
  16. console.log(response);
  1. GET /my-index-000001/_search
  2. {
  3. "query": {
  4. "has_parent": {
  5. "parent_type": "parent",
  6. "query": {
  7. "term": {
  8. "tag": {
  9. "value": "Elasticsearch"
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }

Top-level parameters for has_parent

parent_type

(Required, string) Name of the parent relationship mapped for the join field.

query

(Required, query object) Query you wish to run on parent documents of the parent_type field. If a parent document matches the search, the query returns its child documents.

score

(Optional, Boolean) Indicates whether the relevance score of a matching parent document is aggregated into its child documents. Defaults to false.

If false, Elasticsearch ignores the relevance score of the parent document. Elasticsearch also assigns each child document a relevance score equal to the query‘s boost, which defaults to 1.

If true, the relevance score of the matching parent document is aggregated into its child documents’ relevance scores.

ignore_unmapped

(Optional, Boolean) Indicates whether to ignore an unmapped parent_type and not return any documents instead of an error. Defaults to false.

If false, Elasticsearch returns an error if the parent_type is unmapped.

You can use this parameter to query multiple indices that may not contain the parent_type.

Notes

Sorting

You cannot sort the results of a has_parent query using standard sort options.

If you need to sort returned documents by a field in their parent documents, use a function_score query and sort by _score. For example, the following query sorts returned documents by the view_count field of their parent documents.

  1. resp = client.search(
  2. query={
  3. "has_parent": {
  4. "parent_type": "parent",
  5. "score": True,
  6. "query": {
  7. "function_score": {
  8. "script_score": {
  9. "script": "_score * doc['view_count'].value"
  10. }
  11. }
  12. }
  13. }
  14. },
  15. )
  16. print(resp)
  1. const response = await client.search({
  2. query: {
  3. has_parent: {
  4. parent_type: "parent",
  5. score: true,
  6. query: {
  7. function_score: {
  8. script_score: {
  9. script: "_score * doc['view_count'].value",
  10. },
  11. },
  12. },
  13. },
  14. },
  15. });
  16. console.log(response);
  1. GET /_search
  2. {
  3. "query": {
  4. "has_parent": {
  5. "parent_type": "parent",
  6. "score": true,
  7. "query": {
  8. "function_score": {
  9. "script_score": {
  10. "script": "_score * doc['view_count'].value"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }