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

I could not find a way to 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 the Sitecore.Search API provides these functions. Am I missing something? Can someone explain how to do filtering and sorting using the new Sitecore.Search API? Or do I need to use wrapped Searcher.Search(Query, Sort) to achieve this?

I am using Sitecore 6.5.

Thanks.

+6
source share
1 answer

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); } } 
+13
source

Source: https://habr.com/ru/post/896677/


All Articles