跳到主要内容

Sitecore

自定义saxa令牌用于搜索范围查询,以支持所有筛选操作

Marketo领先评分

先决条件:

挑战:

我们可以在搜索范围查询中为一个saxa搜索结果组件提供多个搜索过滤器。我们可以在过滤器中使用预定义的saxa搜索令牌。

如果单个搜索筛选器以 则应用于过滤器的操作为
+ 必须(AND操作)
- - - - - - not (not操作)
没有设置或默认值 应该(或操作)

使用预定义的saxa令牌(如ItemsWithTheSameValueInField)的挑战在于,它总是将操作视为“必须”。因此,即使我们切换过滤器,即改变如下所示的操作,也不会影响搜索结果。

1 Itemswiththesamevalueinfielddemo

ItemsWithTheSameValueInField演示

同样适用于taggedsameeascurrentpage和TaggedWithAtLeastOneTagFromCurrentPage等saxa令牌。基本上,这两个令牌的实现是相同的,除了过滤器类型,即操作有不同的值。TaggedTheSameAsCurrentPage设置“must”,TaggedWithAtLeastOneTagFromCurrentPage设置“should”。该操作是在后端硬编码的。在不需要切换筛选操作和使用这些特定的saxa令牌的场景中,这是非常棒的。

这3个令牌将当前页面字段值与搜索结果项的字段值进行比较,以过滤结果。

解决方案:

我们可以根据预定义的令牌实现,通过一些修改来实现新的搜索令牌。我们可以提供内容编辑器用户在搜索范围内设置的操作,如下面的实现所示。

Code commented with “setting the operation given in the scope” is the key, and also do check other comments for reference .

CustomItemsWithTheSameValueInField -用于一个简单的字段类型或只保存一个ID的字段类型。

使用Sitecore.ContentSearch.Utilities;使用Sitecore.Data.Fields;使用Sitecore.XA.Foundation.Search.Attributes;使用Sitecore.XA.Foundation.Search.Pipelines.ResolveSearchQueryTokens;使用系统;使用System.Runtime.CompilerServices;使用包含;命名空间customssa . foundation . search . searchquerytoken{公共类customitemswithsameevalueinfield: ResolveSearchQueryTokensProcessor{受保护字符串TokenPart {get;} = name (customitemswithsameevalueinfield);protected string操作{get; set; } = "should"; [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]; //SearchStringModel holds a specific filter detail given in the scope if (model.Type.Equals("sxa") && this.ContainsToken(model)) { string str = model.Value.Replace(this.TokenPart, string.Empty).TrimStart('|'); Field field = args.ContextItem.Fields[str]; if (field != null) { this.Operation = model.Operation; //setting the operation given in the scope args.Models.Insert(index, this.BuildModel(str, field.Value)); //pass the field value for filter args.Models.Remove(model); } } } } protected virtual SearchStringModel BuildModel( string replace, string fieldValue) { return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)replace.ToLowerInvariant(), (object)fieldValue))) { Operation = this.Operation }; } protected override bool ContainsToken(SearchStringModel m) => Regex.Match(m.Value, FormattableString.Invariant(FormattableStringFactory.Create("{0}\\|[a-zA-Z ]*", (object)this.TokenPart))).Success; } }

下面是一个简短的演示,其中字段Team和Buddy分别为单行文本和Droplink字段类型。

2 Customitemswiththesamevalueinfielddemo

CustomItemsWithTheSameValueInField演示

ItemsWithIDsInFieldAsCurrentPage -用于选择单个ID或多个ID的复杂字段类型。

使用Sitecore.ContentSearch.Utilities;使用Sitecore.Data.Fields;使用Sitecore.Data.Items;使用Sitecore.XA.Foundation.Search.Attributes;使用Sitecore.XA.Foundation.Search.Pipelines.ResolveSearchQueryTokens;使用Sitecore.XA.Foundation.SitecoreExtensions.Extensions;使用系统;使用System.Collections.Generic;使用来;使用System.Runtime.CompilerServices; using System.Text.RegularExpressions; namespace CustomSXA.Foundation.Search.SearchQueryToken { public class ItemsWithIDsInFieldAsCurrentPage : ResolveSearchQueryTokensProcessor { protected string TokenPart => nameof(ItemsWithIDsInFieldAsCurrentPage); protected string Operation { get; set; } = "should"; [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.Is("sxa") && this.ContainsToken(model)) { string str = model.Value.Replace(this.TokenPart, string.Empty).TrimStart('|'); MultilistField field = (MultilistField)args.ContextItem.Fields[str]; if (field != null) { Item[] items = field.GetItems(); foreach (Item obj in ((IEnumerable)items).Reverse()) //loop through all the selected IDs { this.Operation = model.Operation; //setting the operation given in the scope args.Models.Insert(index, this.BuildModel(str, Convert.ToString(obj.ID))); //pass the selected item ID for filter } if (items.Length >= 2) index += items.Length - 1; args.Models.Remove(model); } } } } protected virtual SearchStringModel BuildModel( string replace, string fieldValue) { return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)replace.ToLowerInvariant(), (object)fieldValue))) { Operation = this.Operation }; } protected override bool ContainsToken(SearchStringModel m) => Regex.Match(m.Value, FormattableString.Invariant(FormattableStringFactory.Create("{0}\\|[a-zA-Z ]*", (object)this.TokenPart))).Success; } }

下面是一个简短的演示,其中字段Services和Skills为字段类型Multilist。

3 Itemswithidsinfieldascurrentpage

ItemsWithIDsInFieldAsCurrentPage演示

如下所示修补上面的类。

<?xml version = " 1.0 " ?>          

的NuGet包列表在这里

请查看存储库用于customssa . foundation . search项目中的上述实现。

希望这能有所帮助。

Happy Sitecore搜索查询范围

留下回复

你的电邮地址将不会公布。必填字段已标记

这个网站使用Akismet来减少垃圾邮件。了解如何处理您的评论数据

古普塔Sandeepkumar

Gupta Sandeepkumar是Perficient公司的首席技术顾问。他喜欢Sitecore的研发工作。他为基于saxa、ASP的Sitecore项目做出了贡献。NET MVC和Web表单。他还为Sitecore升级项目做出了贡献。

更多来自作者

类别
关注我们
推特 Linkedin 脸谱网 Youtube Instagram