Skip to content

Multi field mapping support for all core type properties #883

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -3,7 +3,7 @@

namespace Nest
{
public class AttachmentMappingDescriptor<T>
public class AttachmentMappingDescriptor<T> where T : class
{
internal AttachmentMapping _Mapping = new AttachmentMapping();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Nest
{
public class BinaryMappingDescriptor<T>
public class BinaryMappingDescriptor<T> : CoreMappingDescriptorBase<T>
where T : class
{
internal BinaryMapping _Mapping = new BinaryMapping();

Expand Down Expand Up @@ -60,5 +61,11 @@ public BinaryMappingDescriptor<T> CopyTo(params Expression<Func<T, object>>[] ob
this._Mapping.CopyTo = objectPaths.Select(e => (PropertyPathMarker)e);
return this;
}

public BinaryMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
base.Fields(fieldSelector, this._Mapping);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Nest
{
public class BooleanMappingDescriptor<T>
public class BooleanMappingDescriptor<T> : CoreMappingDescriptorBase<T>
where T : class
{
internal BooleanMapping _Mapping = new BooleanMapping();

Expand Down Expand Up @@ -63,5 +64,11 @@ public BooleanMappingDescriptor<T> CopyTo(params Expression<Func<T, object>>[] o
this._Mapping.CopyTo = objectPaths.Select(e => (PropertyPathMarker)e);
return this;
}

public BooleanMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
base.Fields(fieldSelector, this._Mapping);
return this;
}
}
}
29 changes: 29 additions & 0 deletions src/Nest/Domain/Mapping/Descriptors/CoreMappingDescriptorBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public abstract class CoreMappingDescriptorBase<T> where T : class
{
protected virtual void Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector, CoreMappingBase mapping)
{
fieldSelector.ThrowIfNull("fieldSelector");

if (mapping.Fields == null)
mapping.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();

var properties = fieldSelector(new CorePropertiesDescriptor<T>());

foreach (var p in properties.Properties)
{
var value = p.Value as IElasticCoreType;
if (value == null)
continue;

mapping.Fields[p.Key] = value;
}
}
}
}
8 changes: 7 additions & 1 deletion src/Nest/Domain/Mapping/Descriptors/DateMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Nest
{
public class DateMappingDescriptor<T>
public class DateMappingDescriptor<T> : CoreMappingDescriptorBase<T>
where T : class
{
internal DateMapping _Mapping = new DateMapping();

Expand Down Expand Up @@ -68,5 +69,10 @@ public DateMappingDescriptor<T> IgnoreMalformed(bool ignoreMalformed = true)
return this;
}

public DateMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
base.Fields(fieldSelector, this._Mapping);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Nest
{
[Obsolete("Scheduled to be removed in 2.0. Use the core property descriptor instead.")]
public class MultiFieldMappingDescriptor<T> where T : class
{
internal MultiFieldMapping _Mapping = new MultiFieldMapping();
Expand Down
10 changes: 8 additions & 2 deletions src/Nest/Domain/Mapping/Descriptors/NumberMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Nest
{
public class NumberMappingDescriptor<T>
public class NumberMappingDescriptor<T> : CoreMappingDescriptorBase<T>
where T : class
{
internal NumberMapping _Mapping = new NumberMapping();

Expand Down Expand Up @@ -79,7 +80,12 @@ public NumberMappingDescriptor<T> Coerce(bool coerce = true)
{
this._Mapping.Coerce = coerce;
return this;
}

public NumberMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
base.Fields(fieldSelector, this._Mapping);
return this;
}

}
}
3 changes: 2 additions & 1 deletion src/Nest/Domain/Mapping/Descriptors/PropertiesDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public PropertiesDescriptor<T> NestedObject<TChild>(Func<NestedObjectMappingDesc
this.Properties[d._Mapping.Name] = d._Mapping;
return this;
}


[Obsolete("Scheduled to be removed in 2.0. Use Fields() on the core property descriptor instead.")]
public PropertiesDescriptor<T> MultiField(Func<MultiFieldMappingDescriptor<T>, MultiFieldMappingDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Nest
{
public class StringMappingDescriptor<T>
public class StringMappingDescriptor<T> : CoreMappingDescriptorBase<T>
where T : class
{
internal StringMapping _Mapping = new StringMapping();

Expand Down Expand Up @@ -118,6 +119,12 @@ public StringMappingDescriptor<T> CopyTo(params Expression<Func<T, object>>[] ob
{
this._Mapping.CopyTo = objectPaths.Select(e => (PropertyPathMarker)e);
return this;
}

public StringMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
base.Fields(fieldSelector, this._Mapping);
return this;
}
}
}
9 changes: 2 additions & 7 deletions src/Nest/Domain/Mapping/Types/BinaryMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class BinaryMapping : IElasticType, IElasticCoreType
public class BinaryMapping
: CoreMappingBase, IElasticType
{

public PropertyNameMarker Name { get; set; }
Expand All @@ -16,12 +17,6 @@ public class BinaryMapping : IElasticType, IElasticCoreType
[JsonProperty("similarity")]
public string Similarity { get; set; }

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

[JsonProperty("doc_values")]
public bool? DocValues { get; set; }

Expand Down
9 changes: 2 additions & 7 deletions src/Nest/Domain/Mapping/Types/BooleanMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class BooleanMapping : IElasticType, IElasticCoreType
public class BooleanMapping
: CoreMappingBase, IElasticType
{

public PropertyNameMarker Name { get; set; }
Expand All @@ -17,12 +18,6 @@ public class BooleanMapping : IElasticType, IElasticCoreType
[JsonProperty("similarity")]
public string Similarity { get; set; }

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

[JsonProperty("store")]
public bool? Store { get; set; }

Expand Down
21 changes: 21 additions & 0 deletions src/Nest/Domain/Mapping/Types/CoreMappingBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public abstract class CoreMappingBase : IElasticCoreType
{
/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

[JsonProperty("fields"), JsonConverter(typeof(ElasticCoreTypeConverter))]
public IDictionary<PropertyNameMarker, IElasticCoreType> Fields { get; set; }
}
}
9 changes: 2 additions & 7 deletions src/Nest/Domain/Mapping/Types/DateMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class DateMapping : IElasticType, IElasticCoreType
public class DateMapping
: CoreMappingBase, IElasticType
{
public PropertyNameMarker Name { get; set; }

Expand All @@ -16,12 +17,6 @@ public class DateMapping : IElasticType, IElasticCoreType
[JsonProperty("similarity")]
public string Similarity { get; set; }

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

[JsonProperty("store")]
public bool? Store { get; set; }

Expand Down
76 changes: 71 additions & 5 deletions src/Nest/Domain/Mapping/Types/MultiFieldMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using System;
using Newtonsoft.Json.Converters;

namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class MultiFieldMapping : IElasticType
{
{
public MultiFieldMapping()
{
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
}

public PropertyNameMarker Name { get; set; }

private TypeNameMarker _typeOverride;
Expand All @@ -31,11 +37,71 @@ public virtual TypeNameMarker Type
[JsonProperty("fields"), JsonConverter(typeof(ElasticCoreTypeConverter))]
public IDictionary<PropertyNameMarker, IElasticCoreType> Fields { get; set; }

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

public MultiFieldMapping()
{
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
}
[JsonProperty("analyzer")]
public string Analyzer { get; set; }

[JsonProperty("store")]
public bool? Store { get; set; }

[JsonProperty("index"), JsonConverter(typeof(StringEnumConverter))]
public FieldIndexOption? Index { get; set; }

[JsonProperty("term_vector"), JsonConverter(typeof(StringEnumConverter))]
public TermVectorOption? TermVector { get; set; }

[JsonProperty("boost")]
public double? Boost { get; set; }

[JsonProperty("null_value")]
public string NullValue { get; set; }

[JsonProperty("norms")]
public NormsMapping Norms { get; set; }

[JsonProperty("omit_norms")]
public bool? OmitNorms { get; set; }

[JsonProperty("index_options"), JsonConverter(typeof(StringEnumConverter))]
public IndexOptions? IndexOptions { get; set; }

[JsonProperty("index_analyzer")]
public string IndexAnalyzer { get; set; }

[JsonProperty("ignore_above")]
public int? IgnoreAbove { get; set; }

[JsonProperty("search_analyzer")]
public string SearchAnalyzer { get; set; }

[JsonProperty("doc_values")]
public bool? DocValues { get; set; }

[JsonProperty("position_offset_gap")]
public int? PositionOffsetGap { get; set; }

[JsonProperty("precision_step")]
public int? PrecisionStep { get; set; }

[JsonProperty("ignore_malformed")]
public bool? IgnoreMalformed { get; set; }

[JsonProperty("coerce")]
public bool? Coerce { get; set; }

[JsonProperty("compress")]
public bool? Compress { get; set; }

[JsonProperty("compress_threshold")]
public string CompressThreshold { get; set; }

[JsonProperty("copy_to")]
public IEnumerable<PropertyPathMarker> CopyTo { get; set; }
}

public class MultiFieldMappingPath
Expand Down
9 changes: 2 additions & 7 deletions src/Nest/Domain/Mapping/Types/NumberMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class NumberMapping : IElasticType, IElasticCoreType
public class NumberMapping
: CoreMappingBase, IElasticType
{
public PropertyNameMarker Name { get; set; }

Expand All @@ -17,12 +18,6 @@ public class NumberMapping : IElasticType, IElasticCoreType
[JsonProperty("similarity")]
public string Similarity { get; set; }

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
/// </summary>
[JsonProperty("index_name")]
public string IndexName { get; set; }

[JsonProperty("store")]
public bool? Store { get; set; }

Expand Down
Loading