0

I have added a new sxa token processor to filter the passed date events,

This is my Code

 protected virtual SearchStringModel BuildModel(string fieldName)
    {
        Log.Info($"SXA_Token customSearchModel: {new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)fieldName.ToLowerInvariant(), $"[{DateTime.Today.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")} TO *]")))}", "SXA_Token");
        return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)fieldName.ToLowerInvariant(), (object)$"[{DateTime.Today.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")} TO *]")))
        {
            Operation = "must"
        };
    }

This is my Solr Query

INFO  Solr Query - ?q=((event_end_date_s:(\[2022\-03\-10T18\:30\:00.000Z\ TO\ *\]) 

Expected solr Query format

event_end_date_s:[2022-03-10T18:30:00.000Z TO *]

How should I format my date and need to remove \? I just tried to remove ' from my code, still no luck

2 Answers 2

0

I have resolved the issue. I would like to add my answer here

My computed field config

 <field fieldName="event_start_date" sourceField="StartDate" returnType="string">Foundation.Search.ComputedFields.DateFieldComputedField, Foundation.Search</field>
              <field fieldName="event_end_date" sourceField="EndDate" returnType="string">Foundation.Search.ComputedFields.DateFieldComputedField, Foundation.Search</field>

Computed field logic

 public object ComputeFieldValue(IIndexable indexable)
    {
        var indexableItem = indexable as SitecoreIndexableItem;
        if (indexableItem == null) return null;
        if (string.IsNullOrEmpty(SourceField)) return null;
        var field = indexableItem.Item.Fields[SourceField];
        if (field == null) return null;
        return Getvalue(field);
    }

    private DateTime Getvalue(Field fieldValue)
    {
        DateTime serverTime = DateUtil.IsoDateToDateTime(fieldValue.Value, DateTime.MinValue);
        return DateUtil.ToUniversalTime(serverTime);
    }

SXA Token logic

  public class ExcludePastEvents : ResolveSearchQueryTokensProcessor
{
    protected string TokenPart { get; } = "ExcludeItemWithPreviousDateInDateField";
    private readonly string DateCompareIdentifier = "#datecompare#";

    [SxaTokenKey]
    protected override string TokenKey => FormattableString.Invariant(FormattableStringFactory.Create("{0}|FieldName", (object)this.TokenPart));

    public override void Process(ResolveSearchQueryTokensEventArgs args)
    {
        if (args.ContextItem == null)
        {
            return;
        }
        for (int index = 0; index < args.Models.Count; index++)
        {
            SearchStringModel model = args.Models[index];
            if (model.Type.Equals("sxa") && ContainsToken(model))
            {
                string fieldName = model.Value.Replace(TokenPart, string.Empty).TrimStart('|');
                args.Models.Insert(index, this.BuildModel(fieldName));
                args.Models.Remove(model);
            }
        }
    }
    protected virtual SearchStringModel BuildModel(string fieldName)
    {
        try
        {
            Assert.ArgumentNotNull(fieldName, "fieldName");

            string fieldFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'";
            DateTime dateTime = DateUtil.ToUniversalTime(DateTime.UtcNow);

            string formatedDate = string.Format(arg1: "*", format: "{2}[{0} TO {1}]", arg0: dateTime.ToString(fieldFormat), arg2: DateCompareIdentifier);

            return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)fieldName.ToLowerInvariant(), formatedDate)))
            {
                Operation = "must"
            };
        }
        catch (Exception ex)
        {
            Log.Info(string.Format("BuildModel - error-{0}stacktrace{1}", ex.Message.ToString(), ex.StackTrace.ToString()), ex);
        }
        return new SearchStringModel();

    }

    protected override bool ContainsToken(SearchStringModel m)
    {
        return Regex.Match(m.Value, FormattableString.Invariant($"{TokenPart}\\|[a-zA-Z ]*")).Success;
    }
}
0

Try to skip the date formatting. It should work. And if you have the computed field, set the field type DateTime.

<field fieldName="date" returnType="datetime">
Maulik.App.ContentSearch.ComputedIndexFields.DateForBoosting,Maulik.App.ContentSearch
</field>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.