Skip to content

Add support for alias property mapping #3434

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 1 commit into from
Oct 17, 2018
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
2 changes: 2 additions & 0 deletions src/Nest/Mapping/Types/FieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public enum FieldType
DateRange,
[EnumMember(Value = "ip_range")]
IpRange,
[EnumMember(Value = "alias")]
Alias,
[EnumMember(Value = "join")]
Join,
}
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Mapping/Types/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public PropertiesDescriptor<T> Nested<TChild>(Func<NestedPropertyDescriptor<T, T

public PropertiesDescriptor<T> Join(Func<JoinPropertyDescriptor<T>, IJoinProperty> selector) => SetProperty(selector);

public PropertiesDescriptor<T> FieldAlias(Func<FieldAliasPropertyDescriptor<T>, IFieldAliasProperty> selector) => SetProperty(selector);

public PropertiesDescriptor<T> Custom(IProperty customType) => SetProperty(customType);

private PropertiesDescriptor<T> SetProperty<TDescriptor, TInterface>(Func<TDescriptor, TInterface> selector)
Expand All @@ -152,6 +154,7 @@ private PropertiesDescriptor<T> SetProperty(IProperty type)

return this.Assign(a => a[type.Name] = type);
}

}

internal static class PropertiesExtensions
Expand Down
2 changes: 1 addition & 1 deletion src/Nest/Mapping/Types/PropertyDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>

protected string DebugDisplay => $"Type: {Self.Type ?? "<empty>"}, Name: {Self.Name.DebugDisplay} ";

protected PropertyDescriptorBase(FieldType type) { Self.Type = type.GetStringValue(); }
protected PropertyDescriptorBase(FieldType type) => Self.Type = type.GetStringValue();

/// <inheritdoc cref="IProperty.Name"/>
public TDescriptor Name(PropertyName name) => Assign(a => a.Name = name);
Expand Down
1 change: 1 addition & 0 deletions src/Nest/Mapping/Types/PropertyJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
case FieldType.LongRange: return ReadProperty<LongRangeProperty>(jObject, serializer);
case FieldType.IpRange: return ReadProperty<IpRangeProperty>(jObject, serializer);
case FieldType.Join: return ReadProperty<JoinProperty>(jObject, serializer);
case FieldType.Alias: return ReadProperty<FieldAliasProperty>(jObject, serializer);
case FieldType.None:
break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using Newtonsoft.Json;

namespace Nest
{
/// <summary>
/// An alias mapping defines an alternate name for a field in the index. The alias can be used in place
/// of the target field in search requests, and selected other APIs like field capabilities.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public interface IFieldAliasProperty : IProperty
{
/// <summary> The full path to alias </summary>
[JsonProperty("path")]
Field Path { get; set; }
}

/// <inheritdoc cref="IFieldAliasProperty.Path" />
[DebuggerDisplay("{DebugDisplay}")]
public class FieldAliasProperty : PropertyBase, IFieldAliasProperty
{
public FieldAliasProperty() : base(FieldType.Alias) { }

/// <inheritdoc cref="IFieldAliasProperty.Path" />
public Field Path { get; set; }
}

/// <inheritdoc cref="IFieldAliasProperty.Path" />
[DebuggerDisplay("{DebugDisplay}")]
public class FieldAliasPropertyDescriptor<T>
: PropertyDescriptorBase<FieldAliasPropertyDescriptor<T>, IFieldAliasProperty, T>, IFieldAliasProperty
where T : class
{
Field IFieldAliasProperty.Path { get; set; }

public FieldAliasPropertyDescriptor() : base(FieldType.Alias) { }

/// <inheritdoc cref="IFieldAliasProperty.Path" />
public FieldAliasPropertyDescriptor<T> Path(Expression<Func<T, object>> field) => Assign(a => a.Path = field);

/// <inheritdoc cref="IFieldAliasProperty.Path" />
public FieldAliasPropertyDescriptor<T> Path(Field field) => Assign(a => a.Path = field);

}
}
2 changes: 1 addition & 1 deletion src/Tests/Tests.Configuration/tests.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# tracked by git).

# mode either u (unit test), i (integration test) or m (mixed mode)
mode: u
mode: m
# the elasticsearch version that should be started
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
elasticsearch_version: 6.4.1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Nest;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Domain;
using Tests.Framework.Integration;

namespace Tests.Mapping.Types.Specialized.FieldAlias
{
public class FieldAliasPropertyTests : PropertyTestsBase
{
public FieldAliasPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override object ExpectJson => new
{
properties = new
{
leadDevFirstName = new
{
type = "alias",
path = "leadDeveloper.firstName",
}
}
};

protected override Func<PropertiesDescriptor<Project>, IPromise<IProperties>> FluentProperties => f => f
.FieldAlias(s => s
.Name("leadDevFirstName")
.Path(p=>p.LeadDeveloper.FirstName)
);

protected override IProperties InitializerProperties => new Properties
{
{ "leadDevFirstName", new FieldAliasProperty
{
Path = Infer.Field<Project>(p=>p.LeadDeveloper.FirstName)
}
}
};
}
}