Skip to content

Commit

Permalink
Create example with rebuild of index via webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Lahvac committed Aug 4, 2023
1 parent cca72c8 commit f23c655
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ dotnet add package Kentico.Xperience.Lucene
- Read the Lucene.NET [introduction](https://lucenenet.apache.org/) or [full documentation](https://lucenenet.apache.org/docs/4.8.0-beta00016/) to explore the core library's APIs and functionality.
- Explore the [Lucene.NET source on GitHub](https://github.com/apache/lucenenet)
## Sample features

### Trigger rebuild of index via webhook
Rebuild of index could be triggered by calling `POST` on webhook `/search/rebuild` with body
```json
{
"indexName": "...",
"secret": "..."
}
```

This could be used to trigger regular reindexing of content via CRON, Windows Task Scheduler or any other external scheduler.

## Contributing

- .NET SDK >= 7.0.109
Expand Down
59 changes: 58 additions & 1 deletion src/Kentico.Xperience.Lucene.Sample/Search/SearchController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using CMS.Core;
using Kentico.Xperience.Lucene;
using Kentico.Xperience.Lucene.Services;
using Microsoft.AspNetCore.Mvc;

namespace DancingGoat.Search;

public class SearchController : Controller
{
// replace with real secret loaded from config
private const string REBUILD_SECRET = "1234567890aaabbbccc";
private readonly DancingGoatSearchService searchService;
private readonly ILuceneClient luceneClient;
private readonly IEventLogService eventLogService;

public SearchController(DancingGoatSearchService searchService) => this.searchService = searchService;
public SearchController(DancingGoatSearchService searchService, ILuceneClient luceneClient, IEventLogService eventLogService)
{
this.searchService = searchService;
this.luceneClient = luceneClient;
this.eventLogService = eventLogService;
}

[HttpGet]
public IActionResult Index(string query, int pageSize = 10, int page = 1)
Expand All @@ -15,6 +27,51 @@ public IActionResult Index(string query, int pageSize = 10, int page = 1)

return View(results);
}


/// <summary>
/// Rebuild of index could be initialized by HTTP POST request to url [webroot]/search/rebuild with body
/// <code>
/// {
/// "indexName": "...",
/// "secret": "..."
/// }
/// </code>
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> Rebuild([FromBody] RebuildSearchIndexRequest request)
{
try
{
if (request.Secret != REBUILD_SECRET)
{
return Unauthorized("Invalid Secret");
}

if (string.IsNullOrWhiteSpace(request.IndexName))
{
return NotFound($"IndexName is required");
}

var index = IndexStore.Instance.GetIndex(DancingGoatSearchModel.IndexName);
if (index == null)
{
return NotFound($"Index not found: {request.IndexName}");
}

await luceneClient.Rebuild(index.IndexName, null);
return Ok("Index rebuild started");
}
catch (Exception ex)
{
eventLogService.LogException(nameof(SearchController), nameof(Rebuild), ex, 0, $"IndexName: {request.IndexName}");
return Problem("Index rebuild failed");
}

}
}

public record SearchRequest(string Query = "", int PageSize = 20, int Page = 1);
public record RebuildSearchIndexRequest(string IndexName, string Secret);
2 changes: 1 addition & 1 deletion src/Kentico.Xperience.Lucene/Services/ILuceneClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ public interface ILuceneClient
/// <exception cref="ArgumentNullException" />
/// <exception cref="OperationCanceledException" />
/// <exception cref="ObjectDisposedException" />
Task Rebuild(string indexName, CancellationToken cancellationToken);
Task Rebuild(string indexName, CancellationToken? cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<ICollection<LuceneIndexStatisticsViewModel>> GetStatistics(Can


/// <inheritdoc />
public Task Rebuild(string indexName, CancellationToken cancellationToken)
public Task Rebuild(string indexName, CancellationToken? cancellationToken)
{
if (string.IsNullOrEmpty(indexName))
{
Expand Down Expand Up @@ -130,7 +130,7 @@ private async Task<int> DeleteRecordsInternal(IEnumerable<string> objectIds, str
}


private async Task RebuildInternal(LuceneIndex luceneIndex, CancellationToken cancellationToken)
private async Task RebuildInternal(LuceneIndex luceneIndex, CancellationToken? cancellationToken)
{
// Clear statistics cache so listing displays updated data after rebuild
cacheAccessor.Remove(CACHEKEY_STATISTICS);
Expand Down

0 comments on commit f23c655

Please sign in to comment.