Skip to content

Commit

Permalink
adding a session manager for index writer
Browse files Browse the repository at this point in the history
  • Loading branch information
ssinno28 committed Nov 21, 2023
1 parent 680d86a commit 8fbbad5
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 60 deletions.
21 changes: 17 additions & 4 deletions Lucene.Net.IndexProvider.Tests/IndexProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Lucene.Net.Index;
using Lucene.Net.IndexProvider.Helpers;
using Lucene.Net.IndexProvider.Interfaces;
using Lucene.Net.IndexProvider.Models;
using Lucene.Net.IndexProvider.Tests.Models;
using Lucene.Net.Search;
using Lucene.Net.Util;
Expand Down Expand Up @@ -175,9 +176,9 @@ await _indexProvider.Search()
var upperDateString = dateTimeOffset.ToString("yyyyMMddHHmmss.fffzzzzz");
var rangeQuery =
TermRangeQuery.NewStringRange(
nameof(BlogPost.PublishedDateOffset),
dateString,
upperDateString,
nameof(BlogPost.PublishedDateOffset),
dateString,
upperDateString,
true,
true);
Expand Down Expand Up @@ -244,11 +245,21 @@ public async Task InitializeAsync()
var services = new ServiceCollection()
.AddLuceneDocumentMapper()
.AddLuceneProvider()
.AddLogging(x => x.AddConsole());
.AddLogging(x => x.AddConsole())
.AddHttpContextAccessor();

services.Add(new ServiceDescriptor(typeof(ILocalIndexPathFactory), _mockLocalIndexPathFactory.Object));
_serviceProvider = services.BuildServiceProvider();

var configurationManager = _serviceProvider.GetService<IIndexConfigurationManager>();
var sessionManager = _serviceProvider.GetService<IIndexSessionManager>();
configurationManager.AddConfiguration(new LuceneConfig()
{
IndexTypes = new[] { typeof(BlogPost) },
BatchSize = 50000,
LuceneVersion = LuceneVersion.LUCENE_48
});

var dateTime = DateTime.Now;
_indexProvider = _serviceProvider.GetService<IIndexProvider>();
await _indexProvider.CreateIndexIfNotExists(typeof(BlogPost));
Expand Down Expand Up @@ -342,6 +353,8 @@ await _indexProvider.Store(new List<BlogPost>
PublishedDateOffset = dateTimeOffset.AddDays(-10)
}
});

sessionManager.CloseSessionOn(nameof(BlogPost));
}

public async Task DisposeAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Lucene.Net.DocumentMapper" Version="1.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.18.4" />
Expand Down
13 changes: 4 additions & 9 deletions Lucene.Net.IndexProvider/Helpers/ServiceCollectionHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using Lucene.Net.IndexProvider.Interfaces;
using Lucene.Net.IndexProvider.Models;
using Lucene.Net.Search;
using Lucene.Net.Util;
using Lucene.Net.IndexProvider.Managers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Lucene.Net.IndexProvider.Helpers
{
public static class ServiceCollectionHelpers
{
public static IServiceCollection AddLuceneProvider(this IServiceCollection services)
{
var luceneConfig = new LuceneConfig
{
BatchSize = BooleanQuery.MaxClauseCount,
LuceneVersion = LuceneVersion.LUCENE_48
};
services.TryAddSingleton<IIndexSessionManager, IndexSessionManager>();
services.TryAddSingleton<IIndexConfigurationManager, IndexConfigurationManager>();

services.AddSingleton(luceneConfig);
services.AddScoped<IIndexProvider, LuceneIndexProvider>();

return services;
Expand Down
12 changes: 12 additions & 0 deletions Lucene.Net.IndexProvider/Interfaces/IIndexConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using Lucene.Net.Index;
using Lucene.Net.IndexProvider.Models;

namespace Lucene.Net.IndexProvider.Interfaces;

public interface IIndexConfigurationManager
{
List<LuceneConfig> Configurations { get; }
void AddConfiguration(LuceneConfig config);
LuceneConfig GetConfiguration(string indexName);
}
12 changes: 12 additions & 0 deletions Lucene.Net.IndexProvider/Interfaces/IIndexSessionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using Lucene.Net.Index;

namespace Lucene.Net.IndexProvider.Interfaces;

public interface IIndexSessionManager
{
IndexWriter GetSessionFrom(string indexName);
IndexWriter GetTransientSession(string indexName);
void CloseSessionOn(string indexName);
IDictionary<string, IndexWriter> ContextSessions { get; }
}
7 changes: 6 additions & 1 deletion Lucene.Net.IndexProvider/Lucene.Net.IndexProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.DocumentMapper" Version="1.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<Folder Include="Managers\" />
</ItemGroup>

<PropertyGroup>
<Version>1.0.21</Version>
<Version>1.0.22</Version>
<RepositoryUrl>https://github.com/ssinno28/Lucene.Net.IndexProvider</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Description>A simple service that helps to abstract common operations when interacting with lucene.net indexes.</Description>
Expand Down
79 changes: 34 additions & 45 deletions Lucene.Net.IndexProvider/LuceneIndexProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Reflection;
using System.Threading.Tasks;
using Lucene.Net.Analysis.Standard;
Expand All @@ -27,20 +28,23 @@ namespace Lucene.Net.IndexProvider
/// </summary>
public class LuceneIndexProvider : IIndexProvider
{
private readonly LuceneConfig _luceneConfig;
private readonly IDocumentMapper _mapper;
private readonly ILogger<LuceneIndexProvider> _logger;
private readonly ILocalIndexPathFactory _localIndexPathFactory;
private readonly IIndexSessionManager _sessionManager;
private readonly IIndexConfigurationManager _configurationManager;

public LuceneIndexProvider(
LuceneConfig luceneConfig,
IDocumentMapper mapper,
ILoggerFactory loggerFactory,
ILocalIndexPathFactory localIndexPathFactory)
ILocalIndexPathFactory localIndexPathFactory,
IIndexSessionManager sessionManager,
IIndexConfigurationManager configurationManager)
{
_luceneConfig = luceneConfig;
_mapper = mapper;
_localIndexPathFactory = localIndexPathFactory;
_sessionManager = sessionManager;
_configurationManager = configurationManager;
_logger = loggerFactory.CreateLogger<LuceneIndexProvider>();

// Ensures the directory exists
Expand Down Expand Up @@ -76,6 +80,7 @@ public IndexResult<object> GetDocumentById(Type contentType, string id)
{
var directory = GetDirectory(contentType.Name);
IndexResult<object> indexResult = null;
var luceneConfig = _configurationManager.GetConfiguration(contentType.Name);

try
{
Expand All @@ -84,7 +89,7 @@ public IndexResult<object> GetDocumentById(Type contentType, string id)
var indexSearcher = new IndexSearcher(indexReader);

var query = new TermQuery(new Term(GetKeyName(contentType), id));
var hits = indexSearcher.Search(query, _luceneConfig.BatchSize);
var hits = indexSearcher.Search(query, luceneConfig.BatchSize);

if (hits.ScoreDocs.Length == 0)
{
Expand Down Expand Up @@ -228,6 +233,7 @@ public async Task<IndexListResult> GetByFilters(IList<IndexFilter> filters, ILis
{
var directory = GetDirectory(contentType.Name);
List<IndexResult<object>> indexResults = new List<IndexResult<object>>();
var luceneConfig = _configurationManager.GetConfiguration(contentType.Name);

return await Task.Run(() =>
{
Expand Down Expand Up @@ -257,7 +263,7 @@ public async Task<IndexListResult> GetByFilters(IList<IndexFilter> filters, ILis
sort.SetSort(SortField.FIELD_SCORE);
}
var hits = indexSearcher.Search(query, _luceneConfig.BatchSize, sort);
var hits = indexSearcher.Search(query, luceneConfig.BatchSize, sort);
count = hits.TotalHits;
maxScore = hits.MaxScore;
Expand Down Expand Up @@ -314,19 +320,13 @@ public Task<bool> Update(object contentItem, string id)
{
try
{
using (var analyzer = new StandardAnalyzer(_luceneConfig.LuceneVersion))
{
var config = new IndexWriterConfig(_luceneConfig.LuceneVersion, analyzer);
using (var writer = new IndexWriter(GetDirectory(indexName), config))
{
var doc = _mapper.Map(contentItem);
writer.UpdateDocument(new Term(GetKeyName(contentItem.GetType()), id), doc);
var writer = _sessionManager.GetSessionFrom(indexName);
var doc = _mapper.Map(contentItem);
writer.UpdateDocument(new Term(GetKeyName(contentItem.GetType()), id), doc);
if (writer.HasDeletions())
{
writer.ForceMergeDeletes();
}
}
if (writer.HasDeletions())
{
writer.ForceMergeDeletes();
}
return true;
Expand Down Expand Up @@ -356,14 +356,15 @@ public Task CreateIndexIfNotExists(Type contentType)
/// <returns></returns>
public Task CreateIndexIfNotExists(string indexName)
{
var luceneConfig = _configurationManager.GetConfiguration(indexName);
bool exists = DirectoryReader.IndexExists(GetDirectory(indexName));
if (exists) return Task.FromResult(0);

return Task.Run(() =>
{
using (var analyzer = new StandardAnalyzer(_luceneConfig.LuceneVersion))
using (var analyzer = new StandardAnalyzer(luceneConfig.LuceneVersion))
{
var config = new IndexWriterConfig(_luceneConfig.LuceneVersion, analyzer);
var config = new IndexWriterConfig(luceneConfig.LuceneVersion, analyzer);
using (new IndexWriter(GetDirectory(indexName), config))
{
}
Expand Down Expand Up @@ -420,23 +421,17 @@ public Task Store(IList<object> contentItems, string indexName)

return Task.Run(() =>
{
using (var analyzer = new StandardAnalyzer(_luceneConfig.LuceneVersion))
var writer = _sessionManager.GetSessionFrom(indexName);
foreach (var contentItem in contentItems)
{
var config = new IndexWriterConfig(_luceneConfig.LuceneVersion, analyzer);
using (var writer = new IndexWriter(GetDirectory(indexName), config))
try
{
foreach (var contentItem in contentItems)
{
try
{
var doc = _mapper.Map(contentItem);
writer.AddDocument(doc);
}
catch (Exception e)
{
_logger.LogError(e, $"Could not add document to index");
}
}
var doc = _mapper.Map(contentItem);
writer.AddDocument(doc);
}
catch (Exception e)
{
_logger.LogError(e, $"Could not add document to index");
}
}
});
Expand Down Expand Up @@ -474,17 +469,11 @@ public Task Delete(Type contentType, string documentId)
{
return Task.Run(() =>
{
using (var analyzer = new StandardAnalyzer(_luceneConfig.LuceneVersion))
var writer = _sessionManager.GetSessionFrom(contentType.Name);
writer.DeleteDocuments(new Term(GetKeyName(contentType), documentId));
if (writer.HasDeletions())
{
var config = new IndexWriterConfig(_luceneConfig.LuceneVersion, analyzer);
using (var writer = new IndexWriter(GetDirectory(contentType.Name), config))
{
writer.DeleteDocuments(new Term(GetKeyName(contentType), documentId));
if (writer.HasDeletions())
{
writer.ForceMergeDeletes();
}
}
writer.ForceMergeDeletes();
}
});
}
Expand Down
41 changes: 41 additions & 0 deletions Lucene.Net.IndexProvider/Managers/IndexConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using Lucene.Net.IndexProvider.Interfaces;
using Lucene.Net.IndexProvider.Models;

namespace Lucene.Net.IndexProvider.Managers;

public class IndexConfigurationManager : IIndexConfigurationManager
{
private readonly Lazy<List<LuceneConfig>> _configurations =
new Lazy<List<LuceneConfig>>(() => new List<LuceneConfig>());

private readonly ILoggerFactory _loggerFactory;

public IndexConfigurationManager(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
}

private ILogger Logger => CreateLogger<IndexConfigurationManager>();

private ILogger CreateLogger<T>()
{
return _loggerFactory.CreateLogger<IndexConfigurationManager>();
}

public List<LuceneConfig> Configurations => _configurations.Value;

public void AddConfiguration(LuceneConfig config)
{
Configurations.Add(config);
}

public LuceneConfig GetConfiguration(string indexName)
{
var config = Configurations.First(x => x.IndexTypes.Any(t => t.Name.Equals(indexName)));
return config;
}
}
Loading

0 comments on commit 8fbbad5

Please sign in to comment.