cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Get count for the products when clicked on category from solr.

Former Member
0 Likes
258

Hi Friends,

I want to do a solr search to get the number of products under category. I don't want to use productsearchservice , as it hits the DB to find the count.

Thanks in advance.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Likes

Hi Amardeep

You can use the below function in solrProductSearchService. I don't think it hits the database and if it does, you may need to write your own custom code to fetch solr results without hitting DB.

 public ProductCategorySearchPageData<SolrSearchQueryData, ITEM, CategoryModel> categorySearch(String categoryCode, PageableData pageableData)

Having said that, my understanding is that above method's implementation doesn't hit the DB. Now, the return type of this method is

 FacetSearchPageData<SolrSearchQueryData, SearchResultValueData> searchPageData


Now your entire code would look like below:

 final FacetSearchPageData<SolrSearchQueryData, SearchResultValueData> searchPageData = commerceProductSearchService
                         .categorySearch("xyz", null);
 
                 final List<FacetData<SolrSearchQueryData>> facets = searchPageData.getFacets();
 
                 for (final FacetData<SolrSearchQueryData> facetData : facets)
                 {
                     for (final FacetValueData<SolrSearchQueryData> facetValueData : facetData.getTopValues())
                     {
                         final CategoryData data = new CategoryData();
                         data.setCode(facetValueData.getCode());
                         data.setName(facetValueData.getName());
                         
                         final CategoryResultData categoryResultData = new CategoryResultData();
                         categoryResultData.setCategory(data);
                         categoryResultData.setCount(facetValueData.getCount());
                         
                     }
                 }


Let me know how you go.

Former Member
0 Likes

Thanks, I will check this.