# Sorting on distance from parent in has\_parent query

**URL:** <https://discuss.elastic.co/t/sorting-on-distance-from-parent-in-has-parent-query/91574>\
**Category:** Elasticsearch\
**Created:** [July 2, 2017, 11:57pm UTC](https://discuss.elastic.co/t/sorting-on-distance-from-parent-in-has-parent-query/91574 "2017-07-02T23:57:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pieterdt](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pieterdt](https://discuss.elastic.co/u/pieterdt)\
**Post date:** [July 2, 2017, 11:57pm UTC](https://discuss.elastic.co/t/sorting-on-distance-from-parent-in-has-parent-query/91574/1 "2017-07-02T23:57:24Z")

</div>

Hi all,

I've been searching quite a lot but, so far without results, so I'm turning to you for some pointers.

I have 2 types of docs in my index. Rooms and Premises. Rooms have a premises as parent. The Premises (parents) have a location.

Now I want to create a query that shows the rooms matching certain critiria. One of the criteria is the distance from a certain location point.

So far so good, that works (has\_parent query)

Now I want the results to be sorted based on that distance. But as the distance is in the parent, this doesn't seem to be very straight forward. I've been trying to mesh suggestions for sorting based on _a_ parent field with suggestions for sorting on distance. But I didn't get anything to go for.

Are there any good pages out there that I didn't find? Could you jot down the idea that I should implement? I'm fairly new in ES.

Thx for any hints

Kind regards

Pieter

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [July 4, 2017, 1:43pm UTC](https://discuss.elastic.co/t/sorting-on-distance-from-parent-in-has-parent-query/91574/2 "2017-07-04T13:43:28Z")

</div>

ES doesn't support sorting by a field inside a parent or child field. There is a workaround for sorting by parent or child field but it has limitations: [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-has-child-query.html#\_sorting](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-has-child-query.html#_sorting)

---

<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:** [August 1, 2017, 1:43pm UTC](https://discuss.elastic.co/t/sorting-on-distance-from-parent-in-has-parent-query/91574/3 "2017-08-01T13:43:33Z")

</div>

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