Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Goal

This article provides information on how to index documents which are above the document size limit.

...

  1. Open the backend of your Sitefinity instance.

  2. Navigate to Administartion → Settings and click Advanced (your-site-domain/Sitefinity/Administration/Settings/Advanced)

  3. Open the Hawksearch configuration

  4. Under document size limit enter 4000KB

  5. Save the changes

...

Empty content field

Code Block
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Search;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Services.Search.Model;
using Hawksearch.Configuration;
using Hawksearch.SDK.Indexing;

namespace Hawksearch122.CustomSitefinityWebApp
{
    public class CustomSearchService : HawksearchService
    {
        private const string DcoumentContentType = "Telerik.Sitefinity.Libraries.Model.Document";

        protected override List<SubmitDocument> AdaptDocuments(IEnumerable<IDocument> documents)
        {
            var documentListdoc = new List<IDocument>();
            var configManager = ConfigManager.GetManagerdocuments.ToList().FirstOrDefault();
            var hawkConfigdocumentList = new configManager.GetSection<HawkSearchConfig>List<IDocument>(documents);

            foreachif (vardoc document in documents!= null)
            {
                var documentSizecontentTypeField = 0.0;
                var modifiedDocument = documentdoc.Fields.FirstOrDefault(f => f.Name == "ContentType");

                foreachif (varcontentTypeField field in document.Fields!= null)
                {
                    if (string.Equals(fieldcontentTypeField.Value != null.ToString(), DcoumentContentType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var documentSizeconfigManager += System.Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0;ConfigManager.GetManager();
                      }  var hawkConfig = configManager.GetSection<HawkSearchConfig>();
           }             documentList = new List<IDocument>();

if (documentSize > hawkConfig.DocumentSizeLimit)                 {    foreach (var document in documents)
            modifiedDocument = this.ModifyDocument(document);          {
      }                  documentList.Add(modifiedDocument);    var documentSize = 0.0;
     }              return base.AdaptDocuments(documentList);        var }modifiedDocument = document;

      private IDocument ModifyDocument(IDocument document)         {            foreach (var fieldsfield = newin List<IField>(document.Fields);
            var contentField = document.Fields.FirstOrDefault(f => f.Name == "Content");         {
    if (contentField != null)             {            if (field.Value != null)
      contentField.Value = string.Empty;             }           {
  var modifiedDocument = new Document(fields, document.IdentityField.Name);              return modifiedDocument;         }     }
}

Take first 500 words

Code Block
usingdocumentSize += System.Collections.Generic;
using System.Linq;
using Hawksearch.Search;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Services.Search.Model;
using Hawksearch.Configuration;
using Hawksearch.SDK.Indexing;
using Field = Telerik.Sitefinity.Services.Search.Publishing.Field;

namespace Hawksearch122.Custom
{
    public class CustomSearchService : HawksearchService
    {
        protected override List<SubmitDocument> AdaptDocuments(IEnumerable<IDocument> documents)Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0;
                                }
                            }

                            if (documentSize > hawkConfig.DocumentSizeLimit)
                            {
                                modifiedDocument = this.ModifyDocument(document);
                            }

                            documentList.Add(modifiedDocument);
                        }
                    }
                }
            }

            return base.AdaptDocuments(documentList);
        }

        private IDocument ModifyDocument(IDocument document)
        {
            var fields = new List<IField>(document.Fields);
            var contentField = document.Fields.FirstOrDefault(f => f.Name == "Content");

            if (contentField != null)
            {
                contentField.Value = string.Empty;
            }

            var modifiedDocument = new Document(fields, document.IdentityField.Name);

            return modifiedDocument;
        }
    }
}

Take first 500 words

Code Block
languagec#
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Search;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Services.Search.Model;
using Hawksearch.Configuration;
using Hawksearch.SDK.Indexing;
using Field = Telerik.Sitefinity.Services.Search.Publishing.Field;

namespace SitefinityWebApp
{
    public class CustomSearchService : HawksearchService
    {
        private const string DcoumentContentType = "Telerik.Sitefinity.Libraries.Model.Document";

        protected override List<SubmitDocument> AdaptDocuments(IEnumerable<IDocument> documents)
        {
            var doc = documents.ToList().FirstOrDefault();
            var documentList = new List<IDocument>(documents);

            if (doc != null)
            {
                var contentTypeField = doc.Fields.FirstOrDefault(f => f.Name == "ContentType");

                if (contentTypeField != null)
                {
                    if (string.Equals(contentTypeField.Value.ToString(), DcoumentContentType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var configManager = ConfigManager.GetManager();
                        var hawkConfig = configManager.GetSection<HawkSearchConfig>();
                        documentList = new List<IDocument>();

                        foreach (var document in documents)
                        {
                            var documentSize = 0.0;
                            var modifiedDocument = document;

                            foreach (var field in document.Fields)
                            {
                {             var documentList = newif List<IDocument>();(field.Value != null)
               var configManager = ConfigManager.GetManager();             var hawkConfig{
= configManager.GetSection<HawkSearchConfig>();              foreach (var document in documents)             {    documentSize += System.Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0;
        var documentSize = 0.0;                 var modifiedDocument = document; }
                foreach (var field in document.Fields)        }

       {                     if (field.Value != null)documentSize > hawkConfig.DocumentSizeLimit)
                      {      {
                  documentSize += System.Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0;             modifiedDocument = this.ModifyDocument(document);
         }                 }  }

              if (documentSize > hawkConfig.DocumentSizeLimit)           documentList.Add(modifiedDocument);
     {                   }
 modifiedDocument = this.ModifyDocument(document);                 }
                 documentList.Add(modifiedDocument);}
            }

            return base.AdaptDocuments(documentList);
        }

        private IDocument ModifyDocument(IDocument document)
        {
            var wordLimit = 500;
            var fields = new List<IField>(document.Fields);
            var contentField = document.Fields.FirstOrDefault(f => f.Name == "Content");
            fields.Remove(contentField);
            contentField = this.ExtractFieldContent(contentField, wordLimit);
            fields.Add(contentField);

            var modifiedDocument = new Document(fields, document.IdentityField.Name);

            return modifiedDocument;
        }

        private IField ExtractFieldContent(IField contentField, int wordLimit)
        {
            var fieldValue = contentField.Value.ToString();

            if (!string.IsNullOrWhiteSpace(fieldValue))
            {
                var modifiedContent = string.Join(" ", fieldValue.Split(' ').Take(wordLimit).ToArray());

                contentField = new Field
                {
                    Name = "Content",
                    Value = modifiedContent
                };
            }

            return contentField;
        }
    }
}

Register Custom Search Service

...

  1. Open the backend of your Sitefinity instance.

  2. Navigate to Administration → Settings (your-site-domain/Sitefinity/Administration/Settings)

  3. Go to Advanced (your-site-domain/Sitefinity/Administration/Settings/Advanced)

  4. Under Search → Search services → Hawksearch enter the TypeName of you custom search service (e.g. SitefinityWebApp.CustomSearchService)

  5. Save the changes