Kentico - Не удалось изменить настройки предварительного просмотра контента для интеллектуального поиска - PullRequest
0 голосов
/ 12 февраля 2020

Я пытался настроить содержание, следуя этому документу
https://docs.kentico.com/k9/custom-development/miscellaneous-custom-development-tasks/smart-search-api-and-customization/customizing-the-content-of-search-indexes
Хотя я меняю e.Content на что угодно, содержимое в результате не влияло
Это зависит только от поля содержимого, которое я установил в типе страницы

Вот мой код

using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.EventLog;
using CMS.Helpers;
using CMS.SiteProvider;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.Serialization;
using temp.objects;

[assembly: RegisterModule(typeof(IndexWidgetsContent))]

public class IndexWidgetsContent : Module
{
    // Module class constructor, the system registers the module under the name "CustomSmartSearch"
    public IndexWidgetsContent()
        : base("CustomSmartSearch")
    {
    }
    // Zone IDs will be ignore on smart search get content
    public static string[] AllowZones = new string[] {
        "ZoneWidget"
    };
    // Zone IDs will be ignore on smart search get content
    public static string[] ExcludeZones = new string[] {

    };

    // Web part code will be ignore on smart search get content
    public static string[] ExcludeWebparts = new string[] {

    };

    // Web part code will be ignore on smart search get content
    public static string[] ExcludeWebpartContainers = new string[] {
    };
    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        DocumentEvents.GetContent.Execute += OnGetPageContent;
    }

    private void OnGetPageContent(object sender, DocumentSearchEventArgs e)
    {
        TreeNode currentIndexedPage = e.Node;
        e.Content = e.Content == null ? "" : e.Content;
        List<string> ignoreContents = new List<string>();
        if (currentIndexedPage != null)
        {
            PageInfo pageInfo = PageInfoProvider.GetPageInfo(SiteContext.CurrentSiteName, currentIndexedPage.NodeAliasPath, currentIndexedPage.DocumentCulture,
                currentIndexedPage.DocumentUrlPath, currentIndexedPage.NodeID, SiteContext.CurrentSite.CombineWithDefaultCulture);

            // regex to find <span class="sr-only">Content ignore for smart search</span>
            string pattern = @"<(span|button|a|div|label)(.*)class=""(.*)sr-only(.*)"">(?<ContentADA>[^<]*)<\/(span|button|a|div|label)>";

            if (pageInfo != null)
            {
                CMS.PortalEngine.PageTemplateInstance templateInst = pageInfo.DocumentTemplateInstance;
                try
                {
                    // get content from page's widgets
                    string documentContent = currentIndexedPage.GetStringValue("DocumentContent", "");
                    e.Content += " " + HTMLHelper.HtmlToPlainText(documentContent) + " ";

                    if (!string.IsNullOrEmpty(documentContent) && documentContent.Contains("sr-only"))
                    {
                        var stringReader = new System.IO.StringReader(documentContent);
                        var serializer = new XmlSerializer(typeof(content));
                        var content = serializer.Deserialize(stringReader) as content;
                        if (content != null && content.webparts != null)
                        {
                            foreach (var webpartContent in content.webparts)
                            {
                                if (!string.IsNullOrEmpty(webpartContent.Content))
                                {
                                    RegexOptions options = RegexOptions.Multiline;
                                    foreach (Match match in Regex.Matches(webpartContent.Content, pattern, options))
                                    {
                                        string contentADA = match.Groups["ContentADA"].Value;
                                        ignoreContents.Add(contentADA);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventLogProvider.LogException("NodeID:" + currentIndexedPage.NodeID + ".Culture:" + currentIndexedPage.DocumentCulture, "DocumentContent", ex);
                }

                // Loop though web part in page to get more content
                foreach (var webpartZoneInst in templateInst.WebPartZones)
                {
                    foreach (CMS.PortalEngine.WebPartInstance inst in webpartZoneInst.WebParts)
                    {
                        string widgetText = ValidationHelper.GetString(inst.GetValue("Content"), "");
                        string plainText = HTMLHelper.HtmlToPlainText(widgetText);

                        if (widgetText.Contains("sr-only"))
                        {
                            RegexOptions options = RegexOptions.Multiline;

                            foreach (Match match in Regex.Matches(widgetText, pattern, options))
                            {
                                string contentADA = match.Groups["ContentADA"].Value;
                                ignoreContents.Add(contentADA);
                            }
                        }
                        if (AllowZones.Contains(webpartZoneInst.ZoneID))
                        {
                            e.Content += " " + plainText + " ";
                        }
                    }
                }
                e.Content = e.Content.Trim();
                EventLogProvider.LogInformation("NodeID:" + currentIndexedPage.NodeID + ".Culture:" + currentIndexedPage.DocumentCulture, "ignoreContents", JsonConvert.SerializeObject(ignoreContents));
                foreach (string contentADA in ignoreContents)
                {
                    // i removed unnecessary content inside  e.Content but it still display inside search result
                    e.Content = e.Content.Replace(contentADA.Trim(), " "); 
                }
                EventLogProvider.LogInformation("NodeID:" + currentIndexedPage.NodeID + ".Culture:" + currentIndexedPage.DocumentCulture, "e.Content.After:", e.Content);
            }
        }
    }


}

namespace temp.objects
{
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class content
    {
        [XmlElement("webpart")]
        public List<webpart> webparts { get; set; }
    }
    public class webpart
    {
        [XmlAttribute]
        public string id { get; set; }

        [XmlText(typeof(string))]
        public string Content { get; set; }
    }
}
...