Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 918
Sudeep Avatar asked Sep 06 '11 01:09

Sudeep


1 Answers

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); 
} 
} 
like image 98
nickwesselman Avatar answered Oct 20 '22 04:10

nickwesselman