Skip to content

Commit

Permalink
Ensure we try to refresh when NRT is not enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Sep 4, 2024
1 parent 7728971 commit 6378213
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Providers/LuceneIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ private LuceneSearcher CreateSearcher()
// wait for most recent changes when first creating the searcher
WaitForChanges();

return new LuceneSearcher(name + "Searcher", searcherManager, FieldAnalyzer, FieldValueTypeCollection);
return new LuceneSearcher(name + "Searcher", searcherManager, FieldAnalyzer, FieldValueTypeCollection, _options.NrtEnabled);
}

/// <summary>
Expand Down Expand Up @@ -1226,7 +1226,7 @@ public void WaitForChanges()
}
else
{
// TODO: MaybeRefresh
_searcher.Value.MaybeRefreshBlocking();
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/Examine.Lucene/Providers/LuceneSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Lucene.Net.Search;
using Lucene.Net.Analysis;


namespace Examine.Lucene.Providers
{

Expand All @@ -14,35 +13,42 @@ public class LuceneSearcher : BaseLuceneSearcher, IDisposable
{
private readonly SearcherManager _searcherManager;
private readonly FieldValueTypeCollection _fieldValueTypeCollection;
private readonly bool _isNrt;
private bool _disposedValue;
private volatile ISearchContext _searchContext;

/// <summary>
/// Constructor allowing for creating a NRT instance based on a given writer
/// </summary>
/// <param name="name"></param>
/// <param name="writer"></param>
/// <param name="analyzer"></param>
/// <param name="fieldValueTypeCollection"></param>
[Obsolete("Use ctor with all dependencies")]
public LuceneSearcher(string name, SearcherManager searcherManager, Analyzer analyzer, FieldValueTypeCollection fieldValueTypeCollection)
: base(name, analyzer)
{
_searcherManager = searcherManager;
_fieldValueTypeCollection = fieldValueTypeCollection;
}

public LuceneSearcher(string name, SearcherManager searcherManager, Analyzer analyzer, FieldValueTypeCollection fieldValueTypeCollection, bool isNrt)
: base(name, analyzer)
{
_searcherManager = searcherManager;
_fieldValueTypeCollection = fieldValueTypeCollection;
_isNrt = isNrt;
}

public override ISearchContext GetSearchContext()
{
// Don't create a new search context unless something has changed
var isCurrent = _searcherManager.IsSearcherCurrent();
if (_searchContext is null || !isCurrent)
{
_searchContext = new SearchContext(_searcherManager, _fieldValueTypeCollection);
_searchContext = new SearchContext(_searcherManager, _fieldValueTypeCollection, _isNrt);
}

return _searchContext;
}

public bool MaybeRefresh() => _searcherManager.MaybeRefresh();

public void MaybeRefreshBlocking() => _searcherManager.MaybeRefreshBlocking();

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
Expand Down
4 changes: 4 additions & 0 deletions src/Examine.Lucene/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ Examine.Lucene.LuceneIndexOptions.NrtTargetMaxStaleSec.get -> double
Examine.Lucene.LuceneIndexOptions.NrtTargetMaxStaleSec.set -> void
Examine.Lucene.LuceneIndexOptions.NrtTargetMinStaleSec.get -> double
Examine.Lucene.LuceneIndexOptions.NrtTargetMinStaleSec.set -> void
Examine.Lucene.Providers.LuceneSearcher.LuceneSearcher(string name, Lucene.Net.Search.SearcherManager searcherManager, Lucene.Net.Analysis.Analyzer analyzer, Examine.Lucene.FieldValueTypeCollection fieldValueTypeCollection, bool isNrt) -> void
Examine.Lucene.Providers.LuceneSearcher.MaybeRefresh() -> bool
Examine.Lucene.Providers.LuceneSearcher.MaybeRefreshBlocking() -> void
Examine.Lucene.Search.SearchContext.SearchContext(Lucene.Net.Search.SearcherManager searcherManager, Examine.Lucene.FieldValueTypeCollection fieldValueTypeCollection, bool isNrt) -> void
Examine.Lucene.Search.SearcherReference.SearcherReference() -> void
virtual Examine.Lucene.Providers.LuceneIndex.UpdateLuceneDocument(Lucene.Net.Index.Term term, Lucene.Net.Documents.Document doc) -> long?
24 changes: 19 additions & 5 deletions src/Examine.Lucene/Search/SearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@
namespace Examine.Lucene.Search
{

public class SearchContext : ISearchContext
public sealed class SearchContext : ISearchContext
{
private readonly SearcherManager _searcherManager;
private readonly FieldValueTypeCollection _fieldValueTypeCollection;
private readonly bool _isNrt;
private string[] _searchableFields;

[Obsolete("Use ctor with all dependencies")]
public SearchContext(SearcherManager searcherManager, FieldValueTypeCollection fieldValueTypeCollection)
{
_searcherManager = searcherManager;
_fieldValueTypeCollection = fieldValueTypeCollection ?? throw new ArgumentNullException(nameof(fieldValueTypeCollection));
}

public SearchContext(SearcherManager searcherManager, FieldValueTypeCollection fieldValueTypeCollection, bool isNrt)
{
_searcherManager = searcherManager;
_fieldValueTypeCollection = fieldValueTypeCollection ?? throw new ArgumentNullException(nameof(fieldValueTypeCollection));
_isNrt = isNrt;
}

// TODO: Do we want to create a new searcher every time? I think so, but we shouldn't allocate so much
public ISearcherReference GetSearcher() =>
// TODO: Only if NRT is disabled?
//_searcherManager.MaybeRefresh();
new SearcherReference(_searcherManager);
public ISearcherReference GetSearcher()
{
if (!_isNrt)
{
_searcherManager.MaybeRefresh();
}

return new SearcherReference(_searcherManager);
}

public string[] SearchableFields
{
Expand Down

0 comments on commit 6378213

Please sign in to comment.