Skip to content

Commit aadaa6d

Browse files
authored
fix #2119 allow custom per field similarity in the mapping other then default or BM25, our put mapping and create index apis already allow you to specify custom similarities settings (#2249)
1 parent dcb56f9 commit aadaa6d

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ public abstract class ElasticsearchPropertyAttributeBase : Attribute, IProperty,
1818
bool? IProperty.Store { get; set; }
1919
bool? IProperty.DocValues { get; set; }
2020
IProperties IProperty.Fields { get; set; }
21-
SimilarityOption? IProperty.Similarity { get; set; }
21+
22+
#pragma warning disable CS0618 // Type or member is obsolete
23+
SimilarityOption? IProperty.Similarity { get { return Self.CustomSimilarity?.ToEnum<SimilarityOption>(); } set { Self.CustomSimilarity = value.GetStringValue(); } }
24+
#pragma warning restore CS0618 // Type or member is obsolete
25+
string IProperty.CustomSimilarity { get; set; }
2226
Fields IProperty.CopyTo { get; set; }
2327

2428
public string Name { get; set; }
2529
public bool Ignore { get; set; }
2630
public bool DocValues { get { return Self.DocValues.GetValueOrDefault(); } set { Self.DocValues = value; } }
2731
public string IndexName { get { return Self.IndexName; } set { Self.IndexName = value; } }
2832
public SimilarityOption Similarity { get { return Self.Similarity.GetValueOrDefault(); } set { Self.Similarity = value; } }
33+
34+
#pragma warning disable CS0618 // Type or member is obsolete
35+
[Obsolete("This is a temporary binary backwards compatible hack to make sure you can specify named similarities in 2.x, scheduled for removal in 5.0")]
36+
public string CustomSimilarity { get { return Self.CustomSimilarity; } set { Self.CustomSimilarity = value; } }
37+
#pragma warning restore CS0618 // Type or member is obsolete
2938
public bool Store { get { return Self.Store.GetValueOrDefault(); } set { Self.Store = value; } }
3039

3140
protected ElasticsearchPropertyAttributeBase(string typeName)

src/Nest/Mapping/Types/PropertyBase.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using Newtonsoft.Json;
34

45
namespace Nest
@@ -24,9 +25,12 @@ public interface IProperty : IFieldMapping
2425
[JsonProperty("fields", DefaultValueHandling = DefaultValueHandling.Ignore)]
2526
IProperties Fields { get; set; }
2627

27-
[JsonProperty("similarity")]
2828
SimilarityOption? Similarity { get; set; }
2929

30+
[JsonProperty("similarity")]
31+
[Obsolete("This is a temporary binary backwards compatible hack to make sure you can specify named similarities in 2.x, scheduled for removal in 5.0")]
32+
string CustomSimilarity { get; set; }
33+
3034
[JsonProperty("copy_to")]
3135
Fields CopyTo { get; set; }
3236

@@ -50,7 +54,8 @@ protected PropertyBase(TypeName typeName)
5054
public bool? DocValues { get; set; }
5155
public IProperties Fields { get; set; }
5256
public string IndexName { get; set; }
53-
public SimilarityOption? Similarity { get; set; }
57+
public SimilarityOption? Similarity { get { return this.CustomSimilarity?.ToEnum<SimilarityOption>(); } set { this.CustomSimilarity = value.GetStringValue(); } }
58+
public string CustomSimilarity { get; set; }
5459
public bool? Store { get; set; }
5560
PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; }
5661
}

src/Nest/Mapping/Types/PropertyDescriptorBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
1414
string IProperty.IndexName { get; set; }
1515
bool? IProperty.Store { get; set; }
1616
bool? IProperty.DocValues { get; set; }
17-
SimilarityOption? IProperty.Similarity { get; set; }
17+
18+
#pragma warning disable CS0618 // Type or member is obsolete
19+
SimilarityOption? IProperty.Similarity { get { return Self.CustomSimilarity?.ToEnum<SimilarityOption>(); } set { Self.CustomSimilarity = value.GetStringValue(); } }
20+
#pragma warning restore CS0618 // Type or member is obsolete
21+
string IProperty.CustomSimilarity { get; set; }
22+
1823
Fields IProperty.CopyTo { get; set; }
1924
IProperties IProperty.Fields { get; set; }
2025

@@ -33,6 +38,9 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
3338
public TDescriptor Fields(Func<PropertiesDescriptor<T>, IPromise<IProperties>> selector) => Assign(a => a.Fields = selector?.Invoke(new PropertiesDescriptor<T>())?.Value);
3439

3540
public TDescriptor Similarity(SimilarityOption similarity) => Assign(a => a.Similarity = similarity);
41+
#pragma warning disable CS0618 // Type or member is obsolete
42+
public TDescriptor Similarity(string similarity) => Assign(a => a.CustomSimilarity = similarity);
43+
#pragma warning restore CS0618 // Type or member is obsolete
3644

3745
public TDescriptor CopyTo(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) => Assign(a => a.CopyTo = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
3846
}

src/Tests/Mapping/Types/Core/String/StringMappingTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class StringTest
1919
PositionIncrementGap = 5,
2020
#pragma warning restore 618
2121
SearchAnalyzer = "mysearchanalyzer",
22-
Similarity = SimilarityOption.BM25,
22+
#pragma warning disable CS0618 // Type or member is obsolete
23+
CustomSimilarity = "my_custom_similarity",
24+
#pragma warning restore CS0618 // Type or member is obsolete
2325
Store = true,
2426
TermVector = TermVectorOption.WithPositionsOffsets)]
2527
public string Full { get; set; }
@@ -53,7 +55,7 @@ public class StringMappingTests : TypeMappingTestBase<StringTest>
5355
null_value = "na",
5456
position_increment_gap = 5,
5557
search_analyzer = "mysearchanalyzer",
56-
similarity = "BM25",
58+
similarity = "my_custom_similarity",
5759
store = true,
5860
term_vector = "with_positions_offsets"
5961
},
@@ -89,7 +91,7 @@ public class StringMappingTests : TypeMappingTestBase<StringTest>
8991
.NullValue("na")
9092
.PositionIncrementGap(5)
9193
.SearchAnalyzer("mysearchanalyzer")
92-
.Similarity(SimilarityOption.BM25)
94+
.Similarity("my_custom_similarity")
9395
.Store(true)
9496
.TermVector(TermVectorOption.WithPositionsOffsets)
9597
)

0 commit comments

Comments
 (0)