1

I'm trying to use the Sitecore Solr query to retrieve specific facet values for a Sitecore item. The facet is a checklist filter and the field is a tree list type.

var test = context.GetQueryable<SearchResultItem>().Where(x => x.ItemId == item.ID).FacetOn(f => f[key]).GetFacets().Categories;

However, I end up with extra values which are not selected for that particular item.

Can anyone help here?

2 Answers 2

1

If you want to get only values that are selected for the items in your search results, you should pass the second parameter to the .FacetOn() method like this:

.FacetOn(f => f[key], 1)

The second parameter specifies the minimum facet count in the found items to be returned. By default this parameter is 0 and it returns all facet categories stored in the index regardless of your .Where() search query. If you set this parameter to 1, it will only return facets where there is at least one item found for each facet.

0

Try this query

var selectedValues= context.GetQueryable<SearchResultItem>()
  .Where(x => x.ItemId == item.ID)
  .Filter(x => x[key].Contains(item[key]))
  .FacetOn(m => m[key])
  .GetFacets()
  .Categories
  .Where(y=> y.Values.Any(k => k.Name == item[key]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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