Scripting aggregation on Object

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,

What is the rationale behind mapping these numeric fields as text?

Hi @Christian_Dahlqvist

Thanks for the Response.

Sorry.. I mentioned wrong mapping here. Actually it is in "float"

{
"resume": {
        "otherInformation": {
            "IndustryScore": {
                "accounting_finance": {
                    "type": "float"
                },
                "agriculture_horticulture": {
                    "type": "float"
                },
                "administrative_management": {
                    "type": "float"
                },
                "airport_aviation": {
                    "type": "float"
                }
            }
        }
    }
}

Can you provide any solution

Hello,

I am going to find the top industry in a document using script_field

"script_fields": {
    "max_value": {
      "script": {
        "source": """
          def max_value = 0.0;
          def max_field = "";
          
          def accounting_finance = doc['resume.otherInformation.industryScores.accounting_finance'].value;
          def agriculture_horticulture = doc['resume.otherInformation.industryScores.agriculture_horticulture'].value;
          def administrative_management = doc['resume.otherInformation.industryScores.administrative_management'].value;
          def airport_aviation = doc['resume.otherInformation.industryScores.airport_aviation'].value;

          if (accounting_finance > max_value) {
            max_value = accounting_finance;
            max_field = "accounting_finance";
          }
          if (administrative_management > max_value) {
            max_value = administrative_management;
            max_field = "administrative_management";
          }
          if (agriculture_horticulture > max_value) {
            max_value = agriculture_horticulture;
            max_field = "agriculture_horticulture";
          }
          if (airport_aviation > max_value) {
            max_value = airport_aviation;
            max_field = "airport_aviation";
          }
         
          
          return [max_field: max_value];
        """
      }
    }
  }

Now I got like,

"_source": {
          "resume": {
            "otherInformation": {
              "industryScores": {
                "accounting_finance": 0.13279595229499055,
                "administrative_management": 0.4282609209875825,
                "agriculture_horticulture": 0.29687788329689085,
                "airport_aviation": 0.10409434976829371
              }
            }
          }
        },
        "fields": {
          "max_value": [
            {
              "administrative_management":  0.4282609209875825
            }
          ]
        }

Now, Anybody tell me how can I perform aggregation on fields data.. or tell me how to perform any query on fields data.

Thanks&Regards

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