# How to query - sort multiple nested fields in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-query-sort-multiple-nested-fields-in-elasticsearch/252470>\
**Category:** Elasticsearch\
**Created:** [October 19, 2020, 4:18am UTC](https://discuss.elastic.co/t/how-to-query-sort-multiple-nested-fields-in-elasticsearch/252470 "2020-10-19T04:18:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unnikr](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnikr](https://discuss.elastic.co/u/unnikr)\
**Post date:** [October 19, 2020, 4:18am UTC](https://discuss.elastic.co/t/how-to-query-sort-multiple-nested-fields-in-elasticsearch/252470/1 "2020-10-19T04:18:21Z")

</div>

I required multiple sorting in nested fields. I tried below code but it's not working. how can I do a multiple sorting in nested fields?

data is stored like this.

```
indent preformatted text by 4 spaces[
{
	"_index": "test",
	"_type": "data",
	"_id": "AXUz9qiooooGmNN",
	"_score": null,
	"_source": {
		"number_filters": [
			{
				 "masterid": "78772703",
				 "label": "unique_id",
				 "type": "number",
				 "value": [
					{
					   "value": 5831967
					}
				 ]
			},
			{
				 "masterid": "1536368343",
				 "label": "unique_id",
				 "type": "number",
				 "value": [
					{
					   "value": 2315458
					}
				 ]
			}
		]
	}
},
{
	"_index": "test",
	"_type": "data",
	"_id": "AXUz9qiooooG6lN",
	"_score": null,
	"_source": {
		"number_filters": [
			{
				 "masterid": "78772703",
				 "label": "unique_id",
				 "type": "number",
				 "value": [
					{
					   "value": 46332
					}
				 ]
			},
			{
				 "masterid": "1536368343",
				 "label": "unique_id",
				 "type": "number",
				 "value": [
					{
					   "value": 46646
					}
				 ]
			}
		]
	}
}

```

]

this is the code I tried for multiple sort.

```
 "sort" : [
       {
         "number_filters.value.value": {
            "order": "desc",
            "nested_path": "number_filters",
            "nested_filter": {
               "term": {
                  "number_filters.masterid": "78772703"
               }
            }
         }
      },
       {
         "number_filters.value.value": {
            "order": "asc",
            "nested_path": "number_filters",
            "nested_filter": {
               "term": {
                  "number_filters.masterid": "1536368343"
               }
            }
         }
      }
   ]
```

---

<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 16, 2020, 4:18am UTC](https://discuss.elastic.co/t/how-to-query-sort-multiple-nested-fields-in-elasticsearch/252470/2 "2020-11-16T04:18:23Z")

</div>

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