Skip to content

fix #2817 allow {dynamic_type} to be set when doing a generic mapping… #2946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static IEnumerable<Type> TypeWithInterfaces(Type objectType)
protected override IList<JsonProperty> 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<IProperty>(type, memberSerialization);

// Descriptors implement properties explicitly, these are not picked up by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IFieldMapping>();

JObject o = JObject.Load(reader);
var o = JObject.Load(reader);

foreach (var p in o.Properties())
{
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElasticsearchDocValuesPropertyAttributeBase>(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> IProperty.LocalMetadata { get; set; }

public string Name { get; set; }
Expand All @@ -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<ElasticsearchPropertyAttributeBase>(true);
Expand Down
1 change: 1 addition & 0 deletions src/Nest/Mapping/DynamicTemplate/SingleMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Nest
{
//TODO make this implement SelectorBase when that PR is moved
public class SingleMappingDescriptor<T> :
DescriptorBase<SingleMappingDescriptor<T>, IPropertiesDescriptor<T, IProperty>>, IPropertiesDescriptor<T, IProperty>
where T : class
Expand Down
8 changes: 4 additions & 4 deletions src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
}
}
7 changes: 2 additions & 5 deletions src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, DynamicMapping> 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; }
}
}
23 changes: 5 additions & 18 deletions src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, DynamicMapping> Dynamic { get; set; }
public bool? Enabled { get; set; }
Expand Down Expand Up @@ -59,24 +54,16 @@ public abstract class ObjectPropertyDescriptorBase<TDescriptor, TInterface, TPar

protected ObjectPropertyDescriptorBase() : this(FieldType.Object) { }

[Obsolete("Please use overload taking FieldType")]
protected ObjectPropertyDescriptorBase(string type) : base(type)
protected ObjectPropertyDescriptorBase(FieldType type) : base(type)
{
_TypeName = TypeName.Create<TChild>();
}

#pragma warning disable 618
protected ObjectPropertyDescriptorBase(FieldType type) : this(type.GetStringValue()) { }
#pragma warning restore 618

public TDescriptor Dynamic(Union<bool, DynamicMapping> dynamic) =>
Assign(a => a.Dynamic = dynamic);
public TDescriptor Dynamic(Union<bool, DynamicMapping> 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<PropertiesDescriptor<TChild>, IPromise<IProperties>> selector) =>
Assign(a => a.Properties = selector?.Invoke(new PropertiesDescriptor<TChild>(a.Properties))?.Value);
Expand Down
17 changes: 7 additions & 10 deletions src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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; }

}
}
6 changes: 2 additions & 4 deletions src/Nest/Mapping/Types/Core/Number/NumberProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public abstract class NumberPropertyDescriptorBase<TDescriptor, TInterface, T>
{
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; }
Expand Down Expand Up @@ -83,8 +82,7 @@ public TDescriptor Fielddata(Func<NumericFielddataDescriptor, INumericFielddata>
public TDescriptor ScalingFactor(double scalingFactor) => Assign(a => a.ScalingFactor = scalingFactor);
}

public class NumberPropertyDescriptor<T>
: NumberPropertyDescriptorBase<NumberPropertyDescriptor<T>, INumberProperty, T>, INumberProperty
public class NumberPropertyDescriptor<T> : NumberPropertyDescriptorBase<NumberPropertyDescriptor<T>, INumberProperty, T>
where T : class
{
}
Expand Down
22 changes: 11 additions & 11 deletions src/Nest/Mapping/Types/Core/String/StringAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
}
}
4 changes: 2 additions & 2 deletions src/Nest/Mapping/Types/Core/String/StringProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -79,7 +79,7 @@ public class StringPropertyDescriptor<T>
int? IStringProperty.PositionIncrementGap { get; set; }
IStringFielddata IStringProperty.Fielddata { get; set; }

public StringPropertyDescriptor() : base("string") { }
public StringPropertyDescriptor() : base(FieldType.String) { }

/// <summary>
/// Shortcut into .Index(FieldIndexOption.NotAnalyzed)
Expand Down
2 changes: 0 additions & 2 deletions src/Nest/Mapping/Types/CorePropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }


Expand Down
7 changes: 1 addition & 6 deletions src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ public abstract class CorePropertyDescriptorBase<TDescriptor, TInterface, T>
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);

Expand Down
7 changes: 1 addition & 6 deletions src/Nest/Mapping/Types/DocValuesPropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
2 changes: 0 additions & 2 deletions src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public abstract class DocValuesPropertyDescriptorBase<TDescriptor, TInterface, T
{
bool? IDocValuesProperty.DocValues { get; set; }

[Obsolete("Please use overload taking FieldType")]
protected DocValuesPropertyDescriptorBase(string type) : base(type) { }
protected DocValuesPropertyDescriptorBase(FieldType type) : base(type) { }

public TDescriptor DocValues(bool docValues = true) => Assign(a => a.DocValues = docValues);
Expand Down
8 changes: 2 additions & 6 deletions src/Nest/Mapping/Types/PropertiesJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Elasticsearch.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -19,8 +17,7 @@ internal class PropertiesJsonConverter : JsonConverter

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dict = value as IDictionary<PropertyName, IProperty>;
if (dict == null) return;
if (!(value is IDictionary<PropertyName, IProperty> dict)) return;
var settings = serializer.GetConnectionSettings();
var props = new Properties();
foreach (var kv in dict)
Expand All @@ -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);
Expand Down
Loading