Habitat Generic SearchService.cs

Hi,

Updated Sitecore.Foundation.Indexing.Services.SearchService.cs file within Habitat project and make it more generic so can extend and use within Feature modules.

Code I have updated in SearchService.cs

It is easy to implement
For an example if you want to implement in feature module than first create a class inherits from SearchResultItem/ IndexedItem and add your fields you require to fetch from indexes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Sitecore.Feature.Exhibitions.Index
{
	using System.Runtime.Serialization;
	using Sitecore.ContentSearch;
	using Sitecore.ContentSearch.SearchTypes;
	using Sitecore.Foundation.Indexing.Models;

	public class ExhibitionSearchResultItem: IndexedItem
	{
		[IndexField("exhibitionstartdate")]
		[DataMember]
		public virtual DateTime StartDate { get; set; }

		[IndexField("exhibitionenddate")]
		[DataMember]
		public virtual DateTime EndDate { get; set; }


	}
}

namespace Sitecore.Feature.Exhibitions.Repositories
{
	using System;
	using System.Linq;
	using Sitecore.ContentSearch;
	using Sitecore.ContentSearch.SearchTypes;
	using Sitecore.Foundation.Indexing.Models;
	using Sitecore.Foundation.Indexing.Services;
	using Sitecore.Feature.Exhibitions.Index;


	public class ExhibitionSearchService : SearchService<ExhibitionSearchResultItem>
	{

		public ExhibitionSearchService(ISearchSettings settings) : base(settings)
		{
		}


		public override IQueryable<ExhibitionSearchResultItem> CreateAndInitializeQuery(IProviderSearchContext context)
		{
			var queryable = base.CreateAndInitializeQuery(context);
			queryable = queryable.Where(x => x.StartDate > DateTime.Now);
			return queryable;
		}

	}
}

Please let me know if you have any suggestion, concern or question.

Cheers

You can download all three files from Git repo links are:
SearchService.cs: https://github.com/ShreeIT/sitecore-habitat-search/blob/master/SearchService.cs
ExhibitionSearchResultItem.cs: https://github.com/ShreeIT/sitecore-habitat-search/blob/master/ExhibitionSearchResultItem.cs
ExhibitionSearchService.cs: https://github.com/ShreeIT/sitecore-habitat-search/blob/master/ExhibitionSearchService.cs

Leave a Reply