Sitecore upgrade from 6.5 to 7 – search code migration (scSearchContrib, FieldSearchParam, DateRangeSearchParam)

At the time of the conversion there wasn’t a lot of search results coming up to help with how to migrate from Sitecore’s scSearchContrib’s query syntax to Sitecore 7’s out of the box implementation with Sitecore.ContentSearch.

The migration wasn’t hard but I thought this post might provide a good starting point for those doing something similar.

Replace these using statements:

using scSearchContrib.Searcher;
using scSearchContrib.Searcher.Parameters;
using scSearchContrib.Searcher.Utilities;

with these:

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.Utilities;
using Sitecore.ContentSearch.Linq;
using Lucene.Net.Documents;

 

Convert a search query that uses scSearchContrib ‘s FieldSearchParam

Before:

protected List GetMediaItemBy(string routeId)
{
    var query = new List();

    var routeFieldParam = new FieldSearchParam
    {
        FieldName = "_applicableroutes",
        FieldValue = routeId.Replace("#", "hash"), //normalize #
        Condition = QueryOccurance.Must,
        Partial = false,
        TemplateIds = IdHelper.NormalizeGuid(TemplateId.ToString()),
        Database = SitecoreService.Database.Name
    };

    query.Add(routeFieldParam);

    using (var runner = new QueryRunner(IndexName))
    {
        var searchResult = runner.GetItems(query.ToArray());
        return searchResult.Select(a => SitecoreService.CreateType(a.GetItem())).ToList();
    }
}

After:

protected List GetMediaItemBy(string routeId)
{
    var searchIndex = ContentSearchManager.GetIndex(this.IndexName);
    using (var runner = searchIndex.CreateSearchContext())
    {
        var searchResult = runner.GetQueryable()
            .Where(i => i["_applicableroutes"] == routeId.Replace("#", "hash")
            && i.TemplateId == ID.Parse(TemplateId)
            && i.DatabaseName == SitecoreService.Database.Name);

        return searchResult.Select(a => SitecoreService.CreateType(a.GetItem(), false, false)).ToList();
    }
}

 

Convert a search query that uses scSearchContrib ‘s DateRangeSearchParam

before:

protected List GetMediaItemBy(string routeId, DateTime syncFromDate, DateTime syncToDate)
{
    var query = new List();

    var routeFieldParam = new FieldSearchParam
        {
            FieldName = "_applicableroutes",
            FieldValue = routeId.Replace("#","hash"), //normalize #
            Condition = QueryOccurance.Must,
            Partial = false,
            TemplateIds = IdHelper.NormalizeGuid(TemplateId.ToString()),
            Database = SitecoreService.Database.Name
        };

    var lastSynDateParam = new DateRangeSearchParam
        {
            Condition = QueryOccurance.Must,
            InnerCondition = QueryOccurance.Must,
            TemplateIds = IdHelper.NormalizeGuid(TemplateId.ToString()),
            Database = SitecoreService.Database.Name,
            Ranges = new List
                {
                    new DateRangeSearchParam.DateRange("_samdatemodified", syncFromDate, syncToDate),
                    new DateRangeSearchParam.DateRange("_expirydate", syncToDate, DateTime.MaxValue)
                }
        };

    query.Add(routeFieldParam);
    query.Add(lastSynDateParam);

    using (var runner = new QueryRunner(IndexName))
    {
        var searchResult = runner.GetItems(query.ToArray());
        return searchResult.Select(a => SitecoreService.CreateType(a.GetItem())).ToList();
    }
}

after:

protected List GetMediaItemBy(string routeId, DateTime syncFromDate, DateTime syncToDate)
{
    var searchIndex = ContentSearchManager.GetIndex(this.IndexName);
    using (var runner = searchIndex.CreateSearchContext())
    {
        var searchResult = runner.GetQueryable()
            .Where(i => i["_applicableroutes"] == routeId.Replace("#", "hash")
                && DateTools.StringToDate(i["_samdatemodified"]).Between(syncFromDate, syncToDate, Inclusion.Both)
                && DateTools.StringToDate(i["_expirydate"]).Between(syncFromDate, DateTime.MaxValue, Inclusion.Both)
                && i.TemplateId == ID.Parse(TemplateId)
                && i.DatabaseName == SitecoreService.Database.Name);

        return searchResult.Select(a => SitecoreService.CreateType(a.GetItem(), false, false)).ToList();
    }
}
Advertisements