Developer forum

Forum » Rapido » Dynamic Articles Paragraph search

Dynamic Articles Paragraph search

Kim Hansen
Reply

Hi,

We got a customer that cant get a search hit, when searching for content in the Content Paragraph in Dynamic Articles.

How do i set up the index and query to add hits for that?

Thanks

/Kim


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Kim,

 

We got the same issue a while back, and the problem is also the same with Paragraph Container. It's because that content comes from an Item Relation list.

 

That said, you need to create an Index Builder Extender and based on the Item Type (in this case Dynamic Articles) get the content of each paragraph and add it to a new property of the document.

 

Here's how we did it, although we're only addressing the title and content (not all of the fields from that item relation list)

 

using System;
using System.Data;
using System.Linq;
using Dynamicweb.Data;
using Dynamicweb.Indexing;
using Dynamicweb.Content;
using Dynamicweb.Content.Items;
using System.Collections.Generic;

namespace Dna.Winnebago.IndexBuilderExtender
{
    public class ContentIndexBuilderExtender : IndexBuilderExtenderBase<ContentIndexBuilder>
    {
        private readonly Lazy<DataRow[]> _paragraphsCollections;
        private static readonly ItemService ItemService = new ItemService();
        
        public ContentIndexBuilderExtender()
        {
            _paragraphsCollections = new Lazy<DataRow[]>(() =>
            {
                return Database.CreateDataSet($"Select ParagraphPageID, ParagraphItemType, ParagraphItemId FROM Paragraph WHERE ParagraphShowParagraph = 1 AND ISNULL(ParagraphItemType, '')='ParagraphContainer'").Tables[0].Select();
            });
        }

        private IEnumerable<DataRow> Paragraphs
        {
            get
            {
                return _paragraphsCollections.Value;
            }
        }
        
        public override void ExtendDocument(IndexDocument doc)
        {
            var pageId = doc.ContainsKey("PageId") ? (doc["PageId"] ?? "").ToString() : "";
            var pageItemId = doc.ContainsKey("PageItemId") ? (doc["PageItemId"] ?? "").ToString() : "";
            
            if (string.IsNullOrWhiteSpace(pageId)) return;

            doc["ItemRelationList_Heading"] = GetTitleFields(pageId, pageItemId).ToArray();
            doc["ItemRelationList_Content"] = GetTextFields(pageId, pageItemId).ToArray();

            if(doc.ContainsKey("Property_Page_CustomSettings"))
            {
                Helpers.SetItemFieldsOnDocument(doc, "PagePropertiesCustom", (doc["Property_Page_CustomSettings"] ?? "0").ToString(), "PagePropertiesCustomItem_");
            }
        }
       
        private IEnumerable<string> GetTitleFields(string pageId, string pageItemId)
        {
            var result = new List<string>();
            
            result.AddRange(GetValuesFromItem("DynamicArticle", pageItemId, "Heading"));
            result.AddRange(GetValuesFromParagraphContainer(pageId, "Title"));

            return result;
        }

        private IEnumerable<string> GetTextFields(string pageId, string pageItemId)
        {
            var result = new List<string>();
            
            result.AddRange(GetValuesFromItem("DynamicArticle", pageItemId, "Text"));
            result.AddRange(GetValuesFromParagraphContainer(pageId, "Text"));

            return result;
        }

        private static IEnumerable<string> GetValuesFromItem(string itemType, string pageItemId, string field)
        {
            if (!IsValidItem(itemType, pageItemId)) return new List<string>();
            
            var itemId = GetItemListId(itemType, pageItemId);
            return AppendValueFromItemList(itemId, field);
        }
        
        private IEnumerable<string> GetValuesFromParagraphContainer(string pageId, string field)
        {
            var result = new List<string>();
            
            foreach (var paragraph in Paragraphs.Where(r => (r["ParagraphPageID"] ?? "").ToString().Equals(pageId)))
            {
                var itemId = GetItemListId("ParagraphContainer", (paragraph["ParagraphItemId"] ?? "").ToString());
                result.AddRange(AppendValueFromItemList(itemId, field));
            }

            return result;
        }
        
        private static bool IsValidItem(string pageItemType, string pageItemId)
        {
            return GetItemListId(pageItemType, pageItemId) > 0;
        }

        private static int GetItemListId(string pageItemType, string pageItemId)
        {
            if (string.IsNullOrEmpty(pageItemId)) return 0;
            
            var item = ItemService.GetItem(pageItemType, pageItemId);
            if (item == null) return 0;
                
            var itemListId = Convert.ToInt32((item["Paragraphs"] ?? 0));
            
            return itemListId;
        }
        
        private static IEnumerable<string> AppendValueFromItemList(int itemListId, string itemField)
        {
            var result = new List<string>();
            var itemList = ItemList.GetItemListById(itemListId);

            if (itemList == null) return result;

            foreach (Item item in itemList.Relations)
            {
                var field = (item[itemField] ?? "").ToString();

                if (!string.IsNullOrEmpty(field))
                {
                    result.Add(field);
                }
            }

            return result;
        }
    }
}

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
Oleg Rodionov Dynamicweb Employee
Oleg Rodionov
Reply

Hi all,

New TFS 71355 has been created to implement support item relation list in content indexing. Thanks. 

BR, Oleg QA

 
Kim Hansen
Reply

Thanks :)

Have not tested the code, as the customer opted out on the function.

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply
This post has been marked as an answer

Hi Kim and Nuno

This feature #71355 is out in Dynamicweb 9.8+ versions

You are able to find this build in the download section:

http://doc.dynamicweb.com/releases-and-downloads/releases

Please contact Dynamicweb Support if you need any additional help regarding this.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

Votes for this answer: 2
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Thanks for letting us know Kristian

 

You must be logged in to post in the forum