elasticsearchsense

ElasticSearch Query for each extension get the sum of its length


I'm looking to find the sum of each extension. The json file contains

Sample code

       {"index":{"_id":"202"}} 
        "length":500000
         "extension":".jpeg"

Output wanted

Key: Jpeg 10000000 Key: docx 20000000

Tried code

        GET /crud_sample2/Customer_Info/_search?extension
          { 
          "aggs": {
          "sum": {
            "terms": {
              "field": "length"
              }
          }
           }
        } 

Output

"key": 500000, "doc_count": 15


Solution

  • Try this instead

    GET /crud_sample2/Customer_Info/_search
    {
      "aggs": {
        "sum": {
          "terms": {
            "field": "extension",
            "order": {"total_length": "desc"}
          },
          "aggs": {
            "total_length": {
              "sum": {
                "field": "length"
              }
            }
          }
        }
      }
    }