diff --git a/src/Kentico.Xperience.Lucene/Services/ILuceneIndexService.cs b/src/Kentico.Xperience.Lucene/Services/ILuceneIndexService.cs index ce5661f..eefd9a8 100644 --- a/src/Kentico.Xperience.Lucene/Services/ILuceneIndexService.cs +++ b/src/Kentico.Xperience.Lucene/Services/ILuceneIndexService.cs @@ -6,7 +6,9 @@ namespace Kentico.Xperience.Lucene.Services; public interface ILuceneIndexService { - T UseWriter(LuceneIndex index, Func useIndexWriter); + T UseWriter(LuceneIndex index, Func useIndexWriter, OpenMode openMode = OpenMode.CREATE_OR_APPEND); + + void ResetIndex(LuceneIndex index); TResult UseSearcher(LuceneIndex index, Func useIndexSearcher); } diff --git a/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs b/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs index 9a78da2..1a4c3f5 100644 --- a/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs +++ b/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs @@ -134,6 +134,7 @@ private async Task RebuildInternal(LuceneIndex luceneIndex, CancellationToken? c { // Clear statistics cache so listing displays updated data after rebuild cacheAccessor.Remove(CACHEKEY_STATISTICS); + luceneIndexService.ResetIndex(luceneIndex); var indexedNodes = new List(); foreach (var includedPathAttribute in luceneIndex.IncludedPaths) diff --git a/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneIndexService.cs b/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneIndexService.cs index 74e28c0..150e5a3 100644 --- a/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneIndexService.cs +++ b/src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneIndexService.cs @@ -10,21 +10,22 @@ namespace Kentico.Xperience.Lucene.Services.Implementations; public class DefaultLuceneIndexService : ILuceneIndexService { private const LuceneVersion LUCENE_VERSION = LuceneVersion.LUCENE_48; - - public TResult UseWriter(LuceneIndex index, Func useIndexWriter) + public TResult UseWriter(LuceneIndex index, Func useIndexWriter, OpenMode openMode = OpenMode.CREATE_OR_APPEND) { using LuceneDirectory indexDir = FSDirectory.Open(index.IndexPath); //Create an index writer var indexConfig = new IndexWriterConfig(LUCENE_VERSION, index.Analyzer) { - OpenMode = OpenMode.CREATE_OR_APPEND // create/overwrite index + OpenMode = openMode // create/overwrite index }; using var writer = new IndexWriter(indexDir, indexConfig); return useIndexWriter(writer); } + public void ResetIndex(LuceneIndex index) => UseWriter(index, (IndexWriter writer) => true, OpenMode.CREATE); + public TResult UseSearcher(LuceneIndex index, Func useIndexSearcher) { if (!System.IO.Directory.Exists(index.IndexPath))