# Sorting on Parent/Child attributes

**URL:** <https://discuss.elastic.co/t/sorting-on-parent-child-attributes/18607>\
**Category:** Elasticsearch\
**Created:** [July 11, 2014, 5:34pm UTC](https://discuss.elastic.co/t/sorting-on-parent-child-attributes/18607 "2014-07-11T17:34:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Rook](https://avatars.discourse-cdn.com/v4/letter/b/f0a364/32.png) [@Brian\_Rook](https://discuss.elastic.co/u/Brian_Rook)\
**Post date:** [July 11, 2014, 5:34pm UTC](https://discuss.elastic.co/t/sorting-on-parent-child-attributes/18607/1 "2014-07-11T17:34:25Z")

</div>

Hello,

I'm looking for a solution to a problem I am having. Lets say I have 2  
types Person and Pet in an index called customers.  
Person  
-account  
-firstname  
-lastname  
-SSN

Pet  
-name  
-type  
-id  
-account

I would like to query/filter on fields in both person and pet in order to  
retrieve people and their associated pet. Additionally, I need to sort on  
a field that could be in either person or pet.

For example, retrieve all people/pets that have wildcard person.firstname  
'_ave_' and pet.type wildcard '_terrier_' and sort on pet.name. Or  
wildcard search on person.SSN = '_55_' and pet.name='_mister_' and sort on  
person.lastname.

I currently have a solution where I search/sort on people or pet based on  
the sort that I am using. I use a hasChild/hasParent to manage the fields  
that are on the 'other' type. Then I use an id field to retrieve the  
entities of the other type. So, if I have a sort on personfirstname, I  
query on person and child (pet) and sort on person.firstname, then use the  
accounts to retrieve the pets (by account) in another query. This is not  
ideal because it is ugly and I suspect difficult to maintain if this  
query's requirements change in the future.

I suspect that I can do a query at the 'customers' level and do 'type'  
queries on the fields that I need for person and pet. Similar to this:  
[http://joelabrahamsson.com/grouping-in-elasticsearch-using-child-documents/](http://joelabrahamsson.com/grouping-in-elasticsearch-using-child-documents/)

However, I'm not sure how I would implement the sort. I suspect that I  
could use a custom scoring script, but I am not sure how I would score text  
fields.

Any thoughts?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/53e9d304-d1e3-4994-b95f-3e5f0052ec96%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/53e9d304-d1e3-4994-b95f-3e5f0052ec96%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:16am UTC](https://discuss.elastic.co/t/sorting-on-parent-child-attributes/18607/2 "2017-07-06T01:16:27Z")

</div>


