Skip to content

Commit ad619f0

Browse files
committed
Add matched_fields to field highlighting options
1 parent a95f597 commit ad619f0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Nest/DSL/Search/HighlightFieldDescriptor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public interface IHighlightField
5454

5555
[JsonProperty("force_source")]
5656
bool? ForceSource { get; set; }
57+
58+
[JsonProperty("matched_fields")]
59+
IEnumerable<PropertyPathMarker> MatchedFields { get; set; }
5760
}
5861

5962
public class HighlightField : IHighlightField
@@ -73,6 +76,7 @@ public class HighlightField : IHighlightField
7376
public string BoundaryChars { get; set; }
7477
public string Type { get; set; }
7578
public bool? ForceSource { get; set; }
79+
public IEnumerable<PropertyPathMarker> MatchedFields { get; set; }
7680
}
7781

7882
public class HighlightFieldDescriptor<T> : IHighlightField where T : class
@@ -108,6 +112,8 @@ public class HighlightFieldDescriptor<T> : IHighlightField where T : class
108112
string IHighlightField.Type { get; set; }
109113

110114
bool? IHighlightField.ForceSource { get; set; }
115+
116+
IEnumerable<PropertyPathMarker> IHighlightField.MatchedFields { get; set; }
111117

112118
public HighlightFieldDescriptor<T> OnField(string field)
113119
{
@@ -208,5 +214,15 @@ public HighlightFieldDescriptor<T> BoundaryMaxSize(int boundaryMaxSize)
208214
Self.BoundaryMaxSize = boundaryMaxSize;
209215
return this;
210216
}
217+
public HighlightFieldDescriptor<T> MatchedFields(IEnumerable<string> fields)
218+
{
219+
Self.MatchedFields = fields.Select(f => (PropertyPathMarker)f);
220+
return this;
221+
}
222+
public HighlightFieldDescriptor<T> MatchedFields(params Expression<Func<T, object>>[] objectPaths)
223+
{
224+
Self.MatchedFields = objectPaths.Select(f => (PropertyPathMarker)f);
225+
return this;
226+
}
211227
}
212228
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ public void TestHighlight()
3232
.OnFields(
3333
f => f
3434
.OnAll()
35-
.NoMatchSize(200)
35+
.NoMatchSize(200)
3636
.PreTags("<em>")
3737
.PostTags("</em>")
38-
.Type(HighlighterType.Plain)
38+
.Type(HighlighterType.Plain),
39+
f => f
40+
.OnField(p => p.Name)
41+
.Type(HighlighterType.Postings)
42+
.MatchedFields(mf => mf.Country, mf => mf.Content)
3943
)
4044
);
4145
var json = TestElasticClient.Serialize(s);
@@ -57,6 +61,10 @@ public void TestHighlight()
5761
post_tags: [""</em>""],
5862
no_match_size: 200,
5963
type: ""plain""
64+
},
65+
name: {
66+
type: ""postings"",
67+
matched_fields: [ ""country"", ""content"" ]
6068
}
6169
},
6270
require_field_match: true,

0 commit comments

Comments
 (0)