Skip to content

Use MultiFieldMapper as base for the core-mappers #885

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 7 commits into from
Aug 19, 2014
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
22 changes: 9 additions & 13 deletions docs/contents/nest/indices/put-mapping.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,37 @@ You can persist this mapping by simpling calling

You can also create mappings on the fly:

var typeMapping = new TypeMapping(Guid.NewGuid().ToString("n"));
var property = new TypeMappingProperty
var indexDefinition = new RootObjectMapping
{
Type = "multi_field"
Properties = new Dictionary<PropertyNameMarker, IElasticType>(),
Name = indexName
};

var primaryField = new TypeMappingProperty
var property = new StringMapping
{
Type = "string",
Index = "not_analyzed"
};

var analyzedField = new TypeMappingProperty
var analyzedField = new StringMapping
{
Type = "string",
Index = "analyzed"
};

property.Fields = new Dictionary<string, TypeMappingProperty>();
property.Fields.Add("name", primaryField);
property.Fields.Add("name_analyzed", analyzedField);
indexDefinition.Properties.Add("name", property);
this.ConnectedClient.Map<object>(x => x.InitializeUsing(indexDefinition));

typeMapping.Properties.Add("name", property);
this.ConnectedClient.Map(typeMapping);

## Multifield Mapping
To create multifield type you can use following example.

var result = this._client.Map<ElasticsearchProject>(m => m
.Properties(props => props
.MultiField(s => s
.String(s => s
.Name(p => p.Name)
.Path(MultiFieldMappingPath.Full)
.Index(FieldIndexOption.not_analyzed)
.Fields(pprops => pprops
.String(ps => ps.Name(p => p.Name).Index(FieldIndexOption.not_analyzed))
.String(ps => ps.Name(p => p.Name.Suffix("searchable")).Index(FieldIndexOption.analyzed))
)
))
Expand Down
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
23 changes: 22 additions & 1 deletion src/Nest/Domain/Mapping/Descriptors/BooleanMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

public BooleanMappingDescriptor<T> Path(MultiFieldMappingPath path)
{
this._Mapping.Path = path.Value;
return this;
}

public BooleanMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
fieldSelector.ThrowIfNull("fieldSelector");
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;
}
return this;
}
}
}
22 changes: 21 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,7 @@

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

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

public DateMappingDescriptor<T> Path(MultiFieldMappingPath path)
{
this._Mapping.Path = path.Value;
return this;
}

public DateMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
fieldSelector.ThrowIfNull("fieldSelector");
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;
}
return this;
}
}
}
23 changes: 22 additions & 1 deletion src/Nest/Domain/Mapping/Descriptors/GenericMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

Expand Down Expand Up @@ -75,5 +75,26 @@ public GenericMappingDescriptor<T> IncludeInAll(bool includeInAll = true)
this._Mapping.IncludeInAll = includeInAll;
return this;
}

public GenericMappingDescriptor<T> Path(MultiFieldMappingPath path)
{
this._Mapping.Path = path.Value;
return this;
}

public GenericMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
fieldSelector.ThrowIfNull("fieldSelector");
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;
}
return this;
}
}
}
26 changes: 23 additions & 3 deletions src/Nest/Domain/Mapping/Descriptors/NumberMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Linq.Expressions;

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

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

public NumberMappingDescriptor<T> Path(MultiFieldMappingPath path)
{
this._Mapping.Path = path.Value;
return this;
}

public NumberMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
fieldSelector.ThrowIfNull("fieldSelector");
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;
}
return this;
}

}
}
25 changes: 23 additions & 2 deletions src/Nest/Domain/Mapping/Descriptors/StringMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Linq.Expressions;

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

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

public StringMappingDescriptor<T> Path(MultiFieldMappingPath path)
{
this._Mapping.Path = path.Value;
return this;
}

public StringMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
{
fieldSelector.ThrowIfNull("fieldSelector");
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;
}
return this;
}
}
}
16 changes: 4 additions & 12 deletions src/Nest/Domain/Mapping/Types/BooleanMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class BooleanMapping : IElasticType, IElasticCoreType
public class BooleanMapping : MultiFieldMapping, IElasticType, IElasticCoreType
{

public PropertyNameMarker Name { get; set; }

[JsonProperty("type")]
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "boolean" }; } }

[JsonProperty("similarity")]
public string Similarity { get; set; }
public BooleanMapping():base("boolean")
{
}

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
Expand All @@ -35,9 +30,6 @@ public class BooleanMapping : IElasticType, IElasticCoreType
[JsonProperty("null_value")]
public bool? NullValue { get; set; }

[JsonProperty("include_in_all")]
public bool? IncludeInAll { get; set; }

[JsonProperty("copy_to")]
public IEnumerable<PropertyPathMarker> CopyTo { get; set; }
}
Expand Down
15 changes: 4 additions & 11 deletions src/Nest/Domain/Mapping/Types/DateMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public class DateMapping : IElasticType, IElasticCoreType
public class DateMapping : MultiFieldMapping, IElasticType, IElasticCoreType
{
public PropertyNameMarker Name { get; set; }

[JsonProperty("type")]
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "date" }; } }

[JsonProperty("similarity")]
public string Similarity { get; set; }
public DateMapping():base("date")
{
}

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
Expand All @@ -40,9 +36,6 @@ public class DateMapping : IElasticType, IElasticCoreType
[JsonProperty("null_value")]
public DateTime? NullValue { get; set; }

[JsonProperty("include_in_all")]
public bool? IncludeInAll { get; set; }

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

Expand Down
16 changes: 4 additions & 12 deletions src/Nest/Domain/Mapping/Types/GenericMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ namespace Nest
/// in order to specify "{dynamic_template}" the type, or if you have some plugin that exposes a new type.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class GenericMapping : IElasticType, IElasticCoreType
public class GenericMapping : MultiFieldMapping, IElasticType, IElasticCoreType
{

public PropertyNameMarker Name { get; set; }

[JsonProperty("type")]
public TypeNameMarker Type { get; set; }

[JsonProperty("similarity")]
public string Similarity { get; set; }
public GenericMapping():base(null)
{
}

/// <summary>
/// The name of the field that will be stored in the index. Defaults to the property/field name.
Expand All @@ -39,9 +34,6 @@ public class GenericMapping : IElasticType, IElasticCoreType
[JsonProperty("boost")]
public double? Boost { get; set; }

[JsonProperty("include_in_all")]
public bool? IncludeInAll { get; set; }

[JsonProperty("term_vector")]
public string TermVector { get; set; }

Expand Down
23 changes: 15 additions & 8 deletions src/Nest/Domain/Mapping/Types/MultiFieldMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ namespace Nest
[JsonObject(MemberSerialization.OptIn)]
public class MultiFieldMapping : IElasticType
{
private readonly TypeNameMarker _defaultType;

public MultiFieldMapping()
: this("multi_field")
{
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
}

protected MultiFieldMapping(TypeNameMarker defaultType)
{
_defaultType = defaultType;
}

public PropertyNameMarker Name { get; set; }

private TypeNameMarker _typeOverride;

[JsonProperty("type")]
public virtual TypeNameMarker Type
{
get { return _typeOverride ?? new TypeNameMarker { Name = "multi_field" }; }
get { return _typeOverride ?? _defaultType; }
set { _typeOverride = value; }
}

Expand All @@ -28,14 +41,8 @@ public virtual TypeNameMarker Type
[JsonProperty("include_in_all")]
public bool? IncludeInAll { get; set; }

[JsonProperty("fields"), JsonConverter(typeof(ElasticCoreTypeConverter))]
[JsonProperty("fields", DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(ElasticCoreTypeConverter))]
public IDictionary<PropertyNameMarker, IElasticCoreType> Fields { get; set; }


public MultiFieldMapping()
{
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
}
}

public class MultiFieldMappingPath
Expand Down
Loading