# Sorting on nested field with conditon

**URL:** <https://discuss.elastic.co/t/sorting-on-nested-field-with-conditon/118689>\
**Category:** Elasticsearch\
**Created:** [February 6, 2018, 5:10pm UTC](https://discuss.elastic.co/t/sorting-on-nested-field-with-conditon/118689 "2018-02-06T17:10:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsebrien](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsebrien/32/27428_2.png) [@jsebrien](https://discuss.elastic.co/u/jsebrien)\
**Post date:** [February 6, 2018, 5:10pm UTC](https://discuss.elastic.co/t/sorting-on-nested-field-with-conditon/118689/1 "2018-02-06T17:10:03Z")

</div>

Hello,

I have the following document:

{   
"mappings" : {  
"doc":{  
"properties":{  
...  
"notations": {  
"type": "nested"  
}  
}  
}  
}  
}

In "notations", I have elements like:

{  
"key":"default"  
"grade":5  
},

{  
"key":"custom"  
"grade":8  
}

Is it possible to sort with field notations.grade with the following condition:

if (exist notation.key=="custom") take the corresponding notation.grade  
else if (exist notation.key=="default") take the corresponding notation.grade

?

I saw the documentation describing nested sorting examples here:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)

but it does not tell if we can add an "else if" condition.

Is there another way to implement that behavior?

Regards,

Julien

---

<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:** [March 6, 2018, 5:10pm UTC](https://discuss.elastic.co/t/sorting-on-nested-field-with-conditon/118689/2 "2018-03-06T17:10:15Z")

</div>

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