# Scripting aggregation on Object

**URL:** <https://discuss.elastic.co/t/scripting-aggregation-on-object/354905>\
**Category:** Elasticsearch\
**Tags:** aggregations\
**Created:** [March 7, 2024, 4:36am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905 "2024-03-07T04:36:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [March 7, 2024, 4:36am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905/1 "2024-03-07T04:36:11Z")

</div>

Hi,

I have an index with mapping,

```auto
{
"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:

```auto
 "resume":{
    "otherInformation": {
           "industryScores": {
    "accounting_finance" : 0.217337339878561,
    "administrative_management" : 1.05579711565674,
    "agriculture_horticulture" : 0.00866393674966852,
    "airport_aviation" : 0.20383702090859
          }
      }
}

```

doc2,

```auto
 "resume":{
    "otherInformation": {
           "industryScores": {
    "accounting_finance" : 5.355444354681,
    "administrative_management" : 0.5754289121,
    "agriculture_horticulture" : 0.00866393674966852,
    "airport_aviation" : 0.20383702090859
          }
      }
}

```

I want aggregation like,

```auto
"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,

```auto
 "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,

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 7, 2024, 5:38am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905/2 "2024-03-07T05:38:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [March 7, 2024, 5:43am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905/3 "2024-03-07T05:43:09Z")

</div>

Hi @Christian_Dahlqvist

Thanks for the Response.

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

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

```

Can you provide any solution

---

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [March 11, 2024, 7:51am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905/4 "2024-03-11T07:51:26Z")

</div>

Hello,

I am going to find the top industry in a document using script\_field

```auto
"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,

```auto
"_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

---

<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:** [April 8, 2024, 7:52am UTC](https://discuss.elastic.co/t/scripting-aggregation-on-object/354905/5 "2024-04-08T07:52:13Z")

</div>

Closed on April 8, 2024, 7:52am UTC

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