Versions Compared

Key

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

In this article you will find:

...

Info

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) -

    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).

    Make sure you have created at least one content item (e.g. News)

  3. Open Administration → Search indexes and create a new index CustomTestIndex

  4. Open it and make sure that News items or the content item you have created is selected

  5. Create a page and add the Hawksearch box and Hawksearch results widgets

  6. Select from the widget designer of both widgets to search in your CustomTestIndex

Register Custom Search Service

In order to use your custom search service instead of the built-in one you need to register it in the backend.

Info

Please refer to this documentation - Register custom search service

Info

Setup search service

In order to add external data to the index you need to create a custom search service class which inherits the HawkseachService class and overrides the CreateIndex and UpdateIndex methods. Please refer to the code snippet below.

Add External Data to the Index

...

Code Block
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);
        }
    }
}
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 results

Now if you Inspect your frontend page you should be able to find your custom field added to your document in the XHR search → results → document fields