Warm tip: This article is reproduced from stackoverflow.com, please click
apache java solr solrj

how compute sum of value for a field using intervals of different field in solr?

发布于 2020-03-27 10:18:57

I have multiple solr documents in the following form

{ requests : [23], timestamp : [1562146437780] }

i have to get sum of requests in intervals of timestamp.

I tried using facets but couldn't figure out how to use on two fields

i did try this. but was giving me count of documents in the timestamp interval.

 query.addNumericRangeFacet(TIMESTAMP, startTime, endTime, gap);

Thanks in advance

Questioner
The Dude
Viewed
138
MatsLindh 2019-07-03 21:43

You can implement this by using the JSON facet API for range facets. These will allow you to give a sub facet that works with the domain (i.e. the documents in the bucket) from the facet above.

You start by creating a range facet:

RangeFacetMap rangeFacet = new RangeFacetMap(TIMESTAMP, startTime, endTime, gap);

Then you add a sub facet that invokes a sum:

rangeFacet.withStatSubFacet("requests_sum", "sum(requests)")

You send this request to Solr by using:

final JsonQueryRequest request = new JsonQueryRequest()
    .setQuery("*:*")
    .withFacet("timestamps", rangeFacet);
QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);

I don't have an instance or documents to test with right now, but this is built from the information under Nester Facet Example in the reference guide.