Custom Search by date range
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.
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"
)));
PublishingSystemFactory.UnregisterPipe(ContentInboundPipe.PipeName);
PublishingSystemFactory.RegisterPipe(ContentInboundPipe.PipeName,
typeof
(MyCustomContentInboundPipe));
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();