Skip to content

Commit 4cd96a3

Browse files
committed
Allow {dynamic_type} to be set when doing a generic mapping
Manual backport of 83e2dba
1 parent 766833c commit 4cd96a3

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public class GenericPropertyDescriptor<T>
103103
public GenericPropertyDescriptor() : base(null) { }
104104
#pragma warning restore 618
105105

106+
public GenericPropertyDescriptor<T> Type(string type) => Assign(a => a.Type = type);
107+
106108
public GenericPropertyDescriptor<T> Index(FieldIndexOption? index = FieldIndexOption.NotAnalyzed) => Assign(a => a.Index = index);
107109

108110
public GenericPropertyDescriptor<T> Boost(double boost) => Assign(a => a.Boost = boost);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Elasticsearch.Net;
4+
using Nest;
5+
using Tests.Framework;
6+
using Tests.Framework.Integration;
7+
using Tests.Framework.ManagedElasticsearch.Clusters;
8+
9+
namespace Tests.Mapping.Types
10+
{
11+
public abstract class SingleMappingPropertyTestsBase
12+
: ApiIntegrationTestBase<WritableCluster, IPutIndexTemplateResponse, IPutIndexTemplateRequest, PutIndexTemplateDescriptor, PutIndexTemplateRequest>
13+
{
14+
protected SingleMappingPropertyTestsBase(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
15+
16+
protected override LazyResponses ClientUsage() => Calls(
17+
fluent: (client, f) => client.PutIndexTemplate(CallIsolatedValue, f),
18+
fluentAsync: (client, f) => client.PutIndexTemplateAsync(CallIsolatedValue, f),
19+
request: (client, r) => client.PutIndexTemplate(r),
20+
requestAsync: (client, r) => client.PutIndexTemplateAsync(r)
21+
);
22+
23+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
24+
protected override string UrlPath => $"/_template/{CallIsolatedValue}?create=false";
25+
protected override bool SupportsDeserialization => false;
26+
protected override bool ExpectIsValid => true;
27+
protected override int ExpectStatusCode => 200;
28+
29+
protected override object ExpectJson => new
30+
{
31+
order = 1,
32+
template = "nestx-*",
33+
settings = new Dictionary<string, object> { { "index.number_of_shards", 1 } },
34+
mappings = new
35+
{
36+
_default_ = new
37+
{
38+
dynamic_templates = new object[]
39+
{
40+
new
41+
{
42+
@base = new
43+
{
44+
match = "*",
45+
match_mapping_type = "*",
46+
mapping = this.SingleMappingJson
47+
}
48+
}
49+
}
50+
}
51+
}
52+
};
53+
54+
protected abstract object SingleMappingJson { get; }
55+
56+
protected override PutIndexTemplateDescriptor NewDescriptor() => new PutIndexTemplateDescriptor(CallIsolatedValue);
57+
protected override Func<PutIndexTemplateDescriptor, IPutIndexTemplateRequest> Fluent => d => d
58+
.Order(1)
59+
.Template("nestx-*")
60+
.Create(false)
61+
.Settings(p=>p.NumberOfShards(1))
62+
.Mappings(m => m
63+
.Map("_default_", tm => tm
64+
.DynamicTemplates(t => t
65+
.DynamicTemplate("base", dt => dt
66+
.Match("*")
67+
.MatchMappingType("*")
68+
.Mapping(FluentSingleMapping)
69+
)
70+
)
71+
)
72+
);
73+
74+
protected abstract Func<SingleMappingDescriptor<object>, IProperty> FluentSingleMapping { get; }
75+
protected abstract IProperty InitializerSingleMapping { get; }
76+
77+
protected override PutIndexTemplateRequest Initializer => new PutIndexTemplateRequest(CallIsolatedValue)
78+
{
79+
Order = 1,
80+
Template = "nestx-*",
81+
Create = false,
82+
Settings = new Nest.IndexSettings
83+
{
84+
NumberOfShards = 1
85+
},
86+
Mappings = new Mappings
87+
{
88+
{ "_default_", new TypeMapping
89+
{
90+
DynamicTemplates = new DynamicTemplateContainer
91+
{
92+
{ "base", new DynamicTemplate
93+
{
94+
Match = "*",
95+
MatchMappingType = "*",
96+
Mapping = InitializerSingleMapping
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}
103+
};
104+
}
105+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using Nest;
3+
using Tests.Framework.Integration;
4+
using Tests.Framework.ManagedElasticsearch.Clusters;
5+
6+
namespace Tests.Mapping.Types.Specialized.Generic
7+
{
8+
public class GenericPropertyTests : SingleMappingPropertyTestsBase
9+
{
10+
private const string GenericType = "{dynamic_type}";
11+
public GenericPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
12+
13+
protected override object SingleMappingJson { get; } = new {index = "no", type= GenericType};
14+
15+
protected override Func<SingleMappingDescriptor<object>, IProperty> FluentSingleMapping => m => m
16+
.Generic(g => g
17+
.Type(GenericType)
18+
.Index(FieldIndexOption.No)
19+
);
20+
21+
protected override IProperty InitializerSingleMapping { get; } = new GenericProperty
22+
{
23+
Type = GenericType,
24+
Index = FieldIndexOption.No
25+
};
26+
}
27+
}

0 commit comments

Comments
 (0)