Search code examples
elasticsearchnest

Querying and returning Sub Document/Nested Objects in ElasticSearch


Good day:

I'm currently have the following structure indexed school -> children...meaning for every school document, there's a list of children sub documents. Children is a nested list of objects inside School. My objective is to query the parent school.id=id and only return the sub-documents matching children.userId = userId while paginating the children size/from. I'm not sure how to accomplish this but, any help using NEST would be appreciated.

Thanks.


Solution

  • EDIT:

    I didn't realize you only wanted to paginate the results from a single document. In that case you can use inner_hits which has it's own from and size parameters you can use.

    Reference: inner_hits documentation

    ORIGINAL ANSWER:

    I don't think you can paginate directly on the inner object when you have a nested type. Instead you would want to index school and children in separate documents and use a join type to create a parent/child relationship between them. Then you could use a has_parent query to search for children and paginate on the children returned.

    Reference:

    How to create the mapping: Join Relationship

    How to create the query: has_parent query