# Is it possible to do nested sort with a conditional query on a sort element

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-do-nested-sort-with-a-conditional-query-on-a-sort-element/59456>\
**Category:** Elasticsearch\
**Created:** [August 31, 2016, 6:16pm UTC](https://discuss.elastic.co/t/is-it-possible-to-do-nested-sort-with-a-conditional-query-on-a-sort-element/59456 "2016-08-31T18:16:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikasdp](https://avatars.discourse-cdn.com/v4/letter/v/f07891/32.png) [@vikasdp](https://discuss.elastic.co/u/vikasdp)\
**Post date:** [August 31, 2016, 6:16pm UTC](https://discuss.elastic.co/t/is-it-possible-to-do-nested-sort-with-a-conditional-query-on-a-sort-element/59456/1 "2016-08-31T18:16:06Z")

</div>

we have following structure in an index - following is only a partial and doc relevant for this question.

"instance" : {  
"id" : 1,  
{"instFields": [  
{  
"sourceFieldId": 2684,  
"fieldValue": "false",  
"fieldBoolean": false  
},  
{  
"sourceFieldId": 1736,  
"fieldValue": "DODGE",  
"fieldString": "DODGE"  
},

{  
"sourceFieldId": 1560,  
"fieldValue": "GRAY",  
"fieldString": "GRAY"  
},  
{  
"sourceFieldId": 1558,  
"fieldValue": "CHALLENGER",  
"fieldString": "CHALLENGER"  
},  
{  
"sourceFieldId": 1556,  
"fieldValue": "2010",  
"fieldDouble": 2010  
}

]  
}  
first user query is give me all instances where sourceFieldId=1736 - this returns all the DODGE instances[] - all this is working fine with an appripriate Elastic Search query. now when user is seeing all DODGE records - user wants to sort by any of those sourceFieldIds for e.g. say user is wanting to sort results by - color - sourceFieldId=1560.

say we have following sort query

"sort": {  
"instance.instFields.fieldString.raw": {  
"order": "asc",  
"nested\_path": "instance.instFields",  
....  
}  
with following part of the sort query - note sort query is properly prepared - only pasting parts here which are relevant for the question

{  
"nested": {  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"instance.instFields.sourceFieldId": "1736"  
}  
},  
{  
"term": {  
"instance.instFields.fieldString.raw": "dodge"  
}  
}  
]  
}  
}  
}  
},  
"path": "instance.instFields"  
}  
}  
resulting docs must return entire instance with all the soureceFields - as on a user page it displays other values of DODGE as well.

now issue is- sort query still has to have knowledge to sort where - "sourceFieldId": 1560 (which is a sourceFieldId for color) to sort on color

is there a way to achieve such a sort query in ES without using dynamic scripting/dynamic templating? something like

"sort": {  
"instance.instFields.fieldString.raw": (where sourceFieldId=1560?)

Apologies for [crossposting](https://stackoverflow.com/questions/39256231/is-it-possible-to-do-nested-sort-with-a-conditional-query-on-a-sort-element) but I have seen ES questions on SO get less attention than here.

---

<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 5, 2017, 10:23pm UTC](https://discuss.elastic.co/t/is-it-possible-to-do-nested-sort-with-a-conditional-query-on-a-sort-element/59456/2 "2017-07-05T22:23:30Z")

</div>


