Search code examples
solrsolrnet

SolrNet facets are being returned from Solr but not through the SolrNet client


I am using this code to query Solr and I can see facets are being returned from Solr but for some reason they are not passed through.

public class HomeController : Controller
{
    private readonly ISolrReadOnlyOperations<Product> _solr;

    public HomeController(ISolrReadOnlyOperations<Product> solr)
    {
        _solr = solr;
    }

    public ActionResult Index()
    {
        var queryOptions = new QueryOptions()
        {
            Rows = 5,
            Facet = new FacetParameters
            {
                Queries = new[] { new SolrFacetFieldQuery("brand") }
            }
        };

        SolrQueryByField query = new SolrQueryByField("category", "phones-tablets/mobile-phones");
        SolrQueryResults<Product> results = _solr.Query(query, queryOptions);

        return View();
    }

}

The above code ends up generating this url http://localhost:8983/solr/new_core/select?q=category%3a(phones%5c-tablets%5c%2fmobile%5c-phones)&rows=5&facet=true&facet.field=brand&version=2.2&wt=xml

When I paste the URL I can see the facets section as expected. But results.FacetQueries.Count is zero. Am I missing something?


Solution

  • FacetQueries is used for returning the result of explicit facet queries. You're performing regular faceting. That result can be accessed through results.FacetFields. From the documentation:

    var r = solr.Query(...);
    
    foreach (var facet in r.FacetFields["category"]) {
      Console.WriteLine("{0}: {1}", facet.Key, facet.Value);
    }