Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My solr data 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 sum of total_amount by grouping month. So the output response will be as follows:

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

I'm using below query for given response but its giving me only number of occurrences.

Query 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": {}
    }

}
like image 358
Mukesh Jeengar Avatar asked Jul 15 '15 12:07

Mukesh Jeengar


2 Answers

Try this query:

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

This is from solr-ref-guide.

The Stats Component accepts the following parameters:

stats | If true, then invokes the Stats component.

stats.field | Specifies a field for which statistics should be generated. This parameter may be invoked multiple times in a query in order to request statistics on multiple fields. (See the example below.)

stats.facet | Returns sub-results for values within the specified facet.

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

This was some of the result.

  • Query run:http://localhost:8983/solr/collection1/select?q=*:*&wt=json&indent=true&stats=true&stats.field=price&stats.facet=manu
 "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":{}}}}}}}}
like image 81
Jihwan Avatar answered Oct 10 '22 20:10

Jihwan


The operations like sum cannot be worked out with Groping...

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

like image 45
Abhijit Bashetti Avatar answered Oct 10 '22 21:10

Abhijit Bashetti