Hi,
I have an index with mapping,
{
"resume": {
"otherInformation": {
"IndustryScore": {
"accounting_finance": {
"type": "text"
},
"agriculture_horticulture": {
"type": "text"
},
"administrative_management": {
"type": "text"
},
"airport_aviation": {
"type": "text"
}
}
}
}
}
In that I have data like
doc1:
"resume":{
"otherInformation": {
"industryScores": {
"accounting_finance" : 0.217337339878561,
"administrative_management" : 1.05579711565674,
"agriculture_horticulture" : 0.00866393674966852,
"airport_aviation" : 0.20383702090859
}
}
}
doc2,
"resume":{
"otherInformation": {
"industryScores": {
"accounting_finance" : 5.355444354681,
"administrative_management" : 0.5754289121,
"agriculture_horticulture" : 0.00866393674966852,
"airport_aviation" : 0.20383702090859
}
}
}
I want aggregation like,
"aggregations": {
"highest_values": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "accounting_finance",
"doc_count": 1
},
{
"key": "administrative_management",
"doc_count": 1
},
{
"key": "agriculture_horticulture",
"doc_count": 0
},
{
"key": "airport_aviation",
"doc_count": 0
}
]
}
}
so, I wrote one scripting aggregation,
"aggs": {
"highest_values": {
"terms": {
"script": {
"source": """
def result = [];
def counts = [:];
for (def document : params.data) {
if (document['_source'] != null &&
document['_source'].containsKey('resume') && document['_source']['resume'] != null &&
document['_source']['resume'].containsKey('otherInformation') && document['_source']['resume']['otherInformation'] != null &&
document['_source']['resume']['otherInformation'].containsKey('industryScores')) {
def industryScores = document['_source']['resume']['otherInformation']['industryScores'];
def maxValue = 0;
def highestKey = null;
for (def entry : industryScores.entrySet()) {
def value = entry.getValue();
if (value > maxValue) {
maxValue = value;
highestKey = entry.getKey();
}
}
if (highestKey != null) {
counts[highestKey] = (counts[highestKey] ?: 0) + 1;
}
}
}
for (def entry : counts.entrySet()) {
result.add(["key": entry.getKey(), "doc_count": entry.getValue()]);
}
return result;
"""
},
"size": 10000
}
}
}
but, It is not working.. can anybody provide a best query for this
Thanks&Regards,