From dc0b88658c847412df030bd05a762690e0dbff35 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 26 Sep 2024 09:07:25 -0600 Subject: [PATCH] reverts some last minute changes, ensures multiple searches work when not nrt --- src/Examine.Lucene/Indexing/Int32Type.cs | 3 --- src/Examine.Lucene/Providers/LuceneIndex.cs | 15 ++++++----- src/Examine.Lucene/PublicAPI.Unshipped.txt | 1 + .../Search/LuceneSearchExecutor.cs | 4 +-- .../Search/LuceneSearchQuery.cs | 4 ++- .../Search/LuceneSearchResults.cs | 6 ++--- .../Examine.Lucene/Search/FluentApiTests.cs | 26 +++++++++++++++++++ 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/Examine.Lucene/Indexing/Int32Type.cs b/src/Examine.Lucene/Indexing/Int32Type.cs index cedb7991c..8f8008afa 100644 --- a/src/Examine.Lucene/Indexing/Int32Type.cs +++ b/src/Examine.Lucene/Indexing/Int32Type.cs @@ -7,12 +7,9 @@ namespace Examine.Lucene.Indexing { public class Int32Type : IndexFieldRangeValueType { - private readonly string _docValuesFieldName; - public Int32Type(string fieldName, ILoggerFactory logger, bool store = true) : base(fieldName, logger, store) { - _docValuesFieldName = "dv_" + fieldName; } /// diff --git a/src/Examine.Lucene/Providers/LuceneIndex.cs b/src/Examine.Lucene/Providers/LuceneIndex.cs index 08ebc5008..df9d883be 100644 --- a/src/Examine.Lucene/Providers/LuceneIndex.cs +++ b/src/Examine.Lucene/Providers/LuceneIndex.cs @@ -1078,10 +1078,15 @@ private LuceneSearcher CreateSearcher() }; _nrtReopenThread.Start(); - } - // wait for most recent changes when first creating the searcher - WaitForChanges(); + // wait for most recent changes when first creating the searcher + _nrtReopenThread.WaitForGeneration(_latestGen.Value, 5000); + } + else + { + // wait for most recent changes when first creating the searcher + searcherManager.MaybeRefreshBlocking(); + } return new LuceneSearcher(name + "Searcher", searcherManager, FieldAnalyzer, FieldValueTypeCollection, _options.NrtEnabled); } @@ -1230,10 +1235,6 @@ public void WaitForChanges() _logger.LogDebug("{IndexName} WaitForChanges returned {GenerationFound}", Name, found); } } - else - { - _searcher.Value.MaybeRefreshBlocking(); - } } } diff --git a/src/Examine.Lucene/PublicAPI.Unshipped.txt b/src/Examine.Lucene/PublicAPI.Unshipped.txt index e43c6e4e0..984fad863 100644 --- a/src/Examine.Lucene/PublicAPI.Unshipped.txt +++ b/src/Examine.Lucene/PublicAPI.Unshipped.txt @@ -17,6 +17,7 @@ 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.LuceneSearchResults.LuceneSearchResults(System.Collections.Generic.IReadOnlyCollection results, int totalItemCount, float maxScore, Examine.Lucene.Search.SearchAfterOptions searchAfterOptions) -> 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/LuceneSearchExecutor.cs b/src/Examine.Lucene/Search/LuceneSearchExecutor.cs index 7c873cf77..11e4ac80b 100644 --- a/src/Examine.Lucene/Search/LuceneSearchExecutor.cs +++ b/src/Examine.Lucene/Search/LuceneSearchExecutor.cs @@ -130,13 +130,13 @@ public ISearchResults Execute() // Order by Doc Id for improved perf! // See https://cwiki.apache.org/confluence/display/lucene/ImproveSearchingSpeed - foreach (var scoreDoc in topDocs.ScoreDocs.OrderBy(x => x.Doc)) + foreach (var scoreDoc in topDocs.ScoreDocs/*.OrderBy(x => x.Doc)*/) { var result = GetSearchResult(scoreDoc, topDocs, searcher.IndexSearcher); results.Add(result); } - var searchAfterOptions = scoreDocAfter != null ? GetSearchAfterOptions(topDocs) : null; + var searchAfterOptions = GetSearchAfterOptions(topDocs); float maxScore = topDocs.MaxScore; return new LuceneSearchResults(results, totalItemCount, maxScore, searchAfterOptions); diff --git a/src/Examine.Lucene/Search/LuceneSearchQuery.cs b/src/Examine.Lucene/Search/LuceneSearchQuery.cs index 57f3cf484..8c10eefef 100644 --- a/src/Examine.Lucene/Search/LuceneSearchQuery.cs +++ b/src/Examine.Lucene/Search/LuceneSearchQuery.cs @@ -214,13 +214,15 @@ private ISearchResults Search(QueryOptions options) return EmptySearchResults.Instance; } + // TODO: Use a Filter for category, not a query + // https://cwiki.apache.org/confluence/display/lucene/ImproveSearchingSpeed query = new BooleanQuery { // prefix the category field query as a must { GetFieldInternalQuery(ExamineFieldNames.CategoryFieldName, new ExamineValue(Examineness.Explicit, Category), true), Occur.MUST } }; - // add the ones that we're already existing + // add the ones that were already existing foreach (var c in existingClauses) { query.Add(c); diff --git a/src/Examine.Lucene/Search/LuceneSearchResults.cs b/src/Examine.Lucene/Search/LuceneSearchResults.cs index f945f5403..d98276600 100644 --- a/src/Examine.Lucene/Search/LuceneSearchResults.cs +++ b/src/Examine.Lucene/Search/LuceneSearchResults.cs @@ -2,8 +2,6 @@ using System.Collections; using System.Collections.Generic; -#nullable enable - namespace Examine.Lucene.Search { public class LuceneSearchResults : ILuceneSearchResults @@ -16,7 +14,7 @@ public LuceneSearchResults( IReadOnlyCollection results, int totalItemCount, float maxScore, - SearchAfterOptions? searchAfterOptions) + SearchAfterOptions searchAfterOptions) { _results = results; TotalItemCount = totalItemCount; @@ -32,7 +30,7 @@ public LuceneSearchResults( /// public float MaxScore { get; } - public SearchAfterOptions? SearchAfter { get; } + public SearchAfterOptions SearchAfter { get; } public IEnumerator GetEnumerator() => _results.GetEnumerator(); diff --git a/src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs b/src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs index ecd8550d1..a6f487301 100644 --- a/src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs +++ b/src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs @@ -20,6 +20,32 @@ namespace Examine.Test.Examine.Lucene.Search [TestFixture] public class FluentApiTests : ExamineBaseTest { + [Test] + public void Multiple_Searches() + { + var analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion); + + using (var luceneDir1 = new RandomIdRAMDirectory()) + using (var indexer1 = GetTestIndex(luceneDir1, analyzer, nrtEnabled: false)) + { + indexer1.IndexItem(ValueSet.FromObject("1", "content", new { item1 = "value1", item2 = "The agitated zebras gallop back and forth in short, panicky dashes, then skitter off into the total absolute darkness." })); + + var searcher = indexer1.Searcher; + + var result = searcher.Search("darkness"); + foreach (var r in result) + { + Console.WriteLine($"Id = {r.Id}, Score = {r.Score}"); + } + + result = searcher.Search("total darkness"); + foreach (var r in result) + { + Console.WriteLine($"Id = {r.Id}, Score = {r.Score}"); + } + } + } + [Test] public void Allow_Leading_Wildcards() {