# Alphabetical sorting on children

**URL:** https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377
**Category:** Elasticsearch
**Created:** [October 26, 2017, 9:07am UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377 "2017-10-26T09:07:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nino\_aloi](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nino\_aloi](https://discuss.elastic.co/u/nino_aloi)
#### Post date: [October 26, 2017, 9:07am UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377/1 "2017-10-26T09:07:14Z")

</div>

Hi,

I currently need to run some queries on my company dataset stored in elastic. A simplified version of the mapping in place is the following:

```auto
Mapping for Child
{
	"_parent": {
		"type": "parent"
	},
	"properties": {
		"childId": {
			"type": "long"
		},
		"term": {
			"type": "text",
			"fields": {
				"raw": { 
					"type": "keyword"
				}
			}
		},
		"integerValue": {
			"type": "integer"
		},
		"doubleValue": {
			"type": "double"
		},
		"boolValue": {
			"type": "boolean"
		},
		"dateTimeValue": {
			"type": "date"
		}
	}
}

Mapping for Parent
{
	"properties": {
		"parentId": {
			"type": "long"
		},
		"title": {
			"type": "text",
			"fields": {
				"raw": { 
					"type": "keyword"
				}
			}
		},
		"createdDate": {
			"type": "date"
		},
		"createdBy": {
			"type": "integer"
		},
		"geometry": {
			"type": "geo_shape"
		}
	}
}

```

I am providing the company with a custom built query mechanism to query this dataset by using an abstract company syntax that reflects the structure we have in place. The problem I am facing at the moment is related to sorting and originated two questions.

1. How do I sort alphabetically on a child attribute in an "has\_child" query? In this case how would I sort on the term.raw field of a child type document in an "has\_child" query executed on the parent?

2. Assuming I am now able to sort alphabetically on the child attribute in a "has\_child" query, how do I sort in a single query, on multiple fields, BOTH from child and parent while also applying some conditions?

For example, let's say I wanted to retrieve all parents (we always want to retrieve the parents, never the children, children are only used for querying) with createdBy = 123 that also have children with doubleValue = 45.4. All fine up to here, however I'd also like, whichever the results are, for them to be sorted alphabetically on the term.raw field from a child and on the createdDate field. Priority to the term.raw field.

Is it even possible to achieve this? I looked into function\_score and it looked powerful but I don't see how to use it with text/keyword fields and how to use it on parent and children at the same time. Am I wandering into a dangerous place no innocent soul should go to?

Thanks in advance for the help!!!

---

<div class="post-metadata">

### Author: ![nino\_aloi](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nino\_aloi](https://discuss.elastic.co/u/nino_aloi)
#### Post date: [October 27, 2017, 8:52am UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377/2 "2017-10-27T08:52:01Z")

</div>

Is there some more information I should put in? Is the question badly worded? Also an answer to point 1 alone would be greatly helpful thankssssssssssss 😃

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [October 28, 2017, 12:55am UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377/3 "2017-10-28T00:55:34Z")

</div>

Did you check docs [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-has-child-query.html#\_sorting](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-has-child-query.html#_sorting) ? Basically, you only have `_score`, which means that unless you can somehow convert the children's text field to a number, there is not much you can do there.

---

<div class="post-metadata">

### Author: ![nino\_aloi](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nino\_aloi](https://discuss.elastic.co/u/nino_aloi)
#### Post date: [November 1, 2017, 3:06pm UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377/4 "2017-11-01T15:06:39Z")

</div>

Thank you very much for the answer!!!!!! I guess we are either pushing the system to a limit here or using it incorrectly altogether 😅

---

<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: [November 29, 2017, 3:12pm UTC](https://discuss.elastic.co/t/alphabetical-sorting-on-children/105377/5 "2017-11-29T15:12:57Z")

</div>

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