You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Jamshaid Ashraf <ja...@gmail.com> on 2013/07/11 13:56:25 UTC

Applying Sum on Field

Hi,

I'm a new solr user, I wanted to know is there any way to apply sum on a
field in a result document of group query?

Following is the query and its result set, I wanted to apply sum on 'price'
filed grouping on type:


*Sample input:*

<doc>
<str name="id">3</str>
<str name="type">Caffe</str>
<str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
North Beach Learning text analysis with SolrInAction by Manning on my
iPad</str>
<long name="_version_">1440257540658036736</long>
<int name="price">250</int>
</doc>
<doc>
<str name="id">1</str>
<str name="type">Caffe</str>
<str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
North Beach Learning text analysis with SolrInAction by Manning on my
iPad</str>
<long name="_version_">1440257592044552192</long>
<int name="price">100</int>
</doc>
*
*
*Query:*
http://localhost:8080/solr/collection2/select?q=caffe&df=content&group=true&group.field=type

your help will be greatly appreciated!

Regards,
Jamshaid

Re: Applying Sum on Field

Posted by Jack Krupansky <ja...@basetechnology.com>.
Take a look at the "stats" component that calculates aggregate values. It 
has a "facet" parameter that may or may not give you something similar to 
what you want. Or, just form a query that matches the results of the group, 
and then get the stats.

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

-- Jack Krupansky

-----Original Message----- 
From: Jamshaid Ashraf
Sent: Thursday, July 11, 2013 7:56 AM
To: solr-user@lucene.apache.org
Subject: Applying Sum on Field

Hi,

I'm a new solr user, I wanted to know is there any way to apply sum on a
field in a result document of group query?

Following is the query and its result set, I wanted to apply sum on 'price'
filed grouping on type:


*Sample input:*

<doc>
<str name="id">3</str>
<str name="type">Caffe</str>
<str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
North Beach Learning text analysis with SolrInAction by Manning on my
iPad</str>
<long name="_version_">1440257540658036736</long>
<int name="price">250</int>
</doc>
<doc>
<str name="id">1</str>
<str name="type">Caffe</str>
<str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
North Beach Learning text analysis with SolrInAction by Manning on my
iPad</str>
<long name="_version_">1440257592044552192</long>
<int name="price">100</int>
</doc>
*
*
*Query:*
http://localhost:8080/solr/collection2/select?q=caffe&df=content&group=true&group.field=type

your help will be greatly appreciated!

Regards,
Jamshaid 


Re: Applying Sum on Field

Posted by Peter Sturge <pe...@gmail.com>.
Hi,

If you mean adding up numeric values stored in fields - no, Solr doesn't do
this by default.
We had a similar requirement for this, and created a custom SearchComponent
to handle sum, average, stats etc.
There are a number of things you need to bear in mind, such as:
  * Handling errors when a query asks for sums on fields that are
non-numeric
  * Performance issues - e.g. are you willing to wait to add up 50 million
fields of stringified numbers
  * How to return result payloads in a client-friendly way
  * Be prepared to coalesce results from multi-shard/distributed queries.
It's not trivial, but it is do-able.

Peter




On Thu, Jul 11, 2013 at 12:56 PM, Jamshaid Ashraf <ja...@gmail.com>wrote:

> Hi,
>
> I'm a new solr user, I wanted to know is there any way to apply sum on a
> field in a result document of group query?
>
> Following is the query and its result set, I wanted to apply sum on 'price'
> filed grouping on type:
>
>
> *Sample input:*
>
> <doc>
> <str name="id">3</str>
> <str name="type">Caffe</str>
> <str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
> North Beach Learning text analysis with SolrInAction by Manning on my
> iPad</str>
> <long name="_version_">1440257540658036736</long>
> <int name="price">250</int>
> </doc>
> <doc>
> <str name="id">1</str>
> <str name="type">Caffe</str>
> <str name="content">Yummm  Drinking a latte at Caffe Grecco in SF shistoric
> North Beach Learning text analysis with SolrInAction by Manning on my
> iPad</str>
> <long name="_version_">1440257592044552192</long>
> <int name="price">100</int>
> </doc>
> *
> *
> *Query:*
>
> http://localhost:8080/solr/collection2/select?q=caffe&df=content&group=true&group.field=type
>
> your help will be greatly appreciated!
>
> Regards,
> Jamshaid
>