Skip to content

Commit 7c92c5d

Browse files
committed
Merge branch 'feature/2.x-generic-mapping' into 2.x
2 parents 4179866 + 9b7a030 commit 7c92c5d

File tree

6 files changed

+199
-9
lines changed

6 files changed

+199
-9
lines changed

src/Nest/CommonAbstractions/Infer/Types/Types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
5656
{
5757
var nestSettings = settings as IConnectionSettingsValues;
5858
if (nestSettings == null)
59-
throw new Exception("Tried to pass field name on querysting but it could not be resolved because no nest settings are available");
59+
throw new Exception("Tried to pass field name on querystring but it could not be resolved because no nest settings are available");
6060
var infer = new Inferrer(nestSettings);
6161
var types = this.Item2.Types.Select(t => infer.TypeName(t)).Distinct();
6262
return string.Join(",", types);
@@ -75,7 +75,7 @@ public override bool Equals(object obj)
7575
a => false,
7676
m => this.GetHashCode().Equals(other.GetHashCode())
7777
)
78-
);
78+
);
7979
}
8080

8181
public override int GetHashCode()

src/Nest/Mapping/DynamicTemplate/DynamicTemplatesJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class DynamicTemplatesJsonConverter : JsonConverter
1414
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1515
{
1616
var dict = value as DynamicTemplateContainer;
17-
if (dict == null || !dict.HasAny()) return;
17+
if (!dict.HasAny()) return;
1818
writer.WriteStartArray();
1919
foreach (var p in dict)
2020
{

src/Nest/Mapping/DynamicTemplate/SingleMapping.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@ public IProperty Murmur3Hash(Func<Murmur3HashPropertyDescriptor<T>, IMurmur3Hash
4747

4848
public IProperty TokenCount(Func<TokenCountPropertyDescriptor<T>, ITokenCountProperty> selector) =>
4949
selector?.Invoke(new TokenCountPropertyDescriptor<T>());
50+
51+
public IProperty Generic(Func<GenericPropertyDescriptor<T>, IGenericProperty> selector) =>
52+
selector?.Invoke(new GenericPropertyDescriptor<T>());
5053
}
51-
}
54+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Nest
5+
{
6+
/// <summary>
7+
/// A generic property to map properties that may be of different types.
8+
/// Not all methods are valid for all types.
9+
/// </summary>
10+
[JsonObject(MemberSerialization.OptIn)]
11+
public interface IGenericProperty : IProperty
12+
{
13+
[JsonProperty("index")]
14+
FieldIndexOption? Index { get; set; }
15+
16+
[JsonProperty("term_vector")]
17+
TermVectorOption? TermVector { get; set; }
18+
19+
[JsonProperty("boost")]
20+
double? Boost { get; set; }
21+
22+
[JsonProperty("null_value")]
23+
string NullValue { get; set; }
24+
25+
[JsonProperty("norms")]
26+
INorms Norms { get; set; }
27+
28+
[JsonProperty("index_options")]
29+
IndexOptions? IndexOptions { get; set; }
30+
31+
[JsonProperty("analyzer")]
32+
string Analyzer { get; set; }
33+
34+
[JsonProperty("search_analyzer")]
35+
string SearchAnalyzer { get; set; }
36+
37+
[JsonProperty("include_in_all")]
38+
bool? IncludeInAll { get; set; }
39+
40+
[JsonProperty("ignore_above")]
41+
int? IgnoreAbove { get; set; }
42+
43+
[JsonProperty("position_increment_gap")]
44+
int? PositionIncrementGap { get; set; }
45+
46+
[JsonProperty("fielddata")]
47+
IStringFielddata Fielddata { get; set; }
48+
}
49+
50+
/// <summary>
51+
/// A generic property to map properties that may be of different types.
52+
/// Not all methods are valid for all types.
53+
/// </summary>
54+
public class GenericProperty : PropertyBase, IGenericProperty
55+
{
56+
public GenericProperty() : base(null) { }
57+
58+
public TermVectorOption? TermVector { get; set; }
59+
public double? Boost { get; set; }
60+
public string SearchAnalyzer { get; set; }
61+
public bool? IncludeInAll { get; set; }
62+
public int? IgnoreAbove { get; set; }
63+
public int? PositionIncrementGap { get; set; }
64+
public IStringFielddata Fielddata { get; set; }
65+
public FieldIndexOption? Index { get; set; }
66+
public string NullValue { get; set; }
67+
public INorms Norms { get; set; }
68+
public IndexOptions? IndexOptions { get; set; }
69+
public string Analyzer { get; set; }
70+
}
71+
72+
/// <summary>
73+
/// A generic property to map properties that may be of different types.
74+
/// Not all methods are valid for all types.
75+
/// </summary>
76+
/// <typeparam name="T">the type on which the property is declared</typeparam>
77+
public class GenericPropertyDescriptor<T>
78+
: PropertyDescriptorBase<GenericPropertyDescriptor<T>, IGenericProperty, T>, IGenericProperty
79+
where T : class
80+
{
81+
FieldIndexOption? IGenericProperty.Index { get; set; }
82+
TermVectorOption? IGenericProperty.TermVector { get; set; }
83+
double? IGenericProperty.Boost { get; set; }
84+
string IGenericProperty.NullValue { get; set; }
85+
INorms IGenericProperty.Norms { get; set; }
86+
IndexOptions? IGenericProperty.IndexOptions { get; set; }
87+
string IGenericProperty.Analyzer { get; set; }
88+
string IGenericProperty.SearchAnalyzer { get; set; }
89+
bool? IGenericProperty.IncludeInAll { get; set; }
90+
int? IGenericProperty.IgnoreAbove { get; set; }
91+
int? IGenericProperty.PositionIncrementGap { get; set; }
92+
IStringFielddata IGenericProperty.Fielddata { get; set; }
93+
94+
public GenericPropertyDescriptor() : base(null) { }
95+
96+
public GenericPropertyDescriptor<T> Index(FieldIndexOption? index = FieldIndexOption.NotAnalyzed) => Assign(a => a.Index = index);
97+
98+
public GenericPropertyDescriptor<T> Boost(double boost) => Assign(a => a.Boost = boost);
99+
100+
public GenericPropertyDescriptor<T> NullValue(string nullValue) => Assign(a => a.NullValue = nullValue);
101+
102+
public GenericPropertyDescriptor<T> IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll);
103+
104+
public GenericPropertyDescriptor<T> NotAnalyzed() => Index(FieldIndexOption.NotAnalyzed);
105+
106+
public GenericPropertyDescriptor<T> Index(FieldIndexOption index) => Assign(a => a.Index = index);
107+
108+
public GenericPropertyDescriptor<T> TermVector(TermVectorOption termVector) => Assign(a => a.TermVector = termVector);
109+
110+
public GenericPropertyDescriptor<T> IndexOptions(IndexOptions indexOptions) => Assign(a => a.IndexOptions = indexOptions);
111+
112+
public GenericPropertyDescriptor<T> Analyzer(string analyzer) => Assign(a => a.Analyzer = analyzer);
113+
114+
public GenericPropertyDescriptor<T> SearchAnalyzer(string searchAnalyzer) => Assign(a => a.SearchAnalyzer = searchAnalyzer);
115+
116+
public GenericPropertyDescriptor<T> Norms(Func<NormsDescriptor, INorms> selector) => Assign(a => a.Norms = selector?.Invoke(new NormsDescriptor()));
117+
118+
public GenericPropertyDescriptor<T> IgnoreAbove(int ignoreAbove) => Assign(a => a.IgnoreAbove = ignoreAbove);
119+
120+
public GenericPropertyDescriptor<T> PositionIncrementGap(int? positionIncrementGap) => Assign(a => a.PositionIncrementGap = positionIncrementGap);
121+
122+
public GenericPropertyDescriptor<T> Fielddata(Func<StringFielddataDescriptor, IStringFielddata> selector) =>
123+
Assign(a => a.Fielddata = selector?.Invoke(new StringFielddataDescriptor()));
124+
}
125+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@
827827
<Compile Include="Mapping\Types\Specialized\Attachment\AttachmentProperty.cs" />
828828
<Compile Include="Mapping\Types\Specialized\Completion\CompletionAttribute.cs" />
829829
<Compile Include="Mapping\Types\Specialized\Completion\CompletionProperty.cs" />
830+
<Compile Include="Mapping\Types\Specialized\Generic\GenericProperty.cs" />
830831
<Compile Include="Mapping\Types\Specialized\Ip\IpAttribute.cs" />
831832
<Compile Include="Mapping\Types\Specialized\Ip\IpProperty.cs" />
832833
<Compile Include="Mapping\Types\Specialized\Murmur3Hash\Murmur3HashAttribute.cs" />

src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
namespace Tests.Indices.IndexSettings.IndexTemplates.PutIndexTemplate
1010
{
1111
[Collection(IntegrationContext.Indexing)]
12-
public class PutIndexTemplateApiTests : ApiTestBase<IPutIndexTemplateResponse, IPutIndexTemplateRequest, PutIndexTemplateDescriptor, PutIndexTemplateRequest>
12+
public class PutIndexTemplateApiTests :
13+
ApiTestBase<IPutIndexTemplateResponse, IPutIndexTemplateRequest, PutIndexTemplateDescriptor, PutIndexTemplateRequest>
1314
{
14-
public PutIndexTemplateApiTests(IndexingCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
15+
public PutIndexTemplateApiTests(IndexingCluster cluster, EndpointUsage usage) : base(cluster, usage)
16+
{
17+
}
18+
1519
protected override LazyResponses ClientUsage() => Calls(
1620
fluent: (client, f) => client.PutIndexTemplate(CallIsolatedValue, f),
1721
fluentAsync: (client, f) => client.PutIndexTemplateAsync(CallIsolatedValue, f),
1822
request: (client, r) => client.PutIndexTemplate(r),
1923
requestAsync: (client, r) => client.PutIndexTemplateAsync(r)
20-
);
24+
);
2125

2226
protected override HttpMethod HttpMethod => HttpMethod.PUT;
27+
2328
protected override string UrlPath => $"/_template/{CallIsolatedValue}?create=false";
2429

2530
protected override bool SupportsDeserialization => false;
@@ -28,7 +33,28 @@ protected override LazyResponses ClientUsage() => Calls(
2833
{
2934
order = 1,
3035
template = "nestx-*",
31-
settings = new Dictionary<string, object> { { "index.number_of_shards", 1} }
36+
settings = new Dictionary<string, object> { { "index.number_of_shards", 1 } },
37+
mappings = new
38+
{
39+
_default_ = new
40+
{
41+
dynamic_templates = new object[]
42+
{
43+
new
44+
{
45+
@base = new
46+
{
47+
match = "*",
48+
match_mapping_type = "*",
49+
mapping = new
50+
{
51+
index = "no"
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
3258
};
3359

3460
protected override PutIndexTemplateDescriptor NewDescriptor() => new PutIndexTemplateDescriptor(CallIsolatedValue);
@@ -37,7 +63,22 @@ protected override LazyResponses ClientUsage() => Calls(
3763
.Order(1)
3864
.Template("nestx-*")
3965
.Create(false)
40-
.Settings(p=>p.NumberOfShards(1));
66+
.Settings(p=>p.NumberOfShards(1))
67+
.Mappings(m => m
68+
.Map("_default_", tm => tm
69+
.DynamicTemplates(t => t
70+
.DynamicTemplate("base", dt => dt
71+
.Match("*")
72+
.MatchMappingType("*")
73+
.Mapping(mm => mm
74+
.Generic(g => g
75+
.Index(FieldIndexOption.No)
76+
)
77+
)
78+
)
79+
)
80+
)
81+
);
4182

4283
protected override PutIndexTemplateRequest Initializer => new PutIndexTemplateRequest(CallIsolatedValue)
4384
{
@@ -47,6 +88,26 @@ protected override LazyResponses ClientUsage() => Calls(
4788
Settings = new Nest.IndexSettings
4889
{
4990
NumberOfShards = 1
91+
},
92+
Mappings = new Mappings
93+
{
94+
{ "_default_", new TypeMapping
95+
{
96+
DynamicTemplates = new DynamicTemplateContainer
97+
{
98+
{ "base", new DynamicTemplate
99+
{
100+
Match = "*",
101+
MatchMappingType = "*",
102+
Mapping = new GenericProperty
103+
{
104+
Index = FieldIndexOption.No
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
50111
}
51112
};
52113
}

0 commit comments

Comments
 (0)