Skip to content

Commit a3b4fc5

Browse files
authored
fix #2125 bwc fix to support specifying any highlighter as string not just the three buildin ones (#2252)
1 parent aadaa6d commit a3b4fc5

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/Nest/CommonAbstractions/Extensions/ExceptionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Nest
44
{
5-
internal static class ExceptionExtensions
6-
{
5+
internal static class ExceptionExtensions
6+
{
77
internal static T ThrowWhen<T>(this T @object, Func<T, bool> predicate, string exceptionMessage)
88
{
99
var x = predicate?.Invoke(@object);
@@ -12,5 +12,5 @@ internal static T ThrowWhen<T>(this T @object, Func<T, bool> predicate, string e
1212

1313
return @object;
1414
}
15-
}
15+
}
1616
}

src/Nest/Search/Search/Highlighting/HighlightField.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ public interface IHighlightField
4747
[JsonProperty("boundary_chars")]
4848
string BoundaryChars { get; set; }
4949

50-
[JsonProperty("type")]
5150
HighlighterType? Type { get; set; }
5251

52+
[JsonProperty("type")]
53+
[Obsolete("This is a temporary binary backwards compatible hack to make sure you can specify any custom highlightertype in 2.x, scheduled for removal in 5.0")]
54+
string CustomType { get; set; }
55+
5356
[JsonProperty("force_source")]
5457
bool? ForceSource { get; set; }
5558

@@ -75,7 +78,12 @@ public class HighlightField : IHighlightField
7578
public string TagsSchema { get; set; }
7679
public bool? RequireFieldMatch { get; set; }
7780
public string BoundaryChars { get; set; }
78-
public HighlighterType? Type { get; set; }
81+
public HighlighterType? Type
82+
{
83+
get { return this.CustomType.ToEnum<HighlighterType>(); }
84+
set { this.CustomType = value.GetStringValue(); }
85+
}
86+
public string CustomType { get; set; }
7987
public bool? ForceSource { get; set; }
8088
public Fields MatchedFields { get; set; }
8189
public QueryContainer HighlightQuery { get; set; }
@@ -110,7 +118,14 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
110118

111119
string IHighlightField.BoundaryChars { get; set; }
112120

113-
HighlighterType? IHighlightField.Type { get; set; }
121+
HighlighterType? IHighlightField.Type
122+
{
123+
#pragma warning disable CS0618 // Type or member is obsolete
124+
get { return Self.CustomType.ToEnum<HighlighterType>(); }
125+
set { Self.CustomType = value.GetStringValue(); }
126+
#pragma warning restore CS0618 // Type or member is obsolete
127+
}
128+
string IHighlightField.CustomType { get; set; }
114129

115130
bool? IHighlightField.ForceSource { get; set; }
116131

@@ -128,6 +143,9 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
128143
public HighlightFieldDescriptor<T> ForceSource(bool? force = true) => Assign(a => a.ForceSource = force);
129144

130145
public HighlightFieldDescriptor<T> Type(HighlighterType type) => Assign(a => a.Type = type);
146+
#pragma warning disable CS0618 // Type or member is obsolete
147+
public HighlightFieldDescriptor<T> Type(string type) => Assign(a => a.CustomType = type);
148+
#pragma warning restore CS0618 // Type or member is obsolete
131149

132150
public HighlightFieldDescriptor<T> PreTags(string preTags) => Assign(a => a.PreTags = new[] { preTags });
133151

@@ -154,7 +172,7 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
154172
public HighlightFieldDescriptor<T> BoundaryCharacters(string boundaryCharacters) => Assign(a => a.BoundaryChars = boundaryCharacters);
155173

156174
public HighlightFieldDescriptor<T> BoundaryMaxSize(int? boundaryMaxSize) => Assign(a => a.BoundaryMaxSize = boundaryMaxSize);
157-
175+
158176
public HighlightFieldDescriptor<T> MatchedFields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
159177
Assign(a => a.MatchedFields = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
160178

src/Tests/Search/Request/HighlightingUsageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
103103
.Fields(
104104
fs => fs
105105
.Field(p => p.Name.Suffix("standard"))
106-
.Type(HighlighterType.Plain)
106+
.Type("plain")
107107
.ForceSource()
108108
.FragmentSize(150)
109109
.NumberOfFragments(3)
@@ -161,7 +161,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
161161
},
162162
{ "leadDeveloper.firstName", new HighlightField
163163
{
164-
Type = HighlighterType.Fvh,
164+
CustomType = "fvh",
165165
PreTags = new[] { "<name>"},
166166
PostTags = new[] { "</name>"},
167167
HighlightQuery = new MatchQuery

0 commit comments

Comments
 (0)