Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

In this article you will find:

Goal

This article provides information on how to extend the indexing logic for Hawksearch Elastic

Prerequisite

Installed Connector - Installing the Connector

Building a Custom Search Service

  1. Create a custom search service class and inherit from HawksearchService.

  2. Change the Search Service implementation from Sitefinity’s Advanced Settings (your-website-domain/Sitefinity/Administration/Settings/Advanced) -

    1. Navigate to:
      Administration → Settings → Advanced Settings → Search Search ServicesHawksearch In TypeName field enter your custom search service type name (the namespace plus the name of the class e.g. SitefinityWebApp.Search.CustomSearchService).

Add External Data to the Index

  1. Add additional Fields to the index

The string type of the field in the code bellow is used as an example.

using Hawksearch.Search;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity.Services.Search;

namespace SitefinityWebApp.Search
{
    public class CustomSearchService : HawksearchService
    {
        public override void CreateIndex(string sitefinityIndexName, IEnumerable<IFieldDefinition> fieldDefinitions)
        {
            var newFieldDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>();

            newFieldDefinition.Name = "your-custom-field-name";
            newFieldDefinition.Type = typeof(string);

            var definitions = fieldDefinitions.ToList();
            definitions.Add(newFieldDefinition);

            base.CreateIndex(sitefinityIndexName, definitions);
        }
    }
}

2. Add additional Fields to Elasticsearch Document

using Hawksearch.Search;
using System.Collections.Generic;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Services.Search.Model;
using Telerik.Sitefinity.Services.Search.Publishing;

namespace SitefinityWebApp.Search
{
    public class CustomSearchService : HawksearchService
    {
        public override void UpdateIndex(string name, IEnumerable<IDocument> documents)
        {
            var documentList = new List<IDocument>();

            foreach (var document in documents)
            {
                var fields = new List<IField>();
                fields.AddRange(document.Fields);

                var newField = new Field
                {
                    Name = "your-custom-field-name",
                    Value = "your-custom-field-value"
                };

                fields.Add(newField);

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

                documentList.Add(modifiedDocument);
            }

            base.UpdateIndex(name, documentList);
        }
    }
}

  • No labels