Skip to content

Commit 486b4d6

Browse files
committed
Merge branch 'fix/2.x-get-mappings' into 2.x
2 parents c6df5be + 80c064f commit 486b4d6

File tree

7 files changed

+84
-55
lines changed

7 files changed

+84
-55
lines changed

src/Nest/CommonAbstractions/LowLevelDispatch/LowLevelDispatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static Exception InvalidDispatch(string apiCall, IRequest provided, Htt
3232
return new ArgumentException(sb.ToString());
3333
}
3434

35-
private static Regex ReplaceParams = new Regex(@"\{(.+?)\}");
35+
private static readonly Regex ReplaceParams = new Regex(@"\{(.+?)\}");
3636

3737
internal static string PrettyPrintEndpoint(IRequest request, string endpoint)
3838
{

src/Nest/ElasticClient.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class ElasticClient : IElasticClient, IHighLevelToLowLevelDispatc
1313
private IHighLevelToLowLevelDispatcher Dispatcher => this;
1414

1515
private LowLevelDispatch LowLevelDispatch { get; }
16-
16+
1717
private ITransport<IConnectionSettingsValues> Transport { get; }
1818

1919
public IElasticsearchSerializer Serializer => this.Transport.Settings.Serializer;
@@ -24,7 +24,7 @@ public partial class ElasticClient : IElasticClient, IHighLevelToLowLevelDispatc
2424

2525
public ElasticClient() : this(new ConnectionSettings(new Uri("http://localhost:9200"))) { }
2626
public ElasticClient(Uri uri) : this(new ConnectionSettings(uri)) { }
27-
public ElasticClient(IConnectionSettingsValues connectionSettings)
27+
public ElasticClient(IConnectionSettingsValues connectionSettings)
2828
: this(new Transport<IConnectionSettingsValues>(connectionSettings ?? new ConnectionSettings())) { }
2929

3030
public ElasticClient(ITransport<IConnectionSettingsValues> transport)
@@ -40,8 +40,8 @@ public ElasticClient(ITransport<IConnectionSettingsValues> transport)
4040
}
4141

4242
TResponse IHighLevelToLowLevelDispatcher.Dispatch<TRequest, TQueryString, TResponse>(
43-
TRequest request,
44-
Func<TRequest, PostData<object>,
43+
TRequest request,
44+
Func<TRequest, PostData<object>,
4545
ElasticsearchResponse<TResponse>> dispatch
4646
) => this.Dispatcher.Dispatch<TRequest,TQueryString,TResponse>(request, null, dispatch);
4747

@@ -58,19 +58,17 @@ Func<TRequest, PostData<object>, ElasticsearchResponse<TResponse>> dispatch
5858
}
5959

6060
Task<TResponseInterface> IHighLevelToLowLevelDispatcher.DispatchAsync<TRequest, TQueryString, TResponse, TResponseInterface>(
61-
TRequest descriptor,
61+
TRequest descriptor,
6262
Func<TRequest, PostData<object>, Task<ElasticsearchResponse<TResponse>>> dispatch
6363
) => this.Dispatcher.DispatchAsync<TRequest,TQueryString,TResponse,TResponseInterface>(descriptor, null, dispatch);
6464

6565
async Task<TResponseInterface> IHighLevelToLowLevelDispatcher.DispatchAsync<TRequest, TQueryString, TResponse, TResponseInterface>(
66-
TRequest request,
67-
Func<IApiCallDetails, Stream, TResponse> responseGenerator,
66+
TRequest request,
67+
Func<IApiCallDetails, Stream, TResponse> responseGenerator,
6868
Func<TRequest, PostData<object>, Task<ElasticsearchResponse<TResponse>>> dispatch
6969
)
7070
{
7171
request.RouteValues.Resolve(this.ConnectionSettings);
72-
request.RequestParameters.DeserializationOverride(responseGenerator);
73-
7472
request.RequestParameters.DeserializationOverride(responseGenerator);
7573
var response = await dispatch(request, request).ConfigureAwait(false);
7674
return ResultsSelector(response);
@@ -80,7 +78,7 @@ private static TResponse ResultsSelector<TResponse>(ElasticsearchResponse<TRespo
8078
where TResponse : ResponseBase =>
8179
c.Body ?? CreateInvalidInstance<TResponse>(c);
8280

83-
private static TResponse CreateInvalidInstance<TResponse>(IApiCallDetails response)
81+
private static TResponse CreateInvalidInstance<TResponse>(IApiCallDetails response)
8482
where TResponse : ResponseBase
8583
{
8684
var r = typeof(TResponse).CreateInstance<TResponse>();

src/Nest/Indices/MappingManagement/GetMapping/ElasticClient-GetMapping.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public partial class ElasticClient
3131
{
3232
/// <inheritdoc/>
3333
public IGetMappingResponse GetMapping<T>(Func<GetMappingDescriptor<T>, IGetMappingRequest> selector = null)
34-
where T : class =>
34+
where T : class =>
3535
this.GetMapping(selector.InvokeOrDefault(new GetMappingDescriptor<T>()));
3636

3737
/// <inheritdoc/>
38-
public IGetMappingResponse GetMapping(IGetMappingRequest request) =>
38+
public IGetMappingResponse GetMapping(IGetMappingRequest request) =>
3939
this.Dispatcher.Dispatch<IGetMappingRequest, GetMappingRequestParameters, GetMappingResponse>(
4040
request,
4141
new GetMappingConverter((r, s) => DeserializeGetMappingResponse(r, request, s)),
@@ -48,21 +48,17 @@ public Task<IGetMappingResponse> GetMappingAsync<T>(Func<GetMappingDescriptor<T>
4848
this.GetMappingAsync(selector.InvokeOrDefault(new GetMappingDescriptor<T>()));
4949

5050
/// <inheritdoc/>
51-
public Task<IGetMappingResponse> GetMappingAsync(IGetMappingRequest request) =>
51+
public Task<IGetMappingResponse> GetMappingAsync(IGetMappingRequest request) =>
5252
this.Dispatcher.DispatchAsync<IGetMappingRequest, GetMappingRequestParameters, GetMappingResponse, IGetMappingResponse>(
5353
request,
5454
new GetMappingConverter((r, s) => DeserializeGetMappingResponse(r, request, s)),
5555
(p, d) => this.LowLevelDispatch.IndicesGetMappingDispatchAsync<GetMappingResponse>(p)
5656
);
57-
58-
//TODO this is too geared towards getting a single mapping
57+
5958
private GetMappingResponse DeserializeGetMappingResponse(IApiCallDetails response, IGetMappingRequest d, Stream stream)
6059
{
61-
var dict = response.Success
62-
? Serializer.Deserialize<GetRootObjectMappingWrapping>(stream)
63-
: null;
64-
return new GetMappingResponse(response, dict);
60+
var dict = Serializer.Deserialize<GetRootObjectMappingWrapping>(stream);
61+
return new GetMappingResponse(dict);
6562
}
66-
6763
}
68-
}
64+
}
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using Elasticsearch.Net;
43

54
namespace Nest
65
{
76
public interface IGetMappingResponse : IResponse
87
{
9-
/// <summary>
10-
/// TODO dict|indexname, imappings|
11-
/// </summary>
128
Dictionary<string, IList<TypeMapping>> Mappings { get; }
9+
10+
Dictionary<IndexName, IDictionary<TypeName, TypeMapping>> IndexTypeMappings { get; }
11+
1312
TypeMapping Mapping { get; }
13+
1414
void Accept(IMappingVisitor visitor);
1515
}
1616

@@ -20,40 +20,35 @@ internal class GetRootObjectMappingWrapping : Dictionary<string, Dictionary<stri
2020

2121
public class GetMappingResponse : ResponseBase, IGetMappingResponse
2222
{
23-
public override bool IsValid => base.IsValid && this.Mapping != null;
24-
2523
internal GetMappingResponse() { }
2624

27-
internal GetMappingResponse(IApiCallDetails status, GetRootObjectMappingWrapping dict)
25+
internal GetMappingResponse(GetRootObjectMappingWrapping dict)
2826
{
29-
this.Mappings = new Dictionary<string, IList<TypeMapping>>();
30-
//TODO can dict truely ever be null, whats the response look like when field mapping is not found.
31-
//does status.Success not already reflect this?
32-
//this.IsValid = status.Success && dict != null && dict.Count > 0;
33-
if (!status.Success || dict == null) return;
34-
3527
foreach (var index in dict)
3628
{
37-
if (index.Value == null || !index.Value.ContainsKey("mappings"))
38-
continue;
39-
40-
var mappings = index.Value["mappings"];
41-
this.Mappings.Add(index.Key, new List<TypeMapping>());
42-
if (mappings == null) continue;
43-
foreach (var mapping in mappings)
29+
Dictionary<string, TypeMapping> mappings;
30+
if (index.Value != null && index.Value.TryGetValue("mappings", out mappings))
4431
{
45-
if (mapping.Value == null) continue;
46-
this.Mappings[index.Key].Add(mapping.Value);
32+
this.Mappings.Add(index.Key, new List<TypeMapping>());
33+
this.IndexTypeMappings.Add(index.Key, new Dictionary<TypeName, TypeMapping>());
34+
foreach (var mapping in mappings)
35+
{
36+
if (mapping.Value == null) continue;
37+
this.Mappings[index.Key].Add(mapping.Value);
38+
this.IndexTypeMappings[index.Key].Add(mapping.Key, mapping.Value);
39+
}
4740
}
4841
}
49-
50-
this.Mapping = this.Mappings.Where(kv=>kv.Value.HasAny(v=>v != null))
51-
.SelectMany(kv=>kv.Value)
52-
.FirstOrDefault(t=>t != null);
42+
43+
this.Mapping = this.Mappings.Where(kv => kv.Value.HasAny(v => v != null))
44+
.SelectMany(kv => kv.Value)
45+
.FirstOrDefault(t => t != null);
5346
}
5447

5548
public Dictionary<string, IList<TypeMapping>> Mappings { get; internal set; } = new Dictionary<string, IList<TypeMapping>>();
5649

50+
public Dictionary<IndexName, IDictionary<TypeName, TypeMapping>> IndexTypeMappings { get; internal set; } = new Dictionary<IndexName, IDictionary<TypeName, TypeMapping>>();
51+
5752
public TypeMapping Mapping { get; internal set; }
5853

5954
public void Accept(IMappingVisitor visitor)
@@ -62,4 +57,4 @@ public void Accept(IMappingVisitor visitor)
6257
walker.Accept(this);
6358
}
6459
}
65-
}
60+
}

src/Nest/Mapping/Visitor/MappingWalker.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public MappingWalker(IMappingVisitor visitor)
1515
public void Accept(IGetMappingResponse response)
1616
{
1717
if (response == null) return;
18-
this.Accept(response.Mapping);
18+
19+
foreach (var indexMapping in response.IndexTypeMappings)
20+
foreach (var typeMapping in indexMapping.Value)
21+
{
22+
this.Accept(typeMapping.Value);
23+
}
1924
}
2025

2126
public void Accept(TypeMapping mapping)
@@ -24,7 +29,7 @@ public void Accept(TypeMapping mapping)
2429
this._visitor.Visit(mapping);
2530
this.Accept(mapping.Properties);
2631
}
27-
32+
2833
public void Accept(IProperties properties)
2934
{
3035
if (properties == null) return;
@@ -132,4 +137,4 @@ public void Accept(IProperties properties)
132137
}
133138
}
134139
}
135-
}
140+
}

src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ protected override LazyResponses ClientUsage() => Calls(
3838

3939
protected override void ExpectResponse(IGetMappingResponse response)
4040
{
41-
response.IsValid.Should().BeTrue();
42-
4341
var visitor = new TestVisitor();
4442
response.Accept(visitor);
4543

@@ -55,6 +53,43 @@ protected override void ExpectResponse(IGetMappingResponse response)
5553
}
5654
}
5755

56+
[Collection(IntegrationContext.ReadOnly)]
57+
public class GetMappingNonExistentIndexApiTests : ApiIntegrationTestBase<IGetMappingResponse, IGetMappingRequest, GetMappingDescriptor<Project>, GetMappingRequest>
58+
{
59+
private string _nonExistentIndex = "non-existent-index";
60+
61+
public GetMappingNonExistentIndexApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
62+
63+
protected override LazyResponses ClientUsage() => Calls(
64+
fluent: (client, f) => client.GetMapping<Project>(f),
65+
fluentAsync: (client, f) => client.GetMappingAsync<Project>(f),
66+
request: (client, r) => client.GetMapping(r),
67+
requestAsync: (client, r) => client.GetMappingAsync(r)
68+
);
69+
70+
protected override bool ExpectIsValid => true;
71+
protected override int ExpectStatusCode => 200;
72+
protected override HttpMethod HttpMethod => HttpMethod.GET;
73+
protected override string UrlPath => $"/{_nonExistentIndex}/_mapping?ignore_unavailable=true";
74+
75+
protected override Func<GetMappingDescriptor<Project>, IGetMappingRequest> Fluent => d => d
76+
.Index(_nonExistentIndex)
77+
.AllTypes()
78+
.IgnoreUnavailable();
79+
80+
protected override GetMappingRequest Initializer => new GetMappingRequest(_nonExistentIndex, AllTypes)
81+
{
82+
IgnoreUnavailable = true
83+
};
84+
85+
protected override void ExpectResponse(IGetMappingResponse response)
86+
{
87+
response.Mappings.Should().BeEmpty();
88+
response.IndexTypeMappings.Should().BeEmpty();
89+
response.Mapping.Should().BeNull();
90+
}
91+
}
92+
5893
internal class TestVisitor : IMappingVisitor
5994
{
6095
public TestVisitor()

src/Tests/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# mode either u (unit test), i (integration test) or m (mixed mode)
2-
mode: u
2+
mode: m
33
# the elasticsearch version that should be started
44
elasticsearch_version: 2.3.0
55
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)