Skip to content

Commit 9ef2536

Browse files
committed
Implemented support for getmapping to return the results for multiple mappings fix #1066
1 parent a05871a commit 9ef2536

File tree

4 files changed

+185
-15
lines changed

4 files changed

+185
-15
lines changed

src/Nest/Domain/Responses/GetMappingResponse.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ namespace Nest
77
{
88
public interface IGetMappingResponse : IResponse
99
{
10+
Dictionary<string, IList<TypeMapping>> Mappings { get; }
1011
RootObjectMapping Mapping { get; }
1112
void Accept(IMappingVisitor visitor);
1213
}
1314

15+
public class TypeMapping
16+
{
17+
public string TypeName { get; internal set; }
18+
public RootObjectMapping Mapping { get; internal set; }
19+
}
20+
1421
internal class GetRootObjectMappingWrapping : Dictionary<string, Dictionary<string, Dictionary<string, RootObjectMapping>>>
1522
{
1623

@@ -21,31 +28,43 @@ public class GetMappingResponse : BaseResponse, IGetMappingResponse
2128
public GetMappingResponse()
2229
{
2330
this.IsValid = true;
31+
this.Mappings = new Dictionary<string, IList<TypeMapping>>();
2432
}
2533

34+
2635
internal GetMappingResponse(IElasticsearchResponse status, GetRootObjectMappingWrapping dict)
2736
{
37+
this.Mappings = new Dictionary<string, IList<TypeMapping>>();
2838
this.IsValid = status.Success && dict != null && dict.Count > 0;
2939
if (!this.IsValid) return;
30-
var firstNode = dict.First();
31-
if (!firstNode.Value.HasAny())
32-
return;
33-
var mappingNode = firstNode.Value["mappings"];
34-
if (mappingNode == null)
40+
foreach (var index in dict)
3541
{
36-
this.IsValid = false;
37-
return;
38-
}
39-
var mapping = mappingNode.First();
40-
if (mapping.Value == null)
41-
{
42-
this.IsValid = false;
43-
return;
42+
if (index.Value == null || !index.Value.ContainsKey("mappings"))
43+
continue;
44+
45+
var mappings = index.Value["mappings"];
46+
this.Mappings.Add(index.Key, new List<TypeMapping>());
47+
if (mappings == null) continue;
48+
foreach (var mapping in mappings)
49+
{
50+
if (mapping.Value == null) continue;
51+
var typeMapping = new TypeMapping
52+
{
53+
TypeName = mapping.Key,
54+
Mapping = mapping.Value
55+
};
56+
mapping.Value.Name = mapping.Key;
57+
this.Mappings[index.Key].Add(typeMapping);
58+
}
4459
}
45-
mapping.Value.Name = mapping.Key;
46-
this.Mapping = mapping.Value;
60+
61+
this.Mapping = this.Mappings.Where(kv=>kv.Value.HasAny(v=>v.Mapping != null))
62+
.SelectMany(kv=>kv.Value)
63+
.Select(tm=>tm.Mapping)
64+
.FirstOrDefault(t=>t != null);
4765
}
4866

67+
public Dictionary<string, IList<TypeMapping>> Mappings { get; internal set; }
4968
public RootObjectMapping Mapping { get; internal set; }
5069

5170
public void Accept(IMappingVisitor visitor)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using Nest.Tests.MockData.Domain;
6+
using NUnit.Framework;
7+
8+
namespace Nest.Tests.Integration.Mapping
9+
{
10+
[TestFixture]
11+
public class GetMultipleMappingTests : IntegrationTests
12+
{
13+
private void TestElasticsearchProjectMapping(RootObjectMapping typeMapping)
14+
{
15+
Assert.NotNull(typeMapping);
16+
Assert.AreEqual("string", typeMapping.Properties["content"].Type.Name);
17+
Assert.AreEqual("string", typeMapping.Properties["country"].Type.Name);
18+
Assert.AreEqual("double", typeMapping.Properties["doubleValue"].Type.Name);
19+
Assert.AreEqual("long", typeMapping.Properties["longValue"].Type.Name);
20+
Assert.AreEqual("boolean", typeMapping.Properties["boolValue"].Type.Name);
21+
Assert.AreEqual("integer", typeMapping.Properties["intValues"].Type.Name);
22+
Assert.AreEqual("float", typeMapping.Properties["floatValues"].Type.Name);
23+
Assert.AreEqual("string", typeMapping.Properties["name"].Type.Name);
24+
Assert.AreEqual("date", typeMapping.Properties["startedOn"].Type.Name);
25+
Assert.AreEqual("long", typeMapping.Properties["stupidIntIWantAsLong"].Type.Name);
26+
Assert.AreEqual("float", typeMapping.Properties["floatValue"].Type.Name);
27+
Assert.AreEqual("integer", typeMapping.Properties["id"].Type.Name);
28+
Assert.AreEqual("integer", typeMapping.Properties["loc"].Type.Name);
29+
Assert.AreEqual("geo_point", typeMapping.Properties["origin"].Type.Name);
30+
Assert.AreEqual("object", typeMapping.Properties["product"].Type.Name);
31+
32+
var productMapping = typeMapping.Properties["product"] as ObjectMapping;
33+
Assert.NotNull(productMapping);
34+
Assert.AreEqual("string", productMapping.Properties["name"].Type.Name);
35+
Assert.AreEqual("string", productMapping.Properties["id"].Type.Name);
36+
37+
var countryMapping = typeMapping.Properties["country"] as StringMapping;
38+
Assert.NotNull(countryMapping);
39+
Assert.AreEqual(FieldIndexOption.NotAnalyzed, countryMapping.Index);
40+
}
41+
42+
private void TestPersonMapping(RootObjectMapping typeMapping)
43+
{
44+
Assert.NotNull(typeMapping);
45+
Assert.AreEqual("string", typeMapping.Properties["email"].Type.Name);
46+
var firstNameMapping = typeMapping.Properties["email"] as StringMapping;
47+
firstNameMapping.Should().NotBeNull();
48+
firstNameMapping.Index.Should().Be(FieldIndexOption.NotAnalyzed);
49+
}
50+
51+
[Test]
52+
public void GetSameMappingFromTwoDifferentIndices()
53+
{
54+
var indices = new[]
55+
{
56+
ElasticsearchConfiguration.NewUniqueIndexName(),
57+
ElasticsearchConfiguration.NewUniqueIndexName()
58+
};
59+
60+
var x = this.Client.CreateIndex(indices.First(), s => s
61+
.AddMapping<ElasticsearchProject>(m => m.MapFromAttributes())
62+
);
63+
Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());
64+
65+
x = this.Client.CreateIndex(indices.Last(), s => s
66+
.AddMapping<ElasticsearchProject>(m => m.MapFromAttributes())
67+
);
68+
Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());
69+
70+
var response = this.Client.GetMapping<ElasticsearchProject>(i => i
71+
.Index(string.Join(",", indices))
72+
.Type("elasticsearchprojects")
73+
);
74+
response.Should().NotBeNull();
75+
response.Mappings.Should().NotBeEmpty()
76+
.And.HaveCount(2);
77+
foreach (var indexMapping in response.Mappings)
78+
{
79+
var indexName = indexMapping.Key;
80+
indices.Should().Contain(indexName);
81+
var mappings = indexMapping.Value;
82+
mappings.Should().NotBeEmpty().And.HaveCount(1);
83+
foreach (var mapping in mappings)
84+
{
85+
mapping.TypeName.Should().Be("elasticsearchprojects");
86+
TestElasticsearchProjectMapping(mapping.Mapping);
87+
}
88+
}
89+
90+
91+
}
92+
93+
[Test]
94+
public void GetDifferentMappingsFromMultipleIndices()
95+
{
96+
var indices = new[]
97+
{
98+
ElasticsearchConfiguration.NewUniqueIndexName(),
99+
ElasticsearchConfiguration.NewUniqueIndexName()
100+
};
101+
102+
var x = this.Client.CreateIndex(indices.First(), s => s
103+
.AddMapping<ElasticsearchProject>(m => m.MapFromAttributes())
104+
.AddMapping<Person>(m => m.MapFromAttributes())
105+
);
106+
Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());
107+
108+
x = this.Client.CreateIndex(indices.Last(), s => s
109+
.AddMapping<ElasticsearchProject>(m => m.MapFromAttributes())
110+
.AddMapping<GeoLocation>(m => m.MapFromAttributes())
111+
);
112+
Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());
113+
114+
var response = this.Client.GetMapping(new GetMappingRequest(string.Join(",", indices), "*"));
115+
response.Should().NotBeNull();
116+
response.Mappings.Should().NotBeEmpty()
117+
.And.HaveCount(2);
118+
foreach (var indexMapping in response.Mappings)
119+
{
120+
var indexName = indexMapping.Key;
121+
indices.Should().Contain(indexName);
122+
var mappings = indexMapping.Value;
123+
mappings.Should().NotBeEmpty().And.HaveCount(2);
124+
mappings.Should().Contain(m => m.TypeName == "elasticsearchprojects")
125+
.And.Contain(m=>m.TypeName == (indexName == indices.First() ? "person" : "geolocation"));
126+
foreach (var mapping in mappings)
127+
{
128+
switch (mapping.TypeName)
129+
{
130+
case "elasticsearchprojects":
131+
TestElasticsearchProjectMapping(mapping.Mapping);
132+
break;
133+
case "person":
134+
TestPersonMapping(mapping.Mapping);
135+
break;
136+
case "geolocation":
137+
break;
138+
default:
139+
Assert.Fail("Unexpected mapping found {0}", mapping.TypeName);
140+
break;
141+
}
142+
}
143+
}
144+
145+
146+
}
147+
148+
}
149+
}

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<Compile Include="Indices\StatsTests.cs" />
178178
<Compile Include="Core\AsyncTests.cs" />
179179
<Compile Include="Mapping\MappingVisitorTests.cs" />
180+
<Compile Include="Mapping\GetMultipleMappingTests.cs" />
180181
<Compile Include="Reproduce\Reproduce769Tests.cs" />
181182
<Compile Include="Reproduce\Reproduce945Tests.cs" />
182183
<Compile Include="Reproduce\Reproduce953Tests.cs" />

src/Tests/Nest.Tests.MockData/Domain/Person.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Person
1212
[ElasticProperty(Index = FieldIndexOption.Analyzed)]
1313
public string LastName { get; set; }
1414
public int Age { get; set; }
15+
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
1516
public string Email { get; set; }
1617
public DateTime DateOfBirth { get; set; }
1718
[ElasticProperty(Type = FieldType.GeoPoint)]

0 commit comments

Comments
 (0)