# Suggestions for modeling parent-child relationship with sorting by child cardinality

**URL:** <https://discuss.elastic.co/t/suggestions-for-modeling-parent-child-relationship-with-sorting-by-child-cardinality/31272>\
**Category:** Elasticsearch\
**Created:** [September 28, 2015, 3:51pm UTC](https://discuss.elastic.co/t/suggestions-for-modeling-parent-child-relationship-with-sorting-by-child-cardinality/31272 "2015-09-28T15:51:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkw/32/5053_2.png) [@bkw](https://discuss.elastic.co/u/bkw)\
**Post date:** [September 28, 2015, 3:51pm UTC](https://discuss.elastic.co/t/suggestions-for-modeling-parent-child-relationship-with-sorting-by-child-cardinality/31272/1 "2015-09-28T15:51:44Z")

</div>

Hi,

I'd appreciate suggestions for modeling a simple author-books relation for a particular sorting use case (using elasticsearch 2.0)

I want to retrieve a list of all books (together with author details), grouped by author.  
The list shall be ordered descending by the number of books an author has written.  
So the busiest author's books come up on top and books by authors who have only  
written one book come last.

I want to be able to filter this list by attributes of both author and book.

I had some success modelling this as two types with a parent-child relation  
and a has\_parent filter. This thing that escapes me is how to do the sorting  
based on the cardinality of the child (book).  
Will I have to resort to full denormalization, like including all author fields in the book rows?

I'd prefer to do this in one query, although I'm aware that with the current limitation this may not be achievable. Can one aggregate on inner\_hits and sort upon that? I couldn't figure out, how...

I'm aware that I may be asking a bit too much as ES is not a relational database. I'd still would make my life much easier.  
Any suggestion appreciated.

Cheers,  
bkw

---

<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:** [July 5, 2017, 11:47pm UTC](https://discuss.elastic.co/t/suggestions-for-modeling-parent-child-relationship-with-sorting-by-child-cardinality/31272/2 "2017-07-05T23:47:41Z")

</div>


