Accessing non nested fields in a nested query

Hi,

I have a JSON object called listofjsons which looks like this :
"listofjsons": [
{
"Link": "http://dbpedia.org/page/Category:Statistics_Canada",
"score": 2,
"context": "",
"frequency": 1,
"Entity": "Statistics Canada"
},
{
"Link": "http://dbpedia.org/page/Gain",
"score": 3,
"context": "",
"frequency": 1,
"Entity": "Gain"
}
]

and then I have a non nested field called maxFrequency. I have a complex formula to calculate based on matched entities, in which I need to use both score and frequency of every matched entity and also the maxfrequency of the document.

I am using this query :

{
"query": {
"nested": {
"path": "listofjsons",
"score_mode": "total",
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"terms": {
"listofjsons.Entity": [
"Canada","Oil","Gas"
]
}
}
]
}
},
"boost_mode": "replace",
"script_score": {
"script": "0.5doc['listofjsons.score'].valuedoc['listofjsons.frequency'].value/10;"
}
}
}
}
}
}
}
}

How do i access "maxFrequency " inside the nested query.