UI Development

Consuming Json Facet API

Use case 

Query documents(or records) where ‘manufacturer’ & ‘price’ fields exists but exclude records where price is NOT $O.O

And for each manufacturer

1. Calculate total price of all products from all manufacturers

2. Aggregate prices of all their products (i:e rollup prices of manufacturer’s each product into a single bucket)

3. Sort price buckets in ascending order

Json facet query

Query can be formulated as follows

Above Solr query is a combination of common params q and json.facet elements.

Solr response

How to consume Solr response in Java application?

1.   Create Solr cluster with techproducts collection and start nodes as suggested in posts SolrCloud setup on Mac

2.  Index sample documents that come with techproducts

3.  Run the following Spring boot Junit test

4.  Externalize Solr query, json.facet and any properties to test.properties file.

5. Following is the output of Junit test showing sum of all prices per each manufacturer.

6.  The take away from this Junit test:
NamedList<Object>.findRecursive() allows to consume Solr response (even arbitrarily nested buckets) and analytics such as sum, max, min, avg
7. Ensure SolrJ client lib version matches with Solr version. For example, If using Solr 6.4.1 version, use the same versions of SolrJ​ client that comes with Solr download.

8. Spring boot project with above test is checked in to Git repository at https://github.com/chakrayel/myrepo.git.

About The Author

Leave a Reply