In this article you will find:
...
Open the backend of your Sitefinity instance
Navigate to Administartion → Settings and click Advanced (your-site-domain/Sitefinity/Administration/Settings/Advanced)
Open the Hawksearch configuration
Under document size limit enter 100KB
Save the changes
...
Open the backend of your Sitefinity instance
Navigate to Content → Documents & Files
Upload files above the document size limit e.g. 20MB
Navigate to Administration → Search indexes (your-site-domain/Sitefinity/Administration/Search indexes)
Open your currently used and active Index
Select Documents from the scope. This will add the documents you have uploaded to the index
...
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 SitefinityWebApp { public class CustomSearchService : HawksearchService { private const string DocumentContentType = "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(), DocumentContentType, StringComparison.InvariantCultureIgnoreCase)) { var configManager = ConfigManager.GetManager(); var hawkConfig = configManager.GetSection<HawkSearchConfig>(); documentList.Clear(); foreach (var document in documents) { var modifiedDocument = document; var documentSize = this.CalculateDocumentSize(document); if (documentSize > hawkConfig.DocumentSizeLimit) { modifiedDocument = this.ModifyDocument(document); } documentList.Add(modifiedDocument); } } } } return base.AdaptDocuments(documentList); } private double CalculateDocumentSize(IDocument document) { var documentSize = 0.0; foreach (var field in document.Fields) { if (field.Value != null) { documentSize += System.Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0; } } return documentSize; } 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; } } } |
Note |
---|
Once you implement the code in Visual Studio , build your solution and you will also have to reindex the index you are using from Administrator → Search Indexes → Action → Reindex |
Info |
---|
Expected resultsNow if you Inspect your frontend page you should be able to see the title of your large document, but not the content. There will be no content field as well in the XHR search → results → document fields |
Take the first 500 words
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; using Field = Telerik.Sitefinity.Services.Search.Publishing.Field; namespace SitefinityWebApp { public class CustomSearchService : HawksearchService { private const string DocumentContentType = "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(), DocumentContentType, StringComparison.InvariantCultureIgnoreCase)) { var configManager = ConfigManager.GetManager(); var hawkConfig = configManager.GetSection<HawkSearchConfig>(); documentList.Clear(); foreach (var document in documents) { var modifiedDocument = document; var documentSize = this.CalculateDocumentSize(document); if (documentSize > hawkConfig.DocumentSizeLimit) { modifiedDocument = this.ModifyDocument(document); } documentList.Add(modifiedDocument); } } } } return base.AdaptDocuments(documentList); } private double CalculateDocumentSize(IDocument document) { var documentSize = 0.0; foreach (var field in document.Fields) { if (field.Value != null) { documentSize += System.Text.Encoding.Unicode.GetByteCount(field.Value.ToString()) / 1024.0; } } return documentSize; } 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; } } } |
...