2011-04-23 68 views
1

我试图在产品catelogue应用程序与Solr,SolrNet和它的内置asp.net MCV 3实现faceting。到目前为止,我设法列出所有的产品结果,但不包括faceting。我可以如下所示打印各个面。SolrNet faceting asp.net mvc 3

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

我有上面的代码中的两个问题,

1)如果搜索结果中不包含品牌方面它抛出这个错误 给定的关键是不存在的字典。

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

2)我需要显示方面的键和值作为链接。所以点击这个方面我应该能够列出方面的产品。

这是schema.xml,请帮助我,如果你知道上述问题的答案。

<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"/> 

回答

1

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

如果你不这样做对领域的facet field query,只是不结果自讨苦吃。

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.

基本上你需要点击的小数值转换为filter query。 有很多方式来实现这取决于您的具体应用需求。请参阅SolrNet sample app以了解其中的一种方法,请使用其源代码作为指导。

+0

嗨莫里乔,谢谢。 - 实际上,我通过遵循示例应用程序代码创建了我的项目。 1)在我的控制器中,我有这个AllFacetFields = new [] {“merchant”,“brand”,“categories”}'。在示例应用程序上的所有方面都一次打印,但在我的情况下,我需要打印方面condinally。比如,当我列出产品时,我只需要展示品牌的方面。这就是为什么我没有使用它在示例应用程序中完成的方式。正如我看到的,当结果集中没有“品牌”时,我会遇到这种错误。 – randika 2011-04-24 09:36:33

+0

续... 2)我试图打印这样的链接: '

    @foreach(VAR面在Model.Products.FacetFields [ “品牌”]){
  • @ Url.SetFacet(”品牌”,facet.Key)(@ facet.Value)
  • }
' 但我得到这个错误: ' 'System.Web.Mvc.UrlHelper' 有一个名为没有适用的方法 'SetFacet',但似乎有这个名字的扩展方法。扩展方法不能动态分派。考虑铸造动态参数或调用扩展方法,而不使用扩展方法语法。' – randika 2011-04-24 11:04:08

+0

**更新:** 我试过了,它不适用于问题(1) '@if(Model.Products。 FacetFields [ “商家”]。计数> 0){

    @foreach(VAR面在Model.Products.FacetFields [ “商家”]) {
  • @ facet.Key(@ facet.Value)
  • }
}' – randika 2011-04-24 14:01:10