Skip to content

Commit 5eb670d

Browse files
committed
Improved Highlights API, you can now use results.Highlights[docId][highlightField] or results.DocumentsWithMetadata[N].Highlights
1 parent e923785 commit 5eb670d

File tree

9 files changed

+242
-182
lines changed

9 files changed

+242
-182
lines changed

src/Nest.Tests.Integration/Integration/HighlightTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ public void TestHighlight()
3434
Assert.DoesNotThrow(() => result.Highlights.Count());
3535
Assert.IsNotNull(result.Highlights);
3636
Assert.Greater(result.Highlights.Count(), 0);
37-
Assert.True(result.Highlights.Any(h => h.Highlights != null && h.Highlights.Any() && !string.IsNullOrEmpty(h.Highlights.First())));
37+
Assert.True(result.Highlights.All(h => h.Value != null));
38+
39+
Assert.True(result.Highlights.All(h => h.Value.All(hh => !hh.Value.DocumentId.IsNullOrEmpty())));
40+
41+
var id = result.Documents.First().Id.ToString();
42+
var highlights = result.Highlights[id];
43+
Assert.NotNull(highlights);
44+
Assert.NotNull(highlights["content"]);
45+
Assert.Greater(highlights["content"].Highlights.Count(), 0);
3846
}
3947

4048
[Test]

src/Nest.Tests.Integration/Search/QueryResponseMapperTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ public void QueryWithHighlightTest()
530530
var queryResults = this._client.SearchRaw<ElasticSearchProject>(query);
531531

532532
//assert
533-
Assert.IsTrue(queryResults.DocumentsWithMetaData.First().Highlight["content"].Count > 0);
533+
Assert.IsTrue(queryResults.DocumentsWithMetaData.First().Highlight["content"].Highlights.Count() > 0);
534+
534535
}
535536
}
536537
}

src/Nest/Domain/Hit/Highlight.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System;
1+
using System.Collections;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Text;
54

65
namespace Nest
76
{
87
public class Highlight
98
{
9+
public string DocumentId { get; internal set; }
1010
public string Field { get; internal set; }
11-
public IEnumerable<string> Highlights { get; set; }
11+
public IEnumerable<string> Highlights { get; set; }
1212
}
1313
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Windows.Forms;
5+
6+
namespace Nest
7+
{
8+
public class HighlightFieldDictionary : Dictionary<string, Highlight>
9+
{
10+
public HighlightFieldDictionary(IDictionary<string, Highlight> dictionary = null)
11+
{
12+
if (dictionary == null)
13+
return;
14+
foreach(var kv in dictionary)
15+
{
16+
this.Add(kv.Key, kv.Value);
17+
}
18+
}
19+
}
20+
public class HighlightDocumentDictionary : Dictionary<string, HighlightFieldDictionary>
21+
{
22+
23+
}
24+
}

src/Nest/Domain/Hit/Hit.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IHit<out T> where T : class
1919

2020
IEnumerable<object> Sorts { get; }
2121

22-
Dictionary<string, List<string>> Highlight { get; }
22+
HighlightFieldDictionary Highlight { get; }
2323
Explanation Explanation { get; }
2424
ICovariantDictionary<T> PartialFields { get; }
2525
}
@@ -47,10 +47,30 @@ public class Hit<T> : IHit<T>
4747
public IEnumerable<object> Sorts { get; internal set; }
4848

4949
[JsonProperty(PropertyName = "highlight")]
50-
public Dictionary<string, List<string>> Highlight { get; internal set; }
51-
[JsonProperty(PropertyName = "_explanation")]
52-
public Explanation Explanation { get; internal set; }
50+
internal Dictionary<string, List<string>> _Highlight { get; set; }
5351

52+
public HighlightFieldDictionary Highlight
53+
{
54+
get
55+
{
56+
if (_Highlight == null)
57+
return new HighlightFieldDictionary();
58+
59+
var highlights = _Highlight.Select(kv => new Highlight
60+
{
61+
DocumentId = this.Id,
62+
Field = kv.Key,
63+
Highlights = kv.Value
64+
}).ToDictionary(k=>k.Field, v=>v);
65+
66+
return new HighlightFieldDictionary(highlights);
67+
}
68+
}
69+
70+
[JsonProperty(PropertyName = "_explanation")]
71+
public Explanation Explanation { get; internal set; }
72+
73+
5474
public ICovariantDictionary<T> PartialFields { get; internal set; }
5575

5676
public Hit()

0 commit comments

Comments
 (0)