6

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/

shruti1810
  • 3,920
  • 2
  • 16
  • 28
Mukesh Jeengar
  • 762
  • 9
  • 20

1 Answers1

12

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.

Bruno dos Santos
  • 1,361
  • 1
  • 12
  • 21