I have being working on Sitecore 93 SXA, and I'm creating a sxa search query token by extending pipeline ResolveSearchQueryTokensProcessor referring this blog to get the date field of item and compare it with current date, and filter out the SearchStringModel meeting the criteria. here is my code:
protected string TokenPart { get; } = "ExcludeItemWithPreviousDateInDateField";
[SxaTokenKey]
protected override string TokenKey => FormattableString.Invariant(FormattableStringFactory.Create("{0}|FieldName", (object)this.TokenPart));
public override void Process(ResolveSearchQueryTokensEventArgs args)
{
if (args.ContextItem == null || args.ContextItem.ID.IsNull)
return;
for (var index = 0; index < args.Models.Count; ++index)
{
var model = args.Models[index];
if (model.Type.Equals("sxa", StringComparison.OrdinalIgnoreCase) && ContainsToken(model))
{
string fieldName = model.Value.Replace(TokenPart, string.Empty).TrimStart('|');
var dateField = args.ContextItem.Fields[fieldName];
if (dateField != null)
{
args.Models.Insert(index, this.BuildModel(fieldName, dateField.Value));
}
args.Models.Remove(model);
}
}
}
private bool isPastEvent(string fieldValue)
{
var endOfToday = DateTimeProvider.GetUtcNow().Date.AddHours(23).AddMinutes(59).AddSeconds(59);
var fieldTime = DateUtil.ParseDateTime(fieldValue, DateTime.MaxValue).Date.AddHours(23).AddMinutes(59).AddSeconds(59);
if (endOfToday > fieldTime)
return true;
return false;
}
private SearchStringModel BuildModel(string fieldName, string fieldValue)
{
if(isPastEvent(fieldValue))
return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)fieldName.ToLowerInvariant(), (object)fieldValue)))
{
Operation = "must"
};
return null;
}
protected override bool ContainsToken(SearchStringModel m)
{
return Regex.Match(m.Value, FormattableString.Invariant(FormattableStringFactory.Create("{0}\\|[a-zA-Z ]*", (object)this.TokenPart))).Success;
}
here is my patch file:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" >
<sitecore>
<pipelines>
<resolveSearchQueryTokens>
<processor type="Sitecore.PartnerHub.Feature.SXAExtension.ExcludeItemWithPreviousDate, Sitecore.PartnerHub.Feature.SXAExtension" resolve="true" patch:before = "*[1]"/>
</resolveSearchQueryTokens>
</pipelines>
</sitecore>
</configuration>
Now when I am including the patch and dlls in the local instance, something like this in response body:
{"TotalTime":69,"CountTime":0,"QueryTime":17,"Signature":"onlyevents","Index":"sitecore_sxa_master_index","Count":5, "Result" :[jsoncontent]}
The log I found in the Search log for token is:
7844 17:22:59 INFO Solr Query - ?q=__solr_norm_field_name_s:(_url)&start=0&rows=1&fq=_template:(455a3e98a6274b408035e683a0331ac7)&fq=_indexname:(sitecore_master_index)&wt=xml 9860 17:23:00 INFO Solr Query - ?q=((((_template:(d6c0b976d5a74ec598ad8c770cbc751f) AND _template:(ExcludeItemWithPreviousDateInDateField|EventDate)) AND _path:(11111111111111111111111111111111)) AND _datasource:(sitecore)) AND _latestversion:(1))&start=0&rows=20&fl=*,score&fq=_indexname:(sitecore_master_index)&wt=xml
In Solr log:
2020-11-23 13:33:57.631 INFO (qtp952562199-206) [
x:syedaABB93_master_index] o.a.s.c.S.Request [syedaABB93_master_index] webapp=/solr path=/select params={q=((((_template:(d6c0b976d5a74ec598ad8c770cbc751f)+AND+_template:(ExcludeItemWithPreviousDateInDateField|EventDate))+AND+_path:(11111111111111111111111111111111))+AND+_datasource:(sitecore))+AND+_latestversion:(1))&fl=*,score&start=0&fq=_indexname:(sitecore_master_index)&rows=20&wt=xml&wt=xml&version=2.2} hits=0 status=0 QTime=0
Update: I have updated the logs and changed the code, by putting date check in build model. But the code is searching the field value for search model in the current page, which is independent of the search result. My search scope is having template id and sxa token only.
I am trying first time to extend sxa search token, so please guide me if I'm doing anything wrong...
Thanks in advance.