Sudeep
Sudeep

Reputation: 349

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.

Upvotes: 6

Views: 2389

Answers (1)

nickwesselman
nickwesselman

Reputation: 6890

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); 
} 
} 

Upvotes: 13

Related Questions