Skip to content

Commit 7e2f1b7

Browse files
authored
Merge pull request #428 from Shazwazza/backport/fix-searchable-fields-and-commit-race
Backport: Fix SearchableFields caching and CommitNow race condition (#426, #427)
2 parents 557eea9 + 25c2036 commit 7e2f1b7

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/Examine.Lucene/Providers/LuceneIndex.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,8 @@ private int PerformIndexItemsInternal(IEnumerable<ValueSet> valueSets, Cancellat
283283
//this is required to ensure the index is written to during the same thread execution
284284
if (!RunAsync)
285285
{
286-
//commit the changes
286+
//commit the changes (also refreshes NRT reader and fires IndexCommitted)
287287
_committer.CommitNow();
288-
289-
// now force any searcher to be updated.
290-
WaitForChanges();
291288
}
292289
else
293290
{
@@ -515,11 +512,8 @@ private int PerformDeleteFromIndexInternal(IEnumerable<string> itemIds, Cancella
515512
//this is required to ensure the index is written to during the same thread execution
516513
if (!RunAsync)
517514
{
518-
//commit the changes (this will process the deletes too)
515+
//commit the changes (also refreshes NRT reader and fires IndexCommitted)
519516
_committer.CommitNow();
520-
521-
// now force any searcher to be updated.
522-
WaitForChanges();
523517
}
524518
else
525519
{
@@ -794,6 +788,12 @@ public IndexCommiter(LuceneIndex index)
794788
public void CommitNow()
795789
{
796790
_index._writer?.IndexWriter?.Commit();
791+
792+
// Ensure the NRT reader is refreshed before signaling commit completion.
793+
// Without this, consumers reacting to IndexCommitted may search with
794+
// a stale reader that doesn't yet reflect the committed changes.
795+
_index.WaitForChanges();
796+
797797
_index.IndexCommitted?.Invoke(_index, EventArgs.Empty);
798798
}
799799

@@ -864,9 +864,6 @@ private void TimerRelease()
864864
{
865865
//perform the commit
866866
CommitNow();
867-
868-
// after the commit, refresh the searcher
869-
_index.WaitForChanges();
870867
}
871868
catch (Exception e)
872869
{

src/Examine.Lucene/Search/SearchContext.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,18 @@ public string[] SearchableFields
5757
.ToList();
5858

5959
//exclude the special index fields
60-
_searchableFields = fields
60+
var filtered = fields
6161
.Where(x => !x.StartsWith(ExamineFieldNames.SpecialFieldPrefix))
6262
.ToArray();
63+
64+
// Only cache non-empty results so that an initially empty index
65+
// will re-read fields once documents have been indexed.
66+
if (filtered.Length > 0)
67+
{
68+
_searchableFields = filtered;
69+
}
70+
71+
return filtered;
6372
}
6473
finally
6574
{

0 commit comments

Comments
 (0)