How to sort/filter using the new Sitecore.Search API

独自空忆成欢 提交于 2019-11-29 15:20:08

问题


I couldn't find any way to do sort and filter using the new Sitecore.Search API. Lucene provides the following methods:

Search(Query query, Filter filter)
Search(Query query, Sort sort)
Search(Query query, Filter filter, Sort sort)

But I don't think Sitecore.Search API exposes these features. Am I missing something? Can someone please explain how to perform Filter and Sort with the new Sitecore.Search API? Or do I need to use the wrapped Searcher.Search(Query, Sort) to achieve this?

I am using Sitecore 6.5.

Thanks.


回答1:


You can extend IndexSearchContext to have methods that accept a Lucene.Net.Search.Sort object. One of my team members figured this out and it's pretty clean.

public class SortableIndexSearchContext : IndexSearchContext 
{ 
public SortableIndexSearchContext(ILuceneIndex index) 
{ 
Initialize(index, true); 
} 
public SearchHits Search(Query query, Sort sort) 
{ 
return Search(query, SearchContext.Empty, sort); 
} 
public SearchHits Search(PreparedQuery query, Sort sort) 
{ 
return new SearchHits(Searcher.Search(query.Query, sort)); 
} 
public SearchHits Search(QueryBase query, Sort sort) 
{ 
return Search(query, SearchContext.Empty, sort); 
} 
public SearchHits Search(string query, Sort sort) 
{ 
return Search(query, SearchContext.Empty, sort); 
} 
public SearchHits Search(Query query, ISearchContext context, Sort sort) 
{ 
return Search(Prepare(query, context), sort); 
} 
public SearchHits Search(QueryBase query, ISearchContext context, Sort sort) 
{ 
return this.Search(Prepare(Translate(query), context), sort); 
} 
public SearchHits Search(string query, ISearchContext context, Sort sort) 
{ 
return this.Search(Parse(query, context), sort); 
} 
} 


来源:https://stackoverflow.com/questions/7314195/how-to-sort-filter-using-the-new-sitecore-search-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!