Skip to content

Commit afc241c

Browse files
Mpdreamzrusscam
authored andcommitted
Add support for alias property mapping (#3434)
(cherry picked from commit d03dacc)
1 parent fb6df8d commit afc241c

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

src/Nest/Mapping/Types/FieldType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public enum FieldType
8686
DateRange,
8787
[EnumMember(Value = "ip_range")]
8888
IpRange,
89+
[EnumMember(Value = "alias")]
90+
Alias,
8991
[EnumMember(Value = "join")]
9092
Join,
9193
}

src/Nest/Mapping/Types/Properties.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public PropertiesDescriptor<T> Nested<TChild>(Func<NestedPropertyDescriptor<T, T
132132

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

135+
public PropertiesDescriptor<T> FieldAlias(Func<FieldAliasPropertyDescriptor<T>, IFieldAliasProperty> selector) => SetProperty(selector);
136+
135137
public PropertiesDescriptor<T> Custom(IProperty customType) => SetProperty(customType);
136138

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

153155
return this.Assign(a => a[type.Name] = type);
154156
}
157+
155158
}
156159

157160
internal static class PropertiesExtensions

src/Nest/Mapping/Types/PropertyDescriptorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
2020

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

23-
protected PropertyDescriptorBase(FieldType type) { Self.Type = type.GetStringValue(); }
23+
protected PropertyDescriptorBase(FieldType type) => Self.Type = type.GetStringValue();
2424

2525
/// <inheritdoc cref="IProperty.Name"/>
2626
public TDescriptor Name(PropertyName name) => Assign(a => a.Name = name);

src/Nest/Mapping/Types/PropertyJsonConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
6161
case FieldType.LongRange: return ReadProperty<LongRangeProperty>(jObject, serializer);
6262
case FieldType.IpRange: return ReadProperty<IpRangeProperty>(jObject, serializer);
6363
case FieldType.Join: return ReadProperty<JoinProperty>(jObject, serializer);
64+
case FieldType.Alias: return ReadProperty<FieldAliasProperty>(jObject, serializer);
6465
case FieldType.None:
6566
break;
6667
default:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq.Expressions;
4+
using Newtonsoft.Json;
5+
6+
namespace Nest
7+
{
8+
/// <summary>
9+
/// An alias mapping defines an alternate name for a field in the index. The alias can be used in place
10+
/// of the target field in search requests, and selected other APIs like field capabilities.
11+
/// </summary>
12+
[JsonObject(MemberSerialization.OptIn)]
13+
public interface IFieldAliasProperty : IProperty
14+
{
15+
/// <summary> The full path to alias </summary>
16+
[JsonProperty("path")]
17+
Field Path { get; set; }
18+
}
19+
20+
/// <inheritdoc cref="IFieldAliasProperty.Path" />
21+
[DebuggerDisplay("{DebugDisplay}")]
22+
public class FieldAliasProperty : PropertyBase, IFieldAliasProperty
23+
{
24+
public FieldAliasProperty() : base(FieldType.Alias) { }
25+
26+
/// <inheritdoc cref="IFieldAliasProperty.Path" />
27+
public Field Path { get; set; }
28+
}
29+
30+
/// <inheritdoc cref="IFieldAliasProperty.Path" />
31+
[DebuggerDisplay("{DebugDisplay}")]
32+
public class FieldAliasPropertyDescriptor<T>
33+
: PropertyDescriptorBase<FieldAliasPropertyDescriptor<T>, IFieldAliasProperty, T>, IFieldAliasProperty
34+
where T : class
35+
{
36+
Field IFieldAliasProperty.Path { get; set; }
37+
38+
public FieldAliasPropertyDescriptor() : base(FieldType.Alias) { }
39+
40+
/// <inheritdoc cref="IFieldAliasProperty.Path" />
41+
public FieldAliasPropertyDescriptor<T> Path(Expression<Func<T, object>> field) => Assign(a => a.Path = field);
42+
43+
/// <inheritdoc cref="IFieldAliasProperty.Path" />
44+
public FieldAliasPropertyDescriptor<T> Path(Field field) => Assign(a => a.Path = field);
45+
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Nest;
3+
using Tests.Core.ManagedElasticsearch.Clusters;
4+
using Tests.Domain;
5+
using Tests.Framework.Integration;
6+
7+
namespace Tests.Mapping.Types.Specialized.FieldAlias
8+
{
9+
public class FieldAliasPropertyTests : PropertyTestsBase
10+
{
11+
public FieldAliasPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
12+
13+
protected override object ExpectJson => new
14+
{
15+
properties = new
16+
{
17+
leadDevFirstName = new
18+
{
19+
type = "alias",
20+
path = "leadDeveloper.firstName",
21+
}
22+
}
23+
};
24+
25+
protected override Func<PropertiesDescriptor<Project>, IPromise<IProperties>> FluentProperties => f => f
26+
.FieldAlias(s => s
27+
.Name("leadDevFirstName")
28+
.Path(p=>p.LeadDeveloper.FirstName)
29+
);
30+
31+
protected override IProperties InitializerProperties => new Properties
32+
{
33+
{ "leadDevFirstName", new FieldAliasProperty
34+
{
35+
Path = Infer.Field<Project>(p=>p.LeadDeveloper.FirstName)
36+
}
37+
}
38+
};
39+
}
40+
}

0 commit comments

Comments
 (0)