Skip to content

Commit

Permalink
RavenDB-22410 Search - analyzer produces multiple tokens from a singl…
Browse files Browse the repository at this point in the history
…e words
  • Loading branch information
maciejaszyk authored and arekpalinski committed May 27, 2024
1 parent cc8a4a1 commit 1963e09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Corax/Querying/IndexSearcher.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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<Slice>(SliceComparer.Instance);
for (var i = 0; i < terms.Count; ++i)
{
Expand Down
36 changes: 36 additions & 0 deletions test/SlowTests/Issues/RavenDB_22410.cs
Original file line number Diff line number Diff line change
@@ -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<Dto>()
.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);
}

0 comments on commit 1963e09

Please sign in to comment.