Search code examples
asp.net-mvc-3solrnet

SolrNet faceting asp.net mvc 3


I'm trying to implement faceting on product catelogue app with Solr, SolrNet and its built with asp.net MCV 3. So far I managed to list all the product results but not the faceting. I could print the facets as shown below.

<ul>
    @foreach (var facet in Model.Products.FacetFields["brand"])
    {
      <li>@facet.Key (@facet.Value)</li>
    }
</ul>

I have two issues with above code,

1) If the Search results doesn't contain facets for brand its throwing this error The given key was not present in the dictionary.

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary

2) I need to show facets keys and values as links. So on click of that facet I should be able to list the products of the facet.

Here is the schema.xml, please help me if you know the answers for the above questions.

<field name="product_id" type="string" indexed="true" stored="true" required="true" /> 
<field name="name" type="string" indexed="true" stored="true"/>
<field name="merchant" type="string" indexed="true" stored="true"/>
<field name="merchant_id" type="string" indexed="true" stored="true"/>
<field name="brand" type="string" indexed="true" stored="true"/>
<field name="brand_id" type="string" indexed="true" stored="true"/>
<field name="categories" type="string" multiValued="true" indexed="true" stored="true"/>

Solution

  • 1) If the Search results doesn't contain facets for brand its throwing this error The given key was not present in the dictionary.

    If you're not doing a facet field query on that field, just don't ask for it in the results.

    2) I need to show facets keys and values as links. So on click of that facet I should be able to list the products of the facet.

    Basically you need to convert the clicked facet value into a filter query. There are many ways to implement this depending on your specific application needs. See the SolrNet sample app for one way to do it, use its source code as guidance.