# Most efficient sorting on nested values

**URL:** <https://discuss.elastic.co/t/most-efficient-sorting-on-nested-values/162653>\
**Category:** Elasticsearch\
**Created:** [January 2, 2019, 12:44pm UTC](https://discuss.elastic.co/t/most-efficient-sorting-on-nested-values/162653 "2019-01-02T12:44:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scarabas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scarabas/32/39356_2.png) [@scarabas](https://discuss.elastic.co/u/scarabas)\
**Post date:** [January 2, 2019, 12:44pm UTC](https://discuss.elastic.co/t/most-efficient-sorting-on-nested-values/162653/1 "2019-01-02T12:44:53Z")

</div>

I am sorting results from Elasticsearch (5.1.1) based on a calculation of values in nested key/value structures.

The sorting has to:

1. find values from given keys across multiple nested structures
2. multiply those values with one another
3. use this multiplication as a score for sorting

What I have currently is working, but it's really slow/inefficient. I have made a Painless script, due to the calculations in #2 above. What I am doing is:

1. loop through all keys to find their respective matching value
2. for the first match, save the value in a variable; for subsequent matches, multiply saved value with current value, and save that in the aforementioned variable

I think the inefficiency is due to:

1. looping though all nested items (there are a lot per document, and many documents)
2. I am using params['\_source'], which has a reputation of slowing down things. AFAIK, I have to use params['\_source'] to address nested values in Painless

Now to the question: how can I solve this problem more efficiently? Am I going about this the wrong way entirely, or is there a way to not use params['\_source']?

My mapping (the nested structure is "my\_ratios"):

{  
"my\_index": {  
"mappings": {  
"my\_type": {  
"properties": {  
"a\_value": {  
"type": "long"  
},  
"my\_ratios": {  
"type": "nested",  
"properties": {  
"Key": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
}  
},  
"Value": {  
"type": "float"  
}  
}  
}  
}  
}  
}  
}  
}

Example of nested key/value structure:  
{  
{  
"Key": "Key1",  
"Value": 0.4898  
},  
{  
"Key": "Key2",  
"Value": 0.14286  
},  
{  
"Key": "Key3",  
"Value": 6.12245  
},  
...  
}

---

<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:** [January 30, 2019, 12:44pm UTC](https://discuss.elastic.co/t/most-efficient-sorting-on-nested-values/162653/2 "2019-01-30T12:44:58Z")

</div>

Closed on January 30, 2019, 12:44pm UTC

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