# Querying parent-child documents

**URL:** <https://discuss.elastic.co/t/querying-parent-child-documents/250631>\
**Category:** Elasticsearch\
**Created:** [October 1, 2020, 9:37am UTC](https://discuss.elastic.co/t/querying-parent-child-documents/250631 "2020-10-01T09:37:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyam\_Maheshwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyam_maheshwari/32/47961_2.png) [@Priyam\_Maheshwari](https://discuss.elastic.co/u/Priyam_Maheshwari)\
**Post date:** [October 1, 2020, 9:37am UTC](https://discuss.elastic.co/t/querying-parent-child-documents/250631/1 "2020-10-01T09:37:38Z")

</div>

I have a following index which stores the course details (I have truncated some attributes for brevity):

```auto
{
  "settings": {
    "index": {
      "number_of_replicas": "1",
      "number_of_shards": "1"
    }
  },
  "aliases": {
    "course": {
      
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "id": {
        "type": "integer"
      },
      "max_per_user": {
        "type": "integer"
      },
      "event_user_id": {
        "type": "integer"
      },
      "event_current_count": {
        "type": "integer"
      },
      "course_event": {
        "type": "join",
        "relations": {
          "course": "event"
        }
      }
    }
  }
}

```

Here _max\_per\_user_ is number of times a user can complete the course. A user is allowed through a course multiple times but not more than _max\_per\_user_ for a course. _event\_current\_count_ tracks the number of times a user has gone through the course. The parent document will have fields - name,id and max\_per\_user. The child documents will have values for _event\_user\_id_ and _event\_current\_count_ . How do I construct a search query to get all parent documents i.e courses for a given user\_id where `event_current_count<max_per_user` . Basically I have a user\_id and for that user I want to find all courses where the user is still eligible.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 1, 2020, 1:08pm UTC](https://discuss.elastic.co/t/querying-parent-child-documents/250631/2 "2020-10-01T13:08:46Z")

</div>

Hey,

can you explain the connection to parent/child? It seems that all the data is within this document, so there is no need for parent/child queries to answer your question.

You could use a [script query](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-script-query.html) to compare those two fields, but I think it might make more sense to create an additional field, that is a boolean, which can be calculated at index time, so that your filter is really just filtering on true/false on that field.

hope that helps, and if I am missing context, please provide a **fully reproducible** example.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 29, 2020, 1:08pm UTC](https://discuss.elastic.co/t/querying-parent-child-documents/250631/3 "2020-10-29T13:08:47Z")

</div>

Closed on October 29, 2020, 1:08pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
