Skip to content

Commit

Permalink
RavenDB-21702 Handle highlightings on null value
Browse files Browse the repository at this point in the history
  • Loading branch information
Lwiel committed Dec 13, 2023
1 parent 6420da2 commit 75c6c56
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public void Setup(IndexQueryServerSide query, DocumentsOperationContext context)
nls = new string[] { t2.Item1.TrimEnd('*').TrimStart('*'), t2.Item2.TrimEnd('*').TrimStart('*') };
break;
case string[] as1:
nls = new string[as1.Length];
for (int i = 0; i < as1.Length; i++)
nls[i] = as1[i].TrimEnd('*').TrimStart('*');
break;
case null:
continue;
default:
throw new NotSupportedException($"The type '{term.Value.Values.GetType().FullName}' is not supported.");
Expand Down Expand Up @@ -317,7 +322,8 @@ public Dictionary<string, Dictionary<string, string[]>> Execute(IndexQueryServer
maxFragments -= ProcessHighlightings(current, fieldDescription, fieldValue, fragments, maxFragments);
}
}
else continue;
else
continue;

if (string.IsNullOrWhiteSpace(fieldDescription.GroupKey) == false)
{
Expand Down
113 changes: 113 additions & 0 deletions test/SlowTests/Issues/RavenDB-21702.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Linq;
using FastTests;
using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;

namespace SlowTests.Issues;

public class RavenDB_21702 : RavenTestBase
{
public RavenDB_21702(ITestOutputHelper output) : base(output)
{
}

[RavenFact(RavenTestCategory.Indexes | RavenTestCategory.Corax)]
public void Test()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
var d1 = new Dto() { Name = "Cool and super long name just so it works in Lucene", City = null, Description = "Some dummy text that's quite long so it works in Lucene" };
var d2 = new Dto() { Name = null, City = null, Description = "Some dummy text that's quite long so it works in Lucene" };

session.Store(d1);
session.Store(d2);

session.SaveChanges();

var coraxIndex = new CoraxDummyIndex();
var luceneIndex = new LuceneDummyIndex();

coraxIndex.Execute(store);
luceneIndex.Execute(store);

Indexes.WaitForIndexing(store);

var coraxResult = session.Query<Dto>(coraxIndex.IndexName)
.Where(x => x.City == null)
.Search(x => x.Name, "Cool")
.Search(x => x.Description, "Some")
.Highlight(x => x.Name, 18, 3, out var coraxNameHighlightings)
.Highlight(x => x.City, 18, 3, out var coraxCityHighlightings)
.Highlight(x => x.Description, 18, 3, out var coraxDescriptionHighlightings)
.ToList();

var luceneResult = session.Query<Dto>(luceneIndex.IndexName)
.Where(x => x.City == null)
.Search(x => x.Name, "Cool")
.Search(x => x.Description, "Some")
.Highlight(x => x.Name, 18, 3, out var luceneNameHighlightings)
.Highlight(x => x.City, 18, 3, out var luceneCityHighlightings)
.Highlight(x => x.Description, 18, 3, out var luceneDescriptionHighlightings)
.ToList();

Assert.Equal(coraxNameHighlightings.ResultIndents.Count(), 1);
Assert.Equal(coraxCityHighlightings.ResultIndents.Count(), 0);
Assert.Equal(coraxDescriptionHighlightings.ResultIndents.Count(), 2);

Assert.Equal(luceneNameHighlightings.ResultIndents.Count(), 1);
Assert.Equal(luceneCityHighlightings.ResultIndents.Count(), 0);
Assert.Equal(luceneDescriptionHighlightings.ResultIndents.Count(), 2);
}
}
}

private class Dto
{
public string Name { get; set; }
public string City { get; set; }
public string Description { get; set; }
}

private class CoraxDummyIndex : AbstractIndexCreationTask<Dto>
{
public CoraxDummyIndex()
{
Map = dtos => from dto in dtos
select new { dto.Name, dto.City, dto.Description };

SearchEngineType = Raven.Client.Documents.Indexes.SearchEngineType.Corax;

Store(dto => dto.Name, FieldStorage.Yes);
Index(dto => dto.Name, FieldIndexing.Search);
TermVector(dto => dto.Name, FieldTermVector.WithPositionsAndOffsets);

Store(dto => dto.Description, FieldStorage.Yes);
Index(dto => dto.Description, FieldIndexing.Search);
TermVector(dto => dto.Description, FieldTermVector.WithPositionsAndOffsets);
}
}

private class LuceneDummyIndex : AbstractIndexCreationTask<Dto>
{
public LuceneDummyIndex()
{
Map = dtos => from dto in dtos
select new { dto.Name, dto.City, dto.Description };

SearchEngineType = Raven.Client.Documents.Indexes.SearchEngineType.Lucene;

Store(dto => dto.Name, FieldStorage.Yes);
Index(dto => dto.Name, FieldIndexing.Search);
TermVector(dto => dto.Name, FieldTermVector.WithPositionsAndOffsets);

Store(dto => dto.Description, FieldStorage.Yes);
Index(dto => dto.Description, FieldIndexing.Search);
TermVector(dto => dto.Description, FieldTermVector.WithPositionsAndOffsets);
}
}
}

0 comments on commit 75c6c56

Please sign in to comment.