# Sorting on Nested data type

**URL:** <https://discuss.elastic.co/t/sorting-on-nested-data-type/85999>\
**Category:** Elasticsearch\
**Created:** [May 16, 2017, 6:32pm UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999 "2017-05-16T18:32:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iluvcode](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iluvcode/32/24929_2.png) [@iluvcode](https://discuss.elastic.co/u/iluvcode)\
**Post date:** [May 16, 2017, 6:32pm UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999/1 "2017-05-16T18:32:59Z")

</div>

I have a nested data type. Before displaying the result, i should sort the results based on menuLevelseq property in Nested data type.

What I am trying to below is, get all the items that belong to menunodeId 742 and sort the records based on menuLevelSeq in nested datatype for menu node id 742. Any help is greatly appreciated.

Query:

GET elasticsearch\_mpwreg/menunodesearchentry/\_search

```
{
	"from": 0,
	"size": 21,
	"sort": [{
		"itemMenuNodes.menuLevelSeq": {
			"order": "asc",
			"nested_path" : "itemMenuNodes",
			"nested_filter" : {
                "term" : { "itemMenuNodes.menuNodeId" : "742" }
             }
		}
	}],
	"query": {
		"bool": {
			"filter": [{
				"bool": {
					"must_not": [{
						"term": {
							"active": {
								"value": 3
							}
						}
					}]
				}
			}, {
				"match": {
					"shownInSearch": {
						"query": true
					}
				}
			}, {
				"term": {
					"itemMenuNodes.menuNodeId": 742
					
				}
			}]
		}
	}
}

```

Index data:

```
{
	"_index": "elasticsearch_mpwreg",
	"_type": "menunodesearchentry",
	"_id": "162488",
	"_score": null,
	"_source": {
		"itemId": 162488,

		"itemMenuNodes": [{
				"menuNodeId": 219,
				"nodeText": "Casual",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					4,
					10,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 742,
				"nodeText": "Baseball Hats & Visors",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					3,
					4,
					6,
					6,
					6,
					6,
					7,
					7,
					7,
					7,
					10,
					11
				],
				"menuLevelSeq": -1
			},
			{
				"menuNodeId": 1369,
				"nodeText": "Macks Prairie Wings",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 2342,
				"nodeText": "MPW Hats & Caps",
				"itemId": 0,
				"menuNodeTextTypeId": [
					3
				],
				"menuLevelSeq": 8
			}
		],
		"hasMoreRecord": false,
		"contentTypeId": 0,
		"itemNo": "MPW111"
	},
	"sort": [-1]
}, {
	"_index": "elasticsearch_mpwreg",
	"_type": "menunodesearchentry",
	"_id": "247990",
	"_score": null,
	"_source": {
		"itemId": 247990,
		"itemMenuNodes": [{
				"menuNodeId": 219,
				"nodeText": "Casual",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					4,
					10,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 742,
				"nodeText": "Baseball Hats & Visors",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					3,
					4,
					6,
					6,
					6,
					6,
					7,
					7,
					7,
					7,
					10,
					11
				],
				"menuLevelSeq": 9
			},
			{
				"menuNodeId": 1369,
				"nodeText": "Macks Prairie Wings",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 2342,
				"nodeText": "MPW Hats & Caps",
				"itemId": 0,
				"menuNodeTextTypeId": [
					3
				],
				"menuLevelSeq": 10
			}
		],
		"id": 247990,
		"hasMoreRecord": false,
		"contentTypeId": 0,
		"itemNo": "MPW111963"
	},
	"sort": [
		0
	]
}
```

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 19, 2017, 6:36am UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999/2 "2017-05-19T06:36:58Z")

</div>

At first sight the query looks good, what is the problem?

---

<div class="post-metadata">

**Author:** ![iluvcode](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iluvcode/32/24929_2.png) [@iluvcode](https://discuss.elastic.co/u/iluvcode)\
**Post date:** [May 19, 2017, 1:17pm UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999/3 "2017-05-19T13:17:07Z")

</div>

@jpountz ,  
itemMenuNodes was not a nested data type in the above query. So I changed the itemMenuNodes to nested data type. Sorting works after changing the itemMenuNodes to nested data type but term query on "itemmenunodes.menunodeid" is not working.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 19, 2017, 1:31pm UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999/4 "2017-05-19T13:31:08Z")

</div>

You need to wrap it under a `nested` query.

---

<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:** [June 16, 2017, 1:31pm UTC](https://discuss.elastic.co/t/sorting-on-nested-data-type/85999/5 "2017-06-16T13:31:14Z")

</div>

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