I have a custom index which has additional teaser title, teaser description, main title and main content fields and the index is resolved based on Sitecore.Context.Database
. Eg. master
database context will resolve it to myproject_page_master_index
while web
database will resolve it to myproject_page_web_index
.
I'm using Sitecore 8.2 latest version
I have following Repository:
public class PageSearchRepository : IPageSearchRepository
{
private readonly ISearchIndex _index;
public PageSearchRepository(ISearchIndex index)
{
_index = index;
}
public PageSearchResults FindByKeywords(string[] keywords, int take, int skip)
{
using (IProviderSearchContext context = this._index.CreateSearchContext())
{
var baseQuery = PredicateBuilder.True<PageSearchResultItem>()
.And(item => item.LatestVersion)
.And(item => item.Language == Sitecore.Context.Language.Name);
foreach (string keyword in keywords)
{
var filterQuery = PredicateBuilder.False<PageSearchResultItem>()
.Or(item => item.Content.Like(keyword))
.Or(item => item.TeaserTitle.Like(keyword))
.Or(item => item.TeaserContent.Like(keyword));
baseQuery = baseQuery.And(filterQuery);
}
var searchResults = context.GetQueryable<PageSearchResultItem>().Filter(baseQuery).GetResults();
int count = searchResults.Hits.Count();
var results = searchResults.Hits.Select(i => i.Document).Skip(skip).Take(take).ToList();
return new PageSearchResults()
{
Results = results,
TotalResults = count
};
}
}
}
I have following service that uses the repository:
public class PageSearchService : IPageSearchService
{
private IPageSearchRepository _pageSearchRepository;
public PageSearchService(IPageSearchRepository pageSearchRepository)
{
this._pageSearchRepository = pageSearchRepository;
}
public PageSearchResults GetPagesBySearchQuery(string searchQuery, int page, int perpage)
{
int take = perpage;
int skip = (page - 1) * perpage;
string[] keywords = searchQuery.Split(' ');
return this._pageSearchRepository.FindByKeywords(keywords, perpage, skip);
}
}
I have following controller that uses the service:
public class SearchPageController : Controller
{
private IPageSearchService _pageSearchService;
public SearchPageController(IPageSearchService pageSearchService)
{
this._pageSearchService = pageSearchService;
}
[HttpGet]
public ActionResult Index(string query, int page = 1, int perpage = 10)
{
var pages = this._pageSearchService.GetPagesBySearchQuery(query, page, perpage);
var model = new SearchPageViewModel()
{
Pages = pages.Results,
PageNumber = page,
NumPages = (pages.TotalResults + perpage -1) / perpage ,
SearchQuery = query,
PerPage = perpage,
BaseUrl = Sitecore.Context.Item.Url()
};
return View(model);
}
}
My Dependencies are registered as follows:
public class DependenciesRegistration : IServicesConfigurator
{
[MethodImpl(MethodImplOptions.NoInlining)]
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection
.AddScoped<ISitecoreContext, SitecoreContext>()
.AddScoped<IPageSearchRepository, PageSearchRepository>()
.AddScoped<IPageSearchService, PageSearchService>();
serviceCollection.AddMvcControllersInCurrentAssembly();
var service = new ServiceDescriptor(typeof(BaseLinkManager), typeof(SwitchingLinkManager), ServiceLifetime.Singleton);
serviceCollection.Replace(service);
}
}
How I can inject the correct context to the repository so that my repository has correct ISearchProviderContext without writing the code to do that in repository class itself?
Sitecore.Context
in your repository?