Would like to use groupby clause in Sitecore 9.1.1 for both search provide (Azure, Solr) in content search API. However, Sitecore does not support this clause. Kindly let us know how we can group result based on common categories so it would work on both (Solr and Azure) search provider.
1 Answer
Categorizing can be achieved with faceting, using the FacetOn
method. Official documentation can be found here.
Simple faceting
var results = queryable.FacetOn(d => d.Name); var facets = results.GetFacets(); foreach (var category in facets.Categories) { Console.WriteLine(category.Name); foreach (var facetValue in category.Values) { Console.WriteLine("{0}: {1}", facetValue.Name, facetValue.Aggregate); } }
With the code above, you can get the facets which are used by your items. facetValue.Name
is the Guid of the items, so based on this you can get the items from Sitecore to show additional data from the item.
-
We would like to show results in group such as Computer category records would show in group (for example 10 records related to computer category show in group and after that other category result would show) . Is this possible through
FacetOn
?– AshuCommented Oct 27, 2020 at 10:05 -
@user3283738 that's not possible with
FacetOn
as far as I know. For that you would need to use the Azure Search SDK and SolrNet and not using the Sitecore Content Search API. Commented Oct 27, 2020 at 11:09 -
@user3283738 I updated my answer. With the trick above should be possible to achieve what you want. Commented Oct 27, 2020 at 11:21
-
I will try faceton approach and let you know. However, I am using content search API so SolrNet and Azure search SDK won't work in my scenario.– AshuCommented Oct 27, 2020 at 12:49
-