From 6378213804b389c0a4bc309b963c0bf874a1bc49 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 4 Sep 2024 10:06:09 -0600 Subject: [PATCH] Ensure we try to refresh when NRT is not enabled. --- src/Examine.Lucene/Providers/LuceneIndex.cs | 4 ++-- .../Providers/LuceneSearcher.cs | 24 ++++++++++++------- src/Examine.Lucene/PublicAPI.Unshipped.txt | 4 ++++ src/Examine.Lucene/Search/SearchContext.cs | 24 +++++++++++++++---- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/Examine.Lucene/Providers/LuceneIndex.cs b/src/Examine.Lucene/Providers/LuceneIndex.cs index 36cd9620..6464cfca 100644 --- a/src/Examine.Lucene/Providers/LuceneIndex.cs +++ b/src/Examine.Lucene/Providers/LuceneIndex.cs @@ -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); } /// @@ -1226,7 +1226,7 @@ public void WaitForChanges() } else { - // TODO: MaybeRefresh + _searcher.Value.MaybeRefreshBlocking(); } } } diff --git a/src/Examine.Lucene/Providers/LuceneSearcher.cs b/src/Examine.Lucene/Providers/LuceneSearcher.cs index d285919a..438599a9 100644 --- a/src/Examine.Lucene/Providers/LuceneSearcher.cs +++ b/src/Examine.Lucene/Providers/LuceneSearcher.cs @@ -3,7 +3,6 @@ using Lucene.Net.Search; using Lucene.Net.Analysis; - namespace Examine.Lucene.Providers { @@ -14,16 +13,11 @@ public class LuceneSearcher : BaseLuceneSearcher, IDisposable { private readonly SearcherManager _searcherManager; private readonly FieldValueTypeCollection _fieldValueTypeCollection; + private readonly bool _isNrt; private bool _disposedValue; private volatile ISearchContext _searchContext; - /// - /// Constructor allowing for creating a NRT instance based on a given writer - /// - /// - /// - /// - /// + [Obsolete("Use ctor with all dependencies")] public LuceneSearcher(string name, SearcherManager searcherManager, Analyzer analyzer, FieldValueTypeCollection fieldValueTypeCollection) : base(name, analyzer) { @@ -31,18 +25,30 @@ public LuceneSearcher(string name, SearcherManager searcherManager, Analyzer ana _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) diff --git a/src/Examine.Lucene/PublicAPI.Unshipped.txt b/src/Examine.Lucene/PublicAPI.Unshipped.txt index eb0f445b..667f334a 100644 --- a/src/Examine.Lucene/PublicAPI.Unshipped.txt +++ b/src/Examine.Lucene/PublicAPI.Unshipped.txt @@ -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? \ No newline at end of file diff --git a/src/Examine.Lucene/Search/SearchContext.cs b/src/Examine.Lucene/Search/SearchContext.cs index b4d514b0..126032a0 100644 --- a/src/Examine.Lucene/Search/SearchContext.cs +++ b/src/Examine.Lucene/Search/SearchContext.cs @@ -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 {