Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - cleanup index on rebuild #4

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Kentico.Xperience.Lucene/Services/ILuceneIndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ namespace Kentico.Xperience.Lucene.Services;

public interface ILuceneIndexService
{
T UseWriter<T>(LuceneIndex index, Func<IndexWriter, T> useIndexWriter);
T UseWriter<T>(LuceneIndex index, Func<IndexWriter, T> useIndexWriter, OpenMode openMode = OpenMode.CREATE_OR_APPEND);

void ResetIndex(LuceneIndex index);

TResult UseSearcher<TResult>(LuceneIndex index, Func<IndexSearcher, TResult> useIndexSearcher);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@


/// <inheritdoc/>
public async Task<ICollection<LuceneIndexStatisticsViewModel>> GetStatistics(CancellationToken cancellationToken) =>

Check warning on line 67 in src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
IndexStore.Instance.GetAllIndexes().Select(i =>
{
var statistics = luceneIndexService.UseSearcher(i, s => new LuceneIndexStatisticsViewModel()
Expand Down Expand Up @@ -107,7 +107,7 @@
return UpsertRecordsInternal(dataObjects, indexName);
}

private async Task<int> DeleteRecordsInternal(IEnumerable<string> objectIds, string indexName)

Check warning on line 110 in src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var index = IndexStore.Instance.GetIndex(indexName);
if (index != null)
Expand All @@ -134,6 +134,7 @@
{
// Clear statistics cache so listing displays updated data after rebuild
cacheAccessor.Remove(CACHEKEY_STATISTICS);
luceneIndexService.ResetIndex(luceneIndex);

var indexedNodes = new List<TreeNode>();
foreach (var includedPathAttribute in luceneIndex.IncludedPaths)
Expand All @@ -160,7 +161,7 @@
indexedNodes.ForEach(node => LuceneQueueWorker.EnqueueLuceneQueueItem(new LuceneQueueItem(node, LuceneTaskType.CREATE, luceneIndex.IndexName)));
}

private async Task<int> UpsertRecordsInternal(IEnumerable<LuceneSearchModel> dataObjects, string indexName)

Check warning on line 164 in src/Kentico.Xperience.Lucene/Services/Implementations/DefaultLuceneClient.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var index = IndexStore.Instance.GetIndex(indexName);
if (index != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResult>(LuceneIndex index, Func<IndexWriter, TResult> useIndexWriter)
public TResult UseWriter<TResult>(LuceneIndex index, Func<IndexWriter, TResult> useIndexWriter, OpenMode openMode = OpenMode.CREATE_OR_APPEND)
seangwright marked this conversation as resolved.
Show resolved Hide resolved
{
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<TResult>(LuceneIndex index, Func<IndexSearcher, TResult> useIndexSearcher)
{
if (!System.IO.Directory.Exists(index.IndexPath))
Expand Down
Loading