How to get the sum of a column by grouping the number of days in solr

My solr details are as follows:

{ { "total_amount": 100, "created_at": "2015-07-08T18:30:00Z" }, { "total_amount": 200, "created_at": "2015-07-08T18:30:00Z" }, { "total_amount": 300, "created_at": "2015-06-08T18:30:00Z" }, { "total_amount": 400, "created_at": "2015-06-08T18:30:00Z" } } 

I want to get the total_amount amount for the grouping month. So the output answer will be as follows:

 { "2015-07-01T00:00:00Z" : 300, "2015-06-01T00:00:00Z" : 700 } 

I use the query below for this answer, but it only gives me the number of occurrences.

Request and response as follows:

 { "responseHeader": { "status": 0, "QTime": 2, "params": { "facet.date.start": "2015-04-01T00:00:00Z", "facet": "true", "fl": "created_at,total_amount", "q": "campaign_id:1", "facet.date": "created_at", "facet.date.gap": "+1MONTH", "wt": "json", "facet.date.end": "2015-09-01T00:00:00Z" } }, "response": { "numFound": 3, "start": 0, "docs": [ { "total_amount": 100, "created_at": "2015-07-08T18:30:00Z" }, { "total_amount": 100, "created_at": "2015-07-08T18:30:00Z" }, { "total_amount": 100, "created_at": "2015-07-08T18:30:00Z" } ] }, "facet_counts": { "facet_queries": {}, "facet_fields": {}, "facet_dates": { "created_at": { "2015-04-01T00:00:00Z": 0, "2015-05-01T00:00:00Z": 0, "2015-06-01T00:00:00Z": 0, "2015-07-01T00:00:00Z": 3, "2015-08-01T00:00:00Z": 0, "gap": "+1MONTH", "start": "2015-04-01T00:00:00Z", "end": "2015-09-01T00:00:00Z" } }, "facet_ranges": {}, "facet_intervals": {}, "facet_heatmaps": {} } } 
+5
source share
2 answers

Try this query:

 stats=true&stats.field=total_amount&stats.facet=created_at 

This is from the solr-ref-guide .

The Stats component accepts the following parameters:

statistics | If true, then calls the Stats component.

stats.field | Defines the field for which statistics should be generated. This parameter can be called several times in a query in order to request statistics for several fields. (See the example below.)

stats.facet | Returns subselections for values ​​within the specified chamfer.

I tested the request using data from the SOLR_HOME / example / examplesdoc directory after running 'java -jar post.jar * .xml'

That was the result.

  "belkin": {
     "min": 11.5,
     "max": 19.950000762939453,
     "count": 2,
     "missing": 0,
     "sum": 31.450000762939453,
     "sumOfSquares": 530.2525304412848,
     "mean": 15.725000381469727,
     "stddev": 5.975052840505987,
     "facets": {}},
   "maxtor": {
     "min": 350.0,
     "max": 350.0,
     "count": 1,
     "missing": 0,
     "sum": 350.0,
     "sumOfSquares": 122500.0,
     "mean": 350.0,
     "stddev": 0.0,
     "facets": {}},
   "inc": {
     "min": 179.99000549316406,
     "max": 2199.0,
     "count": 5,
     "missing": 0,
     "sum": 3587.8900299072266,
     "sumOfSquares": 5366417.4268503785,
     "mean": 717.5780059814454,
     "stddev": 835.4379769780703,
     "facets": {}}}}}}}}
+3
source

Operations like sum cannot be handled with Groping ...

try http://wiki.apache.org/solr/StatsComponent

+2
source

All Articles