Skip to content

Commit

Permalink
Merge pull request #17 from Kentico/seangwright/master
Browse files Browse the repository at this point in the history
Feature: Algolia ISearchIndex Customization
  • Loading branch information
kentico-ericd authored Jun 2, 2022
2 parents 073cb6c + d071f0a commit 9f1c186
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 62 deletions.
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true
},

"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.code-search": true,
"**/bin": true,
"**/obj": true
},

"omnisharp.defaultLaunchSolution": "./XperienceAlgolia.sln",
"omnisharp.enableImportCompletion": true
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;
using Algolia.Search.Models.Search;

using CMS.Core;
using CMS.Helpers;

Expand Down Expand Up @@ -34,8 +34,8 @@ protected override void OnLoad(EventArgs e)
string searchText = QueryHelper.GetString("searchtext", "");
if (!string.IsNullOrEmpty(searchText))
{
var client = Service.Resolve<ISearchClient>();
var searchIndex = client.InitIndex(indexName);
var searchIndexService = Service.Resolve<IAlgoliaIndexService>();
var searchIndex = searchIndexService.InitializeIndex(indexName);
if (searchIndex == null)
{
ShowError("Error loading search index. Please check the Event Log for more details.");
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,21 +311,21 @@ public string Thumbnail { get; set; }

## :mag_right: Implementing the search interface

You can use Algolia's [.NET API](https://www.algolia.com/doc/api-client/getting-started/what-is-the-api-client/csharp/?client=csharp), [JavaScript API](https://www.algolia.com/doc/api-client/getting-started/what-is-the-api-client/javascript/?client=javascript), or [InstantSearch.js](https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/) to implement a search interface on your live site. The following example will help you with creating a search interface for .NET Core. In your Controllers, you can get a `SearchIndex` object by injecting the `ISearchClient` interface and calling the `InitIndex()` method on the client using your index's code name. Then, construct a `Query` to search the Algolia index. Algolia's pagination is zero-based, so in the Dancing Goat sample project we subtract 1 from the current page number:
You can use Algolia's [.NET API](https://www.algolia.com/doc/api-client/getting-started/what-is-the-api-client/csharp/?client=csharp), [JavaScript API](https://www.algolia.com/doc/api-client/getting-started/what-is-the-api-client/javascript/?client=javascript), or [InstantSearch.js](https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/) to implement a search interface on your live site. The following example will help you with creating a search interface for .NET Core. In your Controllers, you can get a `SearchIndex` object by injecting the `IAlgoliaIndexService` interface and calling the `InitializeIndex()` method on the client using your index's code name. Then, construct a `Query` to search the Algolia index. Algolia's pagination is zero-based, so in the Dancing Goat sample project we subtract 1 from the current page number:

```cs
private readonly ISearchClient _searchClient;
private readonly IAlgoliaIndexService _indexService;

public SearchController(ISearchClient searchClient)
public SearchController(IAlgoliaIndexService indexService)
{
_searchClient = searchClient;
_indexService = indexService;
}

public ActionResult Search(string searchText, int page = DEFAULT_PAGE_NUMBER)
{
page = Math.Max(page, DEFAULT_PAGE_NUMBER);

var searchIndex = _searchClient.InitIndex(AlgoliaSiteSearchModel.IndexName);
var searchIndex = _indexService.InitializeIndex(AlgoliaSiteSearchModel.IndexName);
var query = new Query(searchText)
{
Page = page - 1,
Expand Down Expand Up @@ -552,7 +552,7 @@ As the search interface can be designed in multiple languages using Algolia's AP
The Dancing Goat store doesn't use search out-of-the-box, so first you need to hook it up to Algolia. In this example, the search model seen in [Determining which pages to index](#determining-which-pages-to-index) will be used.
1. Inject `ISearchClient` into the `CoffeesController` as shown in [this section](#mag_right-implementing-the-search-interface).
1. Inject `IAlgoliaIndexService` into the `CoffeesController` as shown in [this section](#mag_right-implementing-the-search-interface).
2. In __CoffeesController.cs__, create a method that will perform a standard Algolia search. In the `Query.Filters` property, add a filter to only retrieve records where `ClassName` is `DancingGoatCore.Coffee.` You also specify which `Facets` you want to retrieve, but they are not used yet.
Expand All @@ -571,7 +571,7 @@ private SearchResponse<AlgoliaSiteSearchModel> Search()
Facets = facetsToRetrieve
};

var searchIndex = _searchClient.InitIndex(AlgoliaSiteSearchModel.IndexName);
var searchIndex = _indexService.InitializeIndex(AlgoliaSiteSearchModel.IndexName);
return searchIndex.Search<AlgoliaSiteSearchModel>(query);
}
```
Expand Down Expand Up @@ -700,7 +700,7 @@ private SearchResponse<AlgoliaSiteSearchModel> Search(IAlgoliaFacetFilter filter
Facets = facetsToRetrieve
};

var searchIndex = _searchClient.InitIndex(AlgoliaSiteSearchModel.IndexName);
var searchIndex = _indexService.InitializeIndex(AlgoliaSiteSearchModel.IndexName);
return searchIndex.Search<AlgoliaSiteSearchModel>(query);
}
```
Expand Down Expand Up @@ -1256,7 +1256,7 @@ While the Xperience Algolia integration works without any changes to the Xperien
### Importing the custom module
1. Download the latest _Kentico.Xperience.AlgoliaSearch_ ZIP package in the [/CMS/CMSModules/Kentico.Xperience.AlgoliaSearch](/CMS/CMSModules/Kentico.Xperience.AlgoliaSearch) directory
1. Download the _Kentico.Xperience.AlgoliaSearch_ ZIP package by locating the latest "Custom module" [Release](https://github.com/Kentico/xperience-algolia/releases).
1. In the Xperience adminstration, open the __Sites__ application.
1. [Import](https://docs.xperience.io/deploying-websites/exporting-and-importing-sites/importing-a-site-or-objects) the downloaded package with the __Import files__ and __Import code files__ [settings](https://docs.xperience.io/deploying-websites/exporting-and-importing-sites/importing-a-site-or-objects#Importingasiteorobjects-Import-Objectselectionsettings) enabled.
1. Perform the [necessary steps](https://docs.xperience.io/deploying-websites/exporting-and-importing-sites/importing-a-site-or-objects#Importingasiteorobjects-Importingpackageswithfiles) to include the following imported folder in your project:
Expand Down
2 changes: 1 addition & 1 deletion src/AlgoliaQueueWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AlgoliaQueueWorker()
/// <summary>
/// Adds an <see cref="AlgoliaQueueItem"/> to the worker queue to be processed.
/// </summary>
/// <param name="updatedNode"></param>
/// <param name="queueItem">The item to be added to the queue.</param>
public static void EnqueueAlgoliaQueueItem(AlgoliaQueueItem queueItem)
{
if (queueItem == null || queueItem.Node == null || String.IsNullOrEmpty(queueItem.IndexName))
Expand Down
1 change: 1 addition & 0 deletions src/AlgoliaStartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class AlgoliaStartupExtensions
/// Registers instances of <see cref="IInsightsClient"/> and <see cref="ISearchClient"/>
/// with Dependency Injection.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configuration">The application configuration.</param>
public static IServiceCollection AddAlgolia(this IServiceCollection services, IConfiguration configuration)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Kentico.Xperience.AlgoliaSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<Title>Xperience Algolia Search</Title>
<PackageId>Kentico.Xperience.AlgoliaSearch</PackageId>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Authors>Kentico Software</Authors>
<Company>Kentico Software</Company>
<PackageIcon>icon.png</PackageIcon>
Expand All @@ -21,6 +21,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" $(Configuration) == 'Release' ">
Expand Down
4 changes: 1 addition & 3 deletions src/Models/Facets/AlgoliaFacetFilterViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Algolia.Search.Models.Search;

using Kentico.Xperience.AlgoliaSearch.Attributes;
using Kentico.Xperience.AlgoliaSearch.Attributes;

using Microsoft.Extensions.Localization;

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Facets/IAlgoliaFacetFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IAlgoliaFacetFilter
/// <summary>
/// A collection of an Algolia index's faceted attributes and the available facets.
/// </summary>
public abstract AlgoliaFacetedAttribute[] FacetedAttributes
AlgoliaFacetedAttribute[] FacetedAttributes
{
get;
set;
Expand All @@ -28,7 +28,7 @@ public abstract AlgoliaFacetedAttribute[] FacetedAttributes
/// </summary>
/// <param name="searchModelType">The Algolia search model that is being used in
/// the current query. If null, all facet filters will use the "OR" condition.</param>
public string GetFilter(Type searchModelType = null);
string GetFilter(Type searchModelType = null);


/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Services/IAlgoliaConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IAlgoliaConnection
/// or null.</exception>
/// <exception cref="InvalidOperationException">Thrown if the search model is configured
/// incorrectly or index settings cannot be loaded.</exception>
public void Initialize(string indexName);
void Initialize(string indexName);


/// <summary>
Expand All @@ -33,7 +33,7 @@ public interface IAlgoliaConnection
/// </summary>
/// <param name="objectIds">The Algolia internal IDs of the records to delete.</param>
/// <returns>The number of records deleted.</returns>
public int DeleteRecords(IEnumerable<string> objectIds);
int DeleteRecords(IEnumerable<string> objectIds);


/// <summary>
Expand All @@ -44,7 +44,7 @@ public interface IAlgoliaConnection
/// <remarks>Logs an error if there are issues loading the node data.</remarks>
/// <param name="dataObjects">The objects to upsert into Algolia.</param>
/// <returns>The number of objects processed.</returns>
public int UpsertRecords(IEnumerable<JObject> dataObjects);
int UpsertRecords(IEnumerable<JObject> dataObjects);


/// <summary>
Expand All @@ -53,6 +53,6 @@ public interface IAlgoliaConnection
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if a search model class is not
/// found for the index.</exception>
public void Rebuild();
void Rebuild();
}
}
16 changes: 16 additions & 0 deletions src/Services/IAlgoliaIndexService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Algolia.Search.Clients;

namespace Kentico.Xperience.AlgoliaSearch.Services
{
/// <summary>
/// Initializes <see cref="ISearchIndex" /> instances.
/// </summary>
public interface IAlgoliaIndexService
{
/// <summary>
/// Initializes a new <see cref="ISearchIndex" /> for the given <paramref name="indexName" />.
/// </summary>
/// <param name="indexName">The code name of the index.</param>
ISearchIndex InitializeIndex(string indexName);
}
}
6 changes: 3 additions & 3 deletions src/Services/IAlgoliaIndexingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IAlgoliaIndexingService
/// <param name="node">The <see cref="TreeNode"/> that triggered the event.</param>
/// <param name="wasDeleted">True if the <paramref name="node"/> was deleted.</param>
/// <param name="isNew">True if the <paramref name="node"/> was created.</param>
public void EnqueueAlgoliaItems(TreeNode node, bool wasDeleted, bool isNew);
void EnqueueAlgoliaItems(TreeNode node, bool wasDeleted, bool isNew);


/// <summary>
Expand All @@ -36,7 +36,7 @@ public interface IAlgoliaIndexingService
/// <returns>A <see cref="JObject"/> with its properties and values set.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="node"/> or
/// <paramref name="searchModelType"/> are null.</exception>
public JObject GetTreeNodeData(TreeNode node, Type searchModelType);
JObject GetTreeNodeData(TreeNode node, Type searchModelType);


/// <summary>
Expand All @@ -47,6 +47,6 @@ public interface IAlgoliaIndexingService
/// <remarks>Logs errors if there are issues initializing the <see cref="IAlgoliaConnection"/>.</remarks>
/// <param name="items">The items to process.</param>
/// <returns>The number of items processed.</returns>
public int ProcessAlgoliaTasks(IEnumerable<AlgoliaQueueItem> items);
int ProcessAlgoliaTasks(IEnumerable<AlgoliaQueueItem> items);
}
}
16 changes: 8 additions & 8 deletions src/Services/IAlgoliaInsightsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IAlgoliaInsightsService
/// </summary>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogSearchResultClicked(string eventName, string indexName);
Task<InsightsResponse> LogSearchResultClicked(string eventName, string indexName);


/// <summary>
Expand All @@ -32,7 +32,7 @@ public interface IAlgoliaInsightsService
/// </summary>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogSearchResultConversion(string conversionName, string indexName);
Task<InsightsResponse> LogSearchResultConversion(string conversionName, string indexName);


/// <summary>
Expand All @@ -44,7 +44,7 @@ public interface IAlgoliaInsightsService
/// <param name="indexName">The code name of the Algolia index.</param>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogPageConversion(int documentId, string conversionName, string indexName);
Task<InsightsResponse> LogPageConversion(int documentId, string conversionName, string indexName);


/// <summary>
Expand All @@ -57,7 +57,7 @@ public interface IAlgoliaInsightsService
/// <param name="indexName">The code name of the Algolia index.</param>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogPageViewed(int documentId, string eventName, string indexName);
Task<InsightsResponse> LogPageViewed(int documentId, string eventName, string indexName);


/// <summary>
Expand All @@ -68,7 +68,7 @@ public interface IAlgoliaInsightsService
/// <param name="indexName">The code name of the Algolia index.</param>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogFacetsViewed(IEnumerable<AlgoliaFacetedAttribute> facets, string eventName, string indexName);
Task<InsightsResponse> LogFacetsViewed(IEnumerable<AlgoliaFacetedAttribute> facets, string eventName, string indexName);


/// <summary>
Expand All @@ -79,7 +79,7 @@ public interface IAlgoliaInsightsService
/// <param name="indexName">The code name of the Algolia index.</param>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogFacetClicked(string facet, string eventName, string indexName);
Task<InsightsResponse> LogFacetClicked(string facet, string eventName, string indexName);


/// <summary>
Expand All @@ -90,7 +90,7 @@ public interface IAlgoliaInsightsService
/// <param name="indexName">The code name of the Algolia index.</param>
/// <returns>The response from Algolia, or null if the request was skipped or an error occurred communicating
/// with Algolia.</returns>
public Task<InsightsResponse> LogFacetConverted(string facet, string conversionName, string indexName);
Task<InsightsResponse> LogFacetConverted(string facet, string conversionName, string indexName);


/// <summary>
Expand All @@ -99,6 +99,6 @@ public interface IAlgoliaInsightsService
/// </summary>
/// <typeparam name="TModel">The type of the Algolia search model.</typeparam>
/// <param name="searchResponse">The full response of an Algolia search.</param>
public void SetInsightsUrls<TModel>(SearchResponse<TModel> searchResponse) where TModel : AlgoliaSearchModel;
void SetInsightsUrls<TModel>(SearchResponse<TModel> searchResponse) where TModel : AlgoliaSearchModel;
}
}
Loading

0 comments on commit 9f1c186

Please sign in to comment.