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

The sorting has to:

- find values from given keys across multiple nested structures
- multiply those values with one another
- 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:

- loop through all keys to find their respective matching value
- 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:

- looping though all nested items (there are a lot per document, and many documents)
- 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

},

...

}