Custom Search by date range

Posted by Community Admin on 04-Aug-2018 06:21

Custom Search by date range

All Replies

Posted by Community Admin on 12-Aug-2013 00:00

Hi,

I'm going to build the custom search control that allow user to select a specific date range. But I can't find any resource about search by date range in Sitefinity. Thanks in advance.

 

Posted by Community Admin on 14-Aug-2013 00:00

Hi,

Thank you for contacting us.
To solve your problem you will need to:

1. Extend our "ContentInboundPipe" to add custom field to the exported objects - in your case the field can be of type "int" and the value will be "itemDate.ToString("yyyyMMdd")".
In short - you will need to create a custom pipe inheriting from "ContentInboundPipe" and override the "SetProperties" method with something like this:

override void SetProperties(WrapperObject wrapperObject, IContent contentItem)
   base.SetProperties(wrapperObject, contentItem);
 
   wrapperObject.AddProperty("MyPublicationDateField", int.Parce(date.ToString("yyyyMMdd")));

Then you need to replace our pipe putting something like this code in "Application_Start" method in global.asax:
PublishingSystemFactory.UnregisterPipe(ContentInboundPipe.PipeName);
PublishingSystemFactory.RegisterPipe(ContentInboundPipe.PipeName, typeof(MyCustomContentInboundPipe));
 

2. You need to create a new SearchResults widget, inheriting from the default one.
You need to override the "GetSearcher" method and return new implementation of "ISearcher" interface.
Here is a sample code of "ISearcher" implementation:

private class NewSearcher : Searcher
                   
            public NewSearcher(SearchResults control)
                : base(control)
            
            
 
            public override IEnumerable<IDocument> Search(string query, string catalogue, int skip, int take, out int hitCount)
            
                var control = this.control;
                var service = control.GetSearchService();
 
                var compiledQuery = control.BuildSearchQuery(query, service);
 
// At this point the query will be similar to "(Title:searchText Content:searchText )"
// We need to add the date filter here
                compiledQuery = compiledQuery .Substring(0, compiledQuery .Length - 1); // removing the last ')'
                compiledQuery += "MyPublicationDateField:[20130813 TO 20130814])"; // here we add the date filter and close the expression with ')'
 
                IResultSet result = service.Search(
                    catalogue,
                    compiledQuery,
                    control.HighlightedFields,
                    skip,
                    take,
                    null);
 
                hitCount = result.HitCount;
 
                return result.SetContentLinks();
            
        

When your widget is initialized, it will search for results with this field, matching the range that we set.

Please contact us again if there is anything else you need to ask about this solution, or if you need further assistance.

Regards,
Bonny
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed