Skip to content

Commit 6b3a6bb

Browse files
committed
Add support for highlight query
Closes #854
1 parent fea53ca commit 6b3a6bb

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/Nest/DSL/Search/HighlightDescriptor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public interface IHighlightRequest
4646

4747
[JsonProperty("boundary_chars")]
4848
string BoundaryChars { get; set; }
49+
50+
[JsonProperty("highlight_query")]
51+
IQueryContainer HighlightQuery { get; set; }
4952
}
5053

5154
public class HighlightRequest : IHighlightRequest
@@ -62,6 +65,7 @@ public class HighlightRequest : IHighlightRequest
6265
public Dictionary<PropertyPathMarker, IHighlightField> Fields { get; set; }
6366
public bool? RequireFieldMatch { get; set; }
6467
public string BoundaryChars { get; set; }
68+
public IQueryContainer HighlightQuery { get; set; }
6569
}
6670

6771
public class HighlightDescriptor<T> : IHighlightRequest
@@ -93,6 +97,8 @@ public class HighlightDescriptor<T> : IHighlightRequest
9397

9498
string IHighlightRequest.BoundaryChars { get; set; }
9599

100+
IQueryContainer IHighlightRequest.HighlightQuery { get; set; }
101+
96102
public HighlightDescriptor<T> OnFields(params Action<HighlightFieldDescriptor<T>>[] fieldHighlighters)
97103
{
98104
fieldHighlighters.ThrowIfEmpty("fieldHighlighters");
@@ -182,5 +188,10 @@ public HighlightDescriptor<T> BoundaryMaxSize(int boundaryMaxSize)
182188
Self.BoundaryMaxSize = boundaryMaxSize;
183189
return this;
184190
}
191+
public HighlightDescriptor<T> HighlightQuery(Func<QueryDescriptor<T>, QueryContainer> query)
192+
{
193+
Self.HighlightQuery = query(new QueryDescriptor<T>());
194+
return this;
195+
}
185196
}
186197
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,53 @@ public void TestHighlightNoNullRef()
8080
Assert.GreaterOrEqual(result.Total, 1);
8181
Assert.AreEqual(result.Highlights.Count(), 0);
8282
}
83+
84+
[Test]
85+
public void TestHighlightQuery()
86+
{
87+
var result = this.Client.Search<ElasticsearchProject>(s => s
88+
.From(0)
89+
.Size(10)
90+
.Query(q => q
91+
.QueryString(qs => qs
92+
.Query("elasticsearch.pm")
93+
)
94+
)
95+
.Highlight(h => h
96+
.HighlightQuery(hq => hq
97+
.Bool(b => b
98+
.Must(m => m
99+
.Match(mm => mm
100+
.OnField(p => p.Name)
101+
.Query("elasticsearch.pm")
102+
)
103+
)
104+
.Should(sh => sh
105+
.MatchPhrase(mp => mp
106+
.OnField(p => p.Name)
107+
.Slop(1)
108+
.Boost(10)
109+
)
110+
)
111+
.MinimumShouldMatch(0)
112+
)
113+
)
114+
.PreTags("<b>")
115+
.PostTags("</b>")
116+
.OnFields(
117+
f => f
118+
.OnField(e => e.Name)
119+
.PreTags("<em>")
120+
.PostTags("</em>")
121+
)
122+
)
123+
);
124+
125+
Assert.IsTrue(result.IsValid);
126+
Assert.DoesNotThrow(() => result.Highlights.Count());
127+
Assert.IsNotNull(result.Highlights);
128+
Assert.GreaterOrEqual(result.Total, 2);
129+
Assert.AreEqual(result.Highlights.Count(), 2);
130+
}
83131
}
84132
}

src/Tests/Nest.Tests.Unit/Search/Highlight/HighlightTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public void TestHighlight()
1313
.From(0)
1414
.Size(10)
1515
.Highlight(h => h
16+
.HighlightQuery(hq => hq
17+
.Match(m => m
18+
.OnField(p => p.Name)
19+
.Query("nest")
20+
)
21+
)
1622
.BoundaryCharacters(".,!? \t\n")
1723
.BoundaryMaxSize(20)
1824
.Encoder("html")
@@ -52,7 +58,14 @@ public void TestHighlight()
5258
}
5359
},
5460
require_field_match: true,
55-
boundary_chars: "".,!? \t\n""
61+
boundary_chars: "".,!? \t\n"",
62+
highlight_query: {
63+
match: {
64+
name: {
65+
query: ""nest""
66+
}
67+
}
68+
}
5669
}
5770
}";
5871
Assert.True(json.JsonEquals(expected), json);

0 commit comments

Comments
 (0)