2016-02-29 159 views
1

我有一個IQueryable<T>對象作爲搜索結果對象。 我對這個搜索對象應用過濾和排序。訂購Sitecore搜索結果

在我打電話給GetResults()之前,我想根據該字段的一個字段(字段名稱 - Priority)值排序結果。因此,對於IQueryable<T>對象中的所有項目,我想按優先級字段對它們進行排序,因此所有具有該字段值的項目都保留在頂部,剩下的項目位於底部。

我有fieldmap條目的優先級字段。

search.OrderByDescending(i => !string.IsNullOrEmpty(i.GetItem().GetFieldValue("Priority"))) 

上述命令不起作用。顯然,我不能使用IQueryable的Sitecore擴展方法?如果我轉換search.ToList()。做排序,然後將其轉換回AsQueryable(),我得到以下錯誤:

There is no method 'GetResults' on type 'Sitecore.ContentSearch.Linq.QueryableExtensions' 
that matches the specified arguments 

是否有一個整潔,快捷的方式來解決這個問題?

乾杯

+0

'我對這個搜索對象應用過濾和排序' - 你是通過多個字段排序的嗎?例如標題和優先權? – jammykam

+0

@jammykam - 是的。我首先根據用戶選擇的標準對它們進行排序 - 如名稱,創建日期等。一旦我得到結果,我需要按優先級字段排序。 – NomadTraveler

+0

您是分頁結果還是可以在獲得結果後訂購? –

回答

0

我想你只需要你的字段添加到您的SearchResultItem並將其標記爲一個int。我正在假設該字段是一個整數。製作一個繼承SearchResultItem的自定義類。

public class CustomSearchResultItem : SearchResultItem 
{ 
    [IndexField("Priority")] 
    public int Priority { get; set; } 
} 

然後在您的搜索中使用它。最後點它。

using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext()) 
{ 
    var results = context.GetQueryable<CustomSearchResultItem>().Where(prod => prod.Content.Contains("search box text").OrderByDescending(t => t.Priority); 
} 

在此處找到了一些數據。

http://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/10/sorting-and-ordering-results.aspx

0

您可以通過使用OrderByDescendingThenByDescending聯合使用多個字段搜索結果進行排序。因此,您需要先按優先順序排序,然後按[名稱|日期|隨時]排序。

I want to order them desc by Priority field, so all the items which has a value for that field stay at the top and the rest are at the bottom.

I sort them first on the criteria chosen by the user - like Name, Date created etc. Once I get the results back, I need to order them by priority field

你在自己的問題和意見中存在矛盾。如果你想優先的結果,然後再由用戶選擇的結果的話,下面的工作:

query = dataQuery.OrderByDescending(i => i.Title).ThenByDescending(i => i["Priority"]); 
var results = query.GetResults().Hits.Select(h => h.Document); 

有在Sitecore的早期版本,這意味着該ThenBy條款將OrderBy句之前,因此加入一個bug它在上面反向添加。您可能需要檢查這是否在當前版本中修復。如果是這樣簡單地改變你的查詢:

query = dataQuery.OrderByDescending(i => i["Priority"]).ThenByDescending(i => i.Title); 

您沒有到外地添加到您的SearchResultItem如果你只是想通過它來訂購,只有當你需要該字段的實際值返回以及。

如果您需要通過自定義用戶提供的值進行訂購,那麼您可以通過i => i["whatever-field-the-user-has-selected"]而不是i.Title

你可以在this blog post找到更多的信息。

+0

對不起,如果我的問題似乎令人困惑。我將重新迭代。我首先根據用戶選擇的標準對它們進行排序,然後在優先領域對它們進行排序。我無法使用Priority字段進行排序。我得到的錯誤是'只能在'Sitecore.ContentSearch.Linq.Nodes.FieldNode'上完成訂購。' – NomadTraveler

+0

您是否更新了搜索配置以同時爲優先級字段編制索引? – jammykam

+0

該字段已映射。 – NomadTraveler