Querying parent-child documents

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

{
  "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.

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 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.

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