Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get sum of a field in solr 4.8

This is my response data:

"response": {
    "numFound": 2,
    "start": 0,
    "docs": [
    {
      "total_amount": 10,
      "id": "2"
    },
    {
      "total_amount": 10,
      "id": "1"
    }
  ]
}

I want to get sum of total_amount. I tried facet query also. But I did't get sum. I got some blog on this but that is for solr 5.1. http://yonik.com/solr-facet-functions/

like image 737
Mukesh Jeengar Avatar asked May 26 '15 07:05

Mukesh Jeengar


1 Answers

You can use the stats functionality to get this information. Just put the followed parameters in your query:

stats=true&stats.field=total_amount

Your response will be like that:

"response": {
    "numFound": 2,
    "start": 0,
    "docs": [
      {
        "id": "1",
        "total_amount": 15
      },
      {
        "id": "2",
        "total_amount": 12
      }
    ]
  },
  "stats": {
    "stats_fields": {
      "total_amount": {
        "min": 12,
        "max": 15,
        "count": 2,
        "missing": 0,
        "sum": 27,
        "sumOfSquares": 369,
        "mean": 13.5,
        "stddev": 2.1213203435596424,
        "facets": {}
      }
    }

Note that you have lots of information around the total_amount field including the sum.

like image 193
Bruno dos Santos Avatar answered Nov 17 '22 12:11

Bruno dos Santos