diff --git a/src/Corax/Querying/IndexSearcher.Search.cs b/src/Corax/Querying/IndexSearcher.Search.cs index dc98e6c60bc4..fd0100109381 100644 --- a/src/Corax/Querying/IndexSearcher.Search.cs +++ b/src/Corax/Querying/IndexSearcher.Search.cs @@ -214,6 +214,10 @@ private IQueryMatch SearchQueryWithPhraseQuery(FieldMetadata field, IEnumerable< termMatches ??= new(); terms.Clear(); // Clear the terms list. EncodeAndApplyAnalyzerForMultipleTerms(field, word, ref terms); + + //When single term outputs multiple terms we've to jump into phraseQuery + if (terms.Count > 1) + goto PhraseQuery; foreach (var term in terms.GetEnumerator()) { @@ -264,9 +268,10 @@ private IQueryMatch SearchQueryWithPhraseQuery(FieldMetadata field, IEnumerable< //Phrase query terms.Clear(); EncodeAndApplyAnalyzerForMultipleTerms(field, word, ref terms); + if (terms.Count == 0) continue; //sentence contained only stop-words - + PhraseQuery: var hs = new HashSet(SliceComparer.Instance); for (var i = 0; i < terms.Count; ++i) { diff --git a/test/SlowTests/Issues/RavenDB_22410.cs b/test/SlowTests/Issues/RavenDB_22410.cs new file mode 100644 index 000000000000..ad689c586971 --- /dev/null +++ b/test/SlowTests/Issues/RavenDB_22410.cs @@ -0,0 +1,36 @@ +using System.Linq; +using FastTests; +using Raven.Client.Documents; +using Tests.Infrastructure; +using Xunit; +using Xunit.Abstractions; + +namespace SlowTests.Issues; + +public class RavenDB_22410 : RavenTestBase +{ + public RavenDB_22410(ITestOutputHelper output) : base(output) + { + } + + [RavenTheory(RavenTestCategory.Querying)] + [RavenData(SearchEngineMode = RavenSearchEngineMode.All)] + public void SearchMethodWhenWordIsTransformedIntoMultipleTokensWeTreatThemAsPhraseQuery(Options options) + { + using var store = GetDocumentStore(options); + using var session = store.OpenSession(); + session.Store(new Dto("Din ner"), "Dtos/1"); + session.Store(new Dto("Ner din"), "Dtos/2"); + session.SaveChanges(); + + var results = session.Query() + .Customize(x => x.WaitForNonStaleResults()) + .Search(x => x.Search, "din%ner") + .ToList(); + + Assert.Equal(1, results.Count); + Assert.Equal("Dtos/1", results[0].Id); + } + + private record struct Dto(string Search, string Id = null); +}