简体   繁体   中英

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

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

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM