2011-04-23 66 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