diff --git a/src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs b/src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs index 305d4961d40..8a62d8a3dd2 100644 --- a/src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs +++ b/src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs @@ -95,7 +95,7 @@ private static IEnumerable TypeWithInterfaces(Type objectType) protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) { // Only serialize explicitly implemented IProperty properties on attribute types - if (typeof(ElasticsearchPropertyAttributeBase).IsAssignableFrom(type)) + if (typeof(PropertyBase).IsAssignableFrom(type) || typeof(ElasticsearchPropertyAttributeBase).IsAssignableFrom(type)) return PropertiesOfInterface(type, memberSerialization); // Descriptors implement properties explicitly, these are not picked up by default diff --git a/src/Nest/Indices/MappingManagement/GetFieldMapping/FieldMappingJsonConverter.cs b/src/Nest/Indices/MappingManagement/GetFieldMapping/FieldMappingJsonConverter.cs index 3ac3f27fcb9..9afa6905d35 100644 --- a/src/Nest/Indices/MappingManagement/GetFieldMapping/FieldMappingJsonConverter.cs +++ b/src/Nest/Indices/MappingManagement/GetFieldMapping/FieldMappingJsonConverter.cs @@ -22,8 +22,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var r = new Dictionary(); - - JObject o = JObject.Load(reader); + var o = JObject.Load(reader); foreach (var p in o.Properties()) { @@ -46,8 +45,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist } if (mapping == null) continue; - var esType = mapping as IProperty; - if (esType != null) + if (mapping is IProperty esType) esType.Name = name; r.Add(name, mapping); diff --git a/src/Nest/Mapping/AttributeBased/ElasticsearchCorePropertyAttributeBase.cs b/src/Nest/Mapping/AttributeBased/ElasticsearchCorePropertyAttributeBase.cs index 277082e2634..69c4c34bd4b 100644 --- a/src/Nest/Mapping/AttributeBased/ElasticsearchCorePropertyAttributeBase.cs +++ b/src/Nest/Mapping/AttributeBased/ElasticsearchCorePropertyAttributeBase.cs @@ -10,10 +10,6 @@ namespace Nest public abstract class ElasticsearchCorePropertyAttributeBase : ElasticsearchPropertyAttributeBase, ICoreProperty { protected ElasticsearchCorePropertyAttributeBase(FieldType type) : base(type) { } - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchCorePropertyAttributeBase(string typeName) : base(typeName) { } - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchCorePropertyAttributeBase(Type type) : base(type) { } private ICoreProperty Self => this; diff --git a/src/Nest/Mapping/AttributeBased/ElasticsearchDocValuesPropertyAttributeBase.cs b/src/Nest/Mapping/AttributeBased/ElasticsearchDocValuesPropertyAttributeBase.cs index e1037662b57..e12dc02dc6c 100644 --- a/src/Nest/Mapping/AttributeBased/ElasticsearchDocValuesPropertyAttributeBase.cs +++ b/src/Nest/Mapping/AttributeBased/ElasticsearchDocValuesPropertyAttributeBase.cs @@ -12,17 +12,14 @@ public abstract class ElasticsearchDocValuesPropertyAttributeBase : Elasticsearc bool? IDocValuesProperty.DocValues { get; set; } - public bool DocValues { get { return Self.DocValues.GetValueOrDefault(true); } set { Self.DocValues = value; } } - + public bool DocValues + { + get => Self.DocValues.GetValueOrDefault(true); + set => Self.DocValues = value; + } protected ElasticsearchDocValuesPropertyAttributeBase(FieldType type) : base(type) { } - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchDocValuesPropertyAttributeBase(string typeName) : base(typeName) { } - - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchDocValuesPropertyAttributeBase(Type type) : base(type) { } - public new static ElasticsearchDocValuesPropertyAttributeBase From(MemberInfo memberInfo) { return memberInfo.GetCustomAttribute(true); diff --git a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs index c9c5d3f4da2..9c37466c423 100644 --- a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs +++ b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs @@ -14,7 +14,7 @@ public abstract class ElasticsearchPropertyAttributeBase : Attribute, IProperty, private IProperty Self => this; PropertyName IProperty.Name { get; set; } - TypeName IProperty.Type { get; set; } + string IProperty.Type { get; set; } IDictionary IProperty.LocalMetadata { get; set; } public string Name { get; set; } @@ -25,18 +25,6 @@ protected ElasticsearchPropertyAttributeBase(FieldType type) Self.Type = type.GetStringValue(); } - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchPropertyAttributeBase(string typeName) - { - Self.Type = typeName; - } - - [Obsolete("Please use overload taking FieldType")] - protected ElasticsearchPropertyAttributeBase(Type type) - { - Self.Type = type; - } - public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo) { return memberInfo.GetCustomAttribute(true); diff --git a/src/Nest/Mapping/DynamicTemplate/SingleMapping.cs b/src/Nest/Mapping/DynamicTemplate/SingleMapping.cs index 403bbc1c355..7a7809e1186 100644 --- a/src/Nest/Mapping/DynamicTemplate/SingleMapping.cs +++ b/src/Nest/Mapping/DynamicTemplate/SingleMapping.cs @@ -4,6 +4,7 @@ namespace Nest { + //TODO make this implement SelectorBase when that PR is moved public class SingleMappingDescriptor : DescriptorBase, IPropertiesDescriptor>, IPropertiesDescriptor where T : class diff --git a/src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs b/src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs index 54e75091550..dbc59435297 100644 --- a/src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs +++ b/src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs @@ -2,14 +2,14 @@ { public class NestedAttribute : ObjectAttribute, INestedProperty { - INestedProperty Self => this; + private INestedProperty Self => this; bool? INestedProperty.IncludeInParent { get; set; } bool? INestedProperty.IncludeInRoot { get; set; } - public bool IncludeInParent { get { return Self.IncludeInParent.GetValueOrDefault(); } set { Self.IncludeInParent = value; } } - public bool IncludeInRoot { get { return Self.IncludeInRoot.GetValueOrDefault(); } set { Self.IncludeInRoot = value; } } + public bool IncludeInParent { get => Self.IncludeInParent.GetValueOrDefault(); set => Self.IncludeInParent = value; } + public bool IncludeInRoot { get => Self.IncludeInRoot.GetValueOrDefault(); set => Self.IncludeInRoot = value; } - public NestedAttribute() : base("nested") { } + public NestedAttribute() : base(FieldType.Nested) { } } } diff --git a/src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs b/src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs index d995dbe724c..b282ff2847b 100644 --- a/src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs +++ b/src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs @@ -7,15 +7,12 @@ public class ObjectAttribute : ElasticsearchCorePropertyAttributeBase, IObjectPr private IObjectProperty Self => this; public ObjectAttribute() : base(FieldType.Object) { } -#pragma warning disable 618 - protected ObjectAttribute(string typeName) : base(typeName) { } - protected ObjectAttribute(Type type) : base(type) { } -#pragma warning restore 618 + protected ObjectAttribute(FieldType type) : base(type) { } Union IObjectProperty.Dynamic { get; set; } bool? IObjectProperty.Enabled { get; set; } IProperties IObjectProperty.Properties { get; set; } - public bool Enabled { get { return Self.Enabled.GetValueOrDefault(); } set { Self.Enabled = value; } } + public bool Enabled { get => Self.Enabled.GetValueOrDefault(); set => Self.Enabled = value; } } } diff --git a/src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs b/src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs index 8aa49c74fb8..bba46b2b2b3 100644 --- a/src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs +++ b/src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs @@ -23,12 +23,7 @@ public class ObjectProperty : CorePropertyBase, IObjectProperty { public ObjectProperty() : base(FieldType.Object) { } - [Obsolete("Please use overload taking FieldType")] - protected ObjectProperty(string type) : base(type) { } - -#pragma warning disable 618 - protected ObjectProperty(FieldType type) : this(type.GetStringValue()) { } -#pragma warning restore 618 + protected ObjectProperty(FieldType type) : base(type) { } public Union Dynamic { get; set; } public bool? Enabled { get; set; } @@ -59,24 +54,16 @@ public abstract class ObjectPropertyDescriptorBase(); } -#pragma warning disable 618 - protected ObjectPropertyDescriptorBase(FieldType type) : this(type.GetStringValue()) { } -#pragma warning restore 618 - - public TDescriptor Dynamic(Union dynamic) => - Assign(a => a.Dynamic = dynamic); + public TDescriptor Dynamic(Union dynamic) => Assign(a => a.Dynamic = dynamic); - public TDescriptor Dynamic(bool dynamic = true) => - Assign(a => a.Dynamic = dynamic); + public TDescriptor Dynamic(bool dynamic = true) => Assign(a => a.Dynamic = dynamic); - public TDescriptor Enabled(bool enabled = true) => - Assign(a => a.Enabled = enabled); + public TDescriptor Enabled(bool enabled = true) => Assign(a => a.Enabled = enabled); public TDescriptor Properties(Func, IPromise> selector) => Assign(a => a.Properties = selector?.Invoke(new PropertiesDescriptor(a.Properties))?.Value); diff --git a/src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs b/src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs index 43da8b21213..b582e589ecc 100644 --- a/src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs +++ b/src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs @@ -11,10 +11,7 @@ public class NumberAttribute : ElasticsearchDocValuesPropertyAttributeBase, INum private INumberProperty Self => this; public NumberAttribute() : base(FieldType.Float) { } - public NumberAttribute(NumberType type) : base(type.ToFieldType()) { } -#pragma warning disable 618 - protected NumberAttribute(string type) : base(type) { } -#pragma warning restore 618 + protected NumberAttribute(NumberType type) : base(type.ToFieldType()) { } bool? INumberProperty.Index { get; set; } double? INumberProperty.Boost { get; set; } @@ -24,12 +21,12 @@ protected NumberAttribute(string type) : base(type) { } INumericFielddata INumberProperty.Fielddata { get; set; } double? INumberProperty.ScalingFactor { get; set; } - public bool Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } } - public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } } - public double NullValue { get { return Self.NullValue.GetValueOrDefault(); } set { Self.NullValue = value; } } - public bool IgnoreMalformed { get { return Self.IgnoreMalformed.GetValueOrDefault(); } set { Self.IgnoreMalformed = value; } } - public bool Coerce { get { return Self.Coerce.GetValueOrDefault(); } set { Self.Coerce = value; } } - public double ScalingFactor { get { return Self.ScalingFactor.GetValueOrDefault(); } set { Self.ScalingFactor = value; } } + public bool Index { get => Self.Index.GetValueOrDefault(); set => Self.Index = value; } + public double Boost { get => Self.Boost.GetValueOrDefault(); set => Self.Boost = value; } + public double NullValue { get => Self.NullValue.GetValueOrDefault(); set => Self.NullValue = value; } + public bool IgnoreMalformed { get => Self.IgnoreMalformed.GetValueOrDefault(); set => Self.IgnoreMalformed = value; } + public bool Coerce { get => Self.Coerce.GetValueOrDefault(); set => Self.Coerce = value; } + public double ScalingFactor { get => Self.ScalingFactor.GetValueOrDefault(); set => Self.ScalingFactor = value; } } } diff --git a/src/Nest/Mapping/Types/Core/Number/NumberProperty.cs b/src/Nest/Mapping/Types/Core/Number/NumberProperty.cs index 67c0b90b199..9ab1029e452 100644 --- a/src/Nest/Mapping/Types/Core/Number/NumberProperty.cs +++ b/src/Nest/Mapping/Types/Core/Number/NumberProperty.cs @@ -54,8 +54,7 @@ public abstract class NumberPropertyDescriptorBase { protected NumberPropertyDescriptorBase() : base(FieldType.Float) { } - [Obsolete("Please use overload taking FieldType")] - protected NumberPropertyDescriptorBase(string type) : base(type) { } + protected NumberPropertyDescriptorBase(FieldType type) : base(type) { } bool? INumberProperty.Index { get; set; } double? INumberProperty.Boost { get; set; } @@ -83,8 +82,7 @@ public TDescriptor Fielddata(Func public TDescriptor ScalingFactor(double scalingFactor) => Assign(a => a.ScalingFactor = scalingFactor); } - public class NumberPropertyDescriptor - : NumberPropertyDescriptorBase, INumberProperty, T>, INumberProperty + public class NumberPropertyDescriptor : NumberPropertyDescriptorBase, INumberProperty, T> where T : class { } diff --git a/src/Nest/Mapping/Types/Core/String/StringAttribute.cs b/src/Nest/Mapping/Types/Core/String/StringAttribute.cs index 4f03ecf8959..811aeba1c12 100644 --- a/src/Nest/Mapping/Types/Core/String/StringAttribute.cs +++ b/src/Nest/Mapping/Types/Core/String/StringAttribute.cs @@ -19,18 +19,18 @@ public class StringAttribute : ElasticsearchDocValuesPropertyAttributeBase, IStr int? IStringProperty.PositionIncrementGap { get; set; } IStringFielddata IStringProperty.Fielddata { get; set; } - public string Analyzer { get { return Self.Analyzer; } set { Self.Analyzer = value; } } - public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } } - public int IgnoreAbove { get { return Self.IgnoreAbove.GetValueOrDefault(); } set { Self.IgnoreAbove = value; } } - public FieldIndexOption Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } } - public IndexOptions IndexOptions { get { return Self.IndexOptions.GetValueOrDefault(); } set { Self.IndexOptions = value; } } - public string NullValue { get { return Self.NullValue; } set { Self.NullValue = value; } } - public int PositionIncrementGap { get { return Self.PositionIncrementGap.GetValueOrDefault(); } set { Self.PositionIncrementGap = value; } } - public string SearchAnalyzer { get { return Self.SearchAnalyzer; } set { Self.SearchAnalyzer = value; } } - public TermVectorOption TermVector { get { return Self.TermVector.GetValueOrDefault(); } set { Self.TermVector = value; } } - public bool Norms { get { return Self.Norms.GetValueOrDefault(true); } set { Self.Norms = value; } } + public string Analyzer { get => Self.Analyzer; set => Self.Analyzer = value; } + public double Boost { get => Self.Boost.GetValueOrDefault(); set => Self.Boost = value; } + public int IgnoreAbove { get => Self.IgnoreAbove.GetValueOrDefault(); set => Self.IgnoreAbove = value; } + public FieldIndexOption Index { get => Self.Index.GetValueOrDefault(); set => Self.Index = value; } + public IndexOptions IndexOptions { get => Self.IndexOptions.GetValueOrDefault(); set => Self.IndexOptions = value; } + public string NullValue { get => Self.NullValue; set => Self.NullValue = value; } + public int PositionIncrementGap { get => Self.PositionIncrementGap.GetValueOrDefault(); set => Self.PositionIncrementGap = value; } + public string SearchAnalyzer { get => Self.SearchAnalyzer; set => Self.SearchAnalyzer = value; } + public TermVectorOption TermVector { get => Self.TermVector.GetValueOrDefault(); set => Self.TermVector = value; } + public bool Norms { get => Self.Norms.GetValueOrDefault(true); set => Self.Norms = value; } [Obsolete("Only valid for indices created before Elasticsearch 5.0 and will be removed in the next major version. For newly created indices, use Text or Keyword attribute instead.")] - public StringAttribute() : base("string") { } + public StringAttribute() : base(FieldType.String) { } } } diff --git a/src/Nest/Mapping/Types/Core/String/StringProperty.cs b/src/Nest/Mapping/Types/Core/String/StringProperty.cs index ce8555f7018..6fdf4349715 100644 --- a/src/Nest/Mapping/Types/Core/String/StringProperty.cs +++ b/src/Nest/Mapping/Types/Core/String/StringProperty.cs @@ -46,7 +46,7 @@ public interface IStringProperty : IDocValuesProperty [DebuggerDisplay("{DebugDisplay}")] public class StringProperty : DocValuesPropertyBase, IStringProperty { - public StringProperty() : base("string") { } + public StringProperty() : base(FieldType.String) { } public FieldIndexOption? Index { get; set; } public TermVectorOption? TermVector { get; set; } @@ -79,7 +79,7 @@ public class StringPropertyDescriptor int? IStringProperty.PositionIncrementGap { get; set; } IStringFielddata IStringProperty.Fielddata { get; set; } - public StringPropertyDescriptor() : base("string") { } + public StringPropertyDescriptor() : base(FieldType.String) { } /// /// Shortcut into .Index(FieldIndexOption.NotAnalyzed) diff --git a/src/Nest/Mapping/Types/CorePropertyBase.cs b/src/Nest/Mapping/Types/CorePropertyBase.cs index 5e24b7510ea..1791daaaab0 100644 --- a/src/Nest/Mapping/Types/CorePropertyBase.cs +++ b/src/Nest/Mapping/Types/CorePropertyBase.cs @@ -25,8 +25,6 @@ public interface ICoreProperty : IProperty [DebuggerDisplay("{DebugDisplay}")] public abstract class CorePropertyBase : PropertyBase, ICoreProperty { - [Obsolete("Please use overload taking FieldType")] - protected CorePropertyBase(TypeName typeName) : base(typeName) { } protected CorePropertyBase(FieldType type) : base(type) { } diff --git a/src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs b/src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs index ded88156fde..d46f3ee37fe 100644 --- a/src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs @@ -14,12 +14,7 @@ public abstract class CorePropertyDescriptorBase Fields ICoreProperty.CopyTo { get; set; } IProperties ICoreProperty.Fields { get; set; } - [Obsolete("Please use overload taking FieldType")] - protected CorePropertyDescriptorBase(string type) : base(type) {} - -#pragma warning disable 618 - protected CorePropertyDescriptorBase(FieldType type) : this(type.GetStringValue()) {} -#pragma warning restore 618 + protected CorePropertyDescriptorBase(FieldType type) : base(type) {} public TDescriptor Store(bool store = true) => Assign(a => a.Store = store); diff --git a/src/Nest/Mapping/Types/DocValuesPropertyBase.cs b/src/Nest/Mapping/Types/DocValuesPropertyBase.cs index 05e8f21b447..e6a74a18c1b 100644 --- a/src/Nest/Mapping/Types/DocValuesPropertyBase.cs +++ b/src/Nest/Mapping/Types/DocValuesPropertyBase.cs @@ -14,12 +14,7 @@ public interface IDocValuesProperty : ICoreProperty public abstract class DocValuesPropertyBase : CorePropertyBase, IDocValuesProperty { - [Obsolete("Please use overload taking FieldType")] - protected DocValuesPropertyBase(TypeName typeName) : base(typeName) { } - -#pragma warning disable 618 - protected DocValuesPropertyBase(FieldType type) : this(type.GetStringValue()) { } -#pragma warning restore 618 + protected DocValuesPropertyBase(FieldType type) : base(type) { } public bool? DocValues { get; set; } } diff --git a/src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs b/src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs index 49dbaca8e35..b63a6e90289 100644 --- a/src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs @@ -11,8 +11,6 @@ public abstract class DocValuesPropertyDescriptorBase Assign(a => a.DocValues = docValues); diff --git a/src/Nest/Mapping/Types/PropertiesJsonConverter.cs b/src/Nest/Mapping/Types/PropertiesJsonConverter.cs index 58a23cd7a49..c1e08dfd674 100644 --- a/src/Nest/Mapping/Types/PropertiesJsonConverter.cs +++ b/src/Nest/Mapping/Types/PropertiesJsonConverter.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using Elasticsearch.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -19,8 +17,7 @@ internal class PropertiesJsonConverter : JsonConverter public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - var dict = value as IDictionary; - if (dict == null) return; + if (!(value is IDictionary dict)) return; var settings = serializer.GetConnectionSettings(); var props = new Properties(); foreach (var kv in dict) @@ -33,8 +30,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s continue; } // Check against connection settings mappings - IPropertyMapping propertyMapping; - if (settings.PropertyMappings.TryGetValue(propertyInfo, out propertyMapping)) + if (settings.PropertyMappings.TryGetValue(propertyInfo, out var propertyMapping)) { if (propertyMapping.Ignore) continue; props.Add(propertyMapping.Name, kv.Value); diff --git a/src/Nest/Mapping/Types/PropertyBase.cs b/src/Nest/Mapping/Types/PropertyBase.cs index a617d0ac362..805b10053cb 100644 --- a/src/Nest/Mapping/Types/PropertyBase.cs +++ b/src/Nest/Mapping/Types/PropertyBase.cs @@ -14,7 +14,7 @@ public interface IProperty : IFieldMapping PropertyName Name { get; set; } [JsonProperty("type")] - TypeName Type { get; set; } + string Type { get; set; } /// /// Local property metadata that will NOT be stored in Elasticsearch with the mappings @@ -31,25 +31,27 @@ public interface IPropertyWithClrOrigin [DebuggerDisplay("{DebugDisplay}")] public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin { - [Obsolete("Please use overload taking FieldType")] - protected PropertyBase(TypeName typeName) + private string _type; + protected string TypeOverride { - Type = typeName; + get => _type; + set => _type = value; } -#pragma warning disable 618 - protected PropertyBase(FieldType type) : this(type.GetStringValue()) { } -#pragma warning restore 618 + string IProperty.Type { get => _type; set => _type = value; } - protected string DebugDisplay => $"Type: {Type?.DebugDisplay}, Name: {Name?.DebugDisplay} "; + PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; } + + protected PropertyBase(FieldType type) + { + ((IProperty)this).Type = type.GetStringValue(); + } + + protected string DebugDisplay => $"Type: {((IProperty)this).Type ?? ""}, Name: {Name.DebugDisplay} "; public PropertyName Name { get; set; } - public virtual TypeName Type { get; set; } - PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; } - /// - /// Local property metadata that will NOT be stored in Elasticsearch with the mappings - /// + /// public IDictionary LocalMetadata { get; set; } } } diff --git a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs index e207cfe4dff..6d4caab78dd 100644 --- a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs @@ -12,17 +12,14 @@ public abstract class PropertyDescriptorBase where T : class { PropertyName IProperty.Name { get; set; } - TypeName IProperty.Type { get; set; } + private string _type; + protected string TypeOverride { set => _type = value; } + string IProperty.Type { get => _type; set => _type = value; } IDictionary IProperty.LocalMetadata { get; set; } - protected string DebugDisplay => $"Type: {Self.Type.DebugDisplay}, Name: {Self.Name.DebugDisplay} "; + protected string DebugDisplay => $"Type: {Self.Type ?? ""}, Name: {Self.Name.DebugDisplay} "; - [Obsolete("Please use overload taking FieldType")] - protected PropertyDescriptorBase(string type) { Self.Type = type; } - -#pragma warning disable 618 - protected PropertyDescriptorBase(FieldType type) : this(type.GetStringValue()){} -#pragma warning restore 618 + protected PropertyDescriptorBase(FieldType type) { Self.Type = type.GetStringValue(); } public TDescriptor Name(PropertyName name) => Assign(a => a.Name = name); diff --git a/src/Nest/Mapping/Types/PropertyJsonConverter.cs b/src/Nest/Mapping/Types/PropertyJsonConverter.cs index e34de0a58b0..40c6ba42336 100644 --- a/src/Nest/Mapping/Types/PropertyJsonConverter.cs +++ b/src/Nest/Mapping/Types/PropertyJsonConverter.cs @@ -19,13 +19,11 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jObject = JObject.Load(reader); - JToken typeToken; - JToken propertiesToken; var type = FieldType.None; - if (jObject.TryGetValue("type", out typeToken)) + if (jObject.TryGetValue("type", out var typeToken)) type = typeToken.Value().ToEnum().GetValueOrDefault(type); - else if (jObject.TryGetValue("properties", out propertiesToken)) + else if (jObject.TryGetValue("properties", out _)) type = FieldType.Object; switch (type) @@ -47,7 +45,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist case FieldType.ScaledFloat: case FieldType.HalfFloat: var num = jObject.ToObject(); - num.Type = type.GetStringValue(); + ((IProperty)num).Type = type.GetStringValue(); return num; case FieldType.Date: return jObject.ToObject(); diff --git a/src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs b/src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs index 8739906ada9..aa3d8b8fa91 100644 --- a/src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs +++ b/src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs @@ -52,9 +52,7 @@ public interface IGenericProperty : IDocValuesProperty [DebuggerDisplay("{DebugDisplay}")] public class GenericProperty : DocValuesPropertyBase, IGenericProperty { -#pragma warning disable 618 - public GenericProperty() : base(null) { } -#pragma warning restore 618 + public GenericProperty() : base(FieldType.Object) => this.TypeOverride = null; public TermVectorOption? TermVector { get; set; } public double? Boost { get; set; } @@ -67,6 +65,11 @@ public GenericProperty() : base(null) { } public bool? Norms { get; set; } public IndexOptions? IndexOptions { get; set; } public string Analyzer { get; set; } + public string Type + { + get => this.TypeOverride; + set => this.TypeOverride = value; + } } /// @@ -91,9 +94,9 @@ public class GenericPropertyDescriptor int? IGenericProperty.PositionIncrementGap { get; set; } IStringFielddata IGenericProperty.Fielddata { get; set; } -#pragma warning disable 618 - public GenericPropertyDescriptor() : base(null) { } -#pragma warning restore 618 + public GenericPropertyDescriptor() : base(FieldType.Object) => this.TypeOverride = null; + + public GenericPropertyDescriptor Type(string type) => Assign(a => this.TypeOverride = type); public GenericPropertyDescriptor Index(FieldIndexOption? index = FieldIndexOption.NotAnalyzed) => Assign(a => a.Index = index); diff --git a/src/Nest/Mapping/Visitor/MappingWalker.cs b/src/Nest/Mapping/Visitor/MappingWalker.cs index b795f9af143..8a14c186796 100644 --- a/src/Nest/Mapping/Visitor/MappingWalker.cs +++ b/src/Nest/Mapping/Visitor/MappingWalker.cs @@ -47,7 +47,7 @@ public void Accept(IProperties properties) { var field = kv.Value; var type = field.Type; - var ft = type.Name.ToEnum(); + var ft = type.ToEnum(); switch (ft) { case FieldType.Text: diff --git a/src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs b/src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs index 657b973811f..521efa3ba4d 100644 --- a/src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs +++ b/src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs @@ -6,7 +6,6 @@ using Tests.Framework; using Tests.Framework.Integration; using Tests.Framework.ManagedElasticsearch.Clusters; -using Xunit; namespace Tests.Indices.IndexSettings.IndexTemplates.PutIndexTemplate { diff --git a/src/Tests/Mapping/Types/PropertyTestsBase.cs b/src/Tests/Mapping/Types/PropertyTestsBase.cs index f28e888a0b4..ff937c8ee41 100644 --- a/src/Tests/Mapping/Types/PropertyTestsBase.cs +++ b/src/Tests/Mapping/Types/PropertyTestsBase.cs @@ -2,7 +2,6 @@ using Nest; using Elasticsearch.Net; using Tests.Framework; -using static Tests.Framework.RoundTripper; using Tests.Framework.Integration; using Tests.Framework.ManagedElasticsearch.Clusters; using Tests.Framework.MockData; diff --git a/src/Tests/Mapping/Types/SingleMappingPropertyTestsBase.cs b/src/Tests/Mapping/Types/SingleMappingPropertyTestsBase.cs new file mode 100644 index 00000000000..a89a8945cb9 --- /dev/null +++ b/src/Tests/Mapping/Types/SingleMappingPropertyTestsBase.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using Elasticsearch.Net; +using Nest; +using Tests.Framework; +using Tests.Framework.Integration; +using Tests.Framework.ManagedElasticsearch.Clusters; +using Tests.Framework.MockData; + +namespace Tests.Mapping.Types +{ + public abstract class SingleMappingPropertyTestsBase + : ApiIntegrationTestBase + { + protected SingleMappingPropertyTestsBase(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } + + protected override LazyResponses ClientUsage() => Calls( + fluent: (client, f) => client.PutIndexTemplate(CallIsolatedValue, f), + fluentAsync: (client, f) => client.PutIndexTemplateAsync(CallIsolatedValue, f), + request: (client, r) => client.PutIndexTemplate(r), + requestAsync: (client, r) => client.PutIndexTemplateAsync(r) + ); + + protected override HttpMethod HttpMethod => HttpMethod.PUT; + protected override string UrlPath => $"/_template/{CallIsolatedValue}?create=false"; + protected override bool SupportsDeserialization => false; + protected override bool ExpectIsValid => true; + protected override int ExpectStatusCode => 200; + + protected override object ExpectJson => new + { + order = 1, + index_patterns = new [] {"nestx-*" }, + settings = new Dictionary { { "index.number_of_shards", 1 } }, + mappings = new + { + _default_ = new + { + dynamic_templates = new object[] + { + new + { + @base = new + { + match = "*", + match_mapping_type = "*", + mapping = this.SingleMappingJson + } + } + } + } + } + }; + + protected abstract object SingleMappingJson { get; } + + protected override PutIndexTemplateDescriptor NewDescriptor() => new PutIndexTemplateDescriptor(CallIsolatedValue); + protected override Func Fluent => d => d + .Order(1) + .IndexPatterns("nestx-*") + .Create(false) + .Settings(p=>p.NumberOfShards(1)) + .Mappings(m => m + .Map("_default_", tm => tm + .DynamicTemplates(t => t + .DynamicTemplate("base", dt => dt + .Match("*") + .MatchMappingType("*") + .Mapping(FluentSingleMapping) + ) + ) + ) + ); + + protected abstract Func, IProperty> FluentSingleMapping { get; } + protected abstract IProperty InitializerSingleMapping { get; } + + protected override PutIndexTemplateRequest Initializer => new PutIndexTemplateRequest(CallIsolatedValue) + { + Order = 1, + IndexPatterns = new[] { "nestx-*" }, + Create = false, + Settings = new Nest.IndexSettings + { + NumberOfShards = 1 + }, + Mappings = new Mappings + { + { "_default_", new TypeMapping + { + DynamicTemplates = new DynamicTemplateContainer + { + { "base", new DynamicTemplate + { + Match = "*", + MatchMappingType = "*", + Mapping = InitializerSingleMapping + } + } + } + } + } + } + }; + } +} diff --git a/src/Tests/Mapping/Types/Specialized/Generic/GenericPropertyTests.cs b/src/Tests/Mapping/Types/Specialized/Generic/GenericPropertyTests.cs new file mode 100644 index 00000000000..bd23fcae5de --- /dev/null +++ b/src/Tests/Mapping/Types/Specialized/Generic/GenericPropertyTests.cs @@ -0,0 +1,27 @@ +using System; +using Nest; +using Tests.Framework.Integration; +using Tests.Framework.ManagedElasticsearch.Clusters; + +namespace Tests.Mapping.Types.Specialized.Generic +{ + public class GenericPropertyTests : SingleMappingPropertyTestsBase + { + private const string GenericType = "{dynamic_type}"; + public GenericPropertyTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } + + protected override object SingleMappingJson { get; } = new {index = "no", type= GenericType}; + + protected override Func, IProperty> FluentSingleMapping => m => m + .Generic(g => g + .Type(GenericType) + .Index(FieldIndexOption.No) + ); + + protected override IProperty InitializerSingleMapping { get; } = new GenericProperty + { + Type = GenericType, + Index = FieldIndexOption.No + }; + } +}