0

I was tasked to create a facet component for our solution on top of an already existing search functionality - which is an API controller.

The code snippet I added is the use of .FacetOn(x => x[key]) and .GetResults() but this returns an empty set:

//queryResults is an IQueryable<SearchResultItem>
//searchparams is the JSON request body

//fl is the list of facet titles from JSON request body
//e.g. "Tag List","Action"
var facets = searchparams.fs.Split(','); //["Tag List","Action"]

//Converts facet titles to index keys (info stored as item in Sitecore)
//["tag_list_sm","action_sm"]
var keyList = this.GetFacetKeys(facets);

foreach (var key in keyList)
{
 queryResults.FacetOn(x => x[key] != null);
 //also tried
 //queryResults.FacetOn(x => x[key]);
}

var results = queryResults.GetResults(); //shows result items, but no facets

The tags are stored as a TreeList on the target pages, and are filled up.

Am I missing something? Do I have to add something in the defaultSolrIndexConfiguration node of Sitecore? I also tried creating a model that contains the fields that I need but it still won't show.

public class SearchPage: SearchResultItem
    {
        [IndexField("action_sm")]
        public string Action{ get; set; }

        [IndexField("tag_list_sm")]
        public string TagList{ get; set; }
    }
3
  • 1
    In addition to Leonardo's answer, it can be helpful to inspect the Search log (in your data\logs folder) and view the query that is being sent to Solr. Alternatively, with a short google search you can figure out how to view your query in Fiddler. This can tell you exactly why you are returning zero results and is a very valuable tool for a Sitecore dev working with Solr.
    – jrap
    Dec 17, 2021 at 18:27
  • 1
    @jrap is right. There may be two queries: one for .GetResults() and another for .GetFacets(). You can copy them and run them on your local Solr instance: localhost:8984/solr/sitecore_master_index/select?q=*%3A* Dec 17, 2021 at 18:46
  • @LeonardoFaggiani I've read the search logs and I found that parameters facet=on and facet.fields are not included in the query. How is that possible? @_@
    – Lala
    Dec 20, 2021 at 5:12

2 Answers 2

1

You should be able to get facets with:

queryResults.GetFacets()

Additionally, FacetOn has a parameter called minimumResultCount that you can use for pulling only facets with at least 1 result. For instance:

queryResults.FacetsOn(x => x.TagList, 1)
0

It turns out I missed out the part that I need to reassign the variable with the new IQueryable:

foreach (var key in keyList)
{
 queryResults = queryResults.FacetOn(x => x[key]);
}

Reading through the logs do indeed help as it allowed me to know that facet=on is not included in the executed SOLR query, even more so my index fields. This allowed me to get more clues as to why it is returning an empty set. Check your logs!

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.