0

I would like to query in Solr, to pass the list of categoryID('cat001','cat002','cat003','cat004'), based on the categoryID, we need to get the count of Article of passed categoryID in the Solr Query, required output like as shown below image. enter image description here

Is it possible to achieve such type result from solr? If yes please share the approach, how we can achieve the required result.

2
1

If you only need to get a number of articles for each category, then the facets feature is the best way to achieve this. Facets will count matching documents for each category and return only keys and numbers, while grouping will change the search results and return groups instead of a plain list of documents.

Facets are supported by Sitecore Content Search and you can get them by calling FacetOn and GetFacets methods:

queryable.FacetOn(x => x.CategoryID).GetFacets();
0

I am assuming you are using Sitecore 9 version or later.

You can first reference the necessary dlls like SolrNet and Sitecore.ContentSearch.*.dlls and import the name spaces that are required like Sitecore.ContentSearch.SolrNetExtension.

Then you can use the query library to return matching articles that are grouped by category along with the list of grouped articles

Code Snippet (Caution : This is for reference only. Not tested and working Code

using ( var ctx = ContentSearchManager.GetIndex("Sitecore_master_index").CreateSearchContext())
{ 
string query = ""; // search query fro articles
var options= new QueryOptions
            {
                Grouping = new GroupingParameters
                {
                    Fields = new[] { "type_t" } // replace ur category field here
                }
            };

var result = ctx.Query<SearchResultItem>(q,options);

foreach(var grp in result.Grouping)
{ 
   foreach(var group in grp.Value.Groups)
   {
     // get the count for each group
   }
}
}

 
0

Facet is used to aggregate the count of terms in a field to help a user filter their search more and more to get to the specific results they are after.

You can use Facets in Solr which can be used to group categoryID in your case and return the exact number of count of Articles.

using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
{
    var results = context.GetQueryable<SearchResultItem>().FacetOn(f => f.categoryID)).GetFacets();
}

In FacetOn the first property for this method is the index field you wish to facet on.

The second parameter to FacetOn() is optional, however, it is an integer and it’s a min count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.