Skip to content

Commit 05e4f09

Browse files
committed
Add ping support
Closes #510
1 parent 62a0309 commit 05e4f09

File tree

10 files changed

+162
-16
lines changed

10 files changed

+162
-16
lines changed

src/Nest/DSL/PingDescriptor.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Elasticsearch.Net;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
internal static class PingRequestPathInfo
11+
{
12+
public static void Update(ElasticsearchPathInfo<PingRequestParameters> pathInfo)
13+
{
14+
pathInfo.HttpMethod = PathInfoHttpMethod.HEAD;
15+
}
16+
}
17+
18+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
19+
public interface IPingRequest : IRequest<PingRequestParameters>
20+
{
21+
}
22+
23+
public partial class PingRequest : BasePathRequest<PingRequestParameters>, IPingRequest
24+
{
25+
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<PingRequestParameters> pathInfo)
26+
{
27+
PingRequestPathInfo.Update(pathInfo);
28+
}
29+
}
30+
31+
public partial class PingDescriptor : BasePathDescriptor<PingDescriptor, PingRequestParameters>, IPingRequest
32+
{
33+
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<PingRequestParameters> pathInfo)
34+
{
35+
PingRequestPathInfo.Update(pathInfo);
36+
}
37+
}
38+
}

src/Nest/DSL/_Descriptors.generated.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,16 +4701,10 @@ public PercolateDescriptor<T> VersionType(VersionType version_type)
47014701
///http://www.elasticsearch.org/guide/
47024702
///</pre>
47034703
///</summary>
4704-
public partial class PingDescriptor : BaseRequest<PingRequestParameters>
4704+
public partial class PingDescriptor
47054705
{
47064706

47074707

4708-
4709-
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<PingRequestParameters> pathInfo)
4710-
{
4711-
throw new NotImplementedException();
4712-
}
4713-
47144708

47154709
}
47164710

src/Nest/DSL/_Requests.generated.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4223,14 +4223,8 @@ public VersionType VersionType
42234223
///http://www.elasticsearch.org/guide/
42244224
///</pre>
42254225
///</summary>
4226-
public partial class PingRequest : BasePathRequest<PingRequestParameters>
4226+
public partial class PingRequest
42274227
{
4228-
4229-
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<PingRequestParameters> pathInfo)
4230-
{
4231-
throw new NotImplementedException();
4232-
}
4233-
42344228
}
42354229

42364230

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
public interface IPingResponse : IResponse
10+
{
11+
}
12+
13+
[JsonObject]
14+
public class PingResponse : BaseResponse, IPingResponse
15+
{
16+
}
17+
}

src/Nest/ElasticClient-Ping.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Elasticsearch.Net;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Nest
9+
{
10+
public partial class ElasticClient
11+
{
12+
/// <inheritdoc />
13+
public IPingResponse Ping(Func<PingDescriptor, PingDescriptor> pingSelector = null)
14+
{
15+
pingSelector = pingSelector ?? (s => s);
16+
return this.Dispatch<PingDescriptor, PingRequestParameters, PingResponse>(
17+
pingSelector,
18+
(p, d) => this.RawDispatch.PingDispatch<PingResponse>(p)
19+
);
20+
}
21+
22+
/// <inheritdoc />
23+
public Task<IPingResponse> PingAsync(Func<PingDescriptor, PingDescriptor> pingSelector = null)
24+
{
25+
pingSelector = pingSelector ?? (s => s);
26+
return this.DispatchAsync<PingDescriptor, PingRequestParameters, PingResponse, IPingResponse>(
27+
pingSelector,
28+
(p, d) => this.RawDispatch.PingDispatchAsync<PingResponse>(p)
29+
);
30+
}
31+
32+
/// <inheritdoc />
33+
public IPingResponse Ping(IPingRequest pingRequest)
34+
{
35+
return this.Dispatch<IPingRequest, PingRequestParameters, PingResponse>(
36+
pingRequest,
37+
(p, d) => this.RawDispatch.PingDispatch<PingResponse>(p)
38+
);
39+
}
40+
41+
/// <inheritdoc />
42+
public Task<IPingResponse> PingAsync(IPingRequest pingRequest)
43+
{
44+
return this.DispatchAsync<IPingRequest, PingRequestParameters, PingResponse, IPingResponse>(
45+
pingRequest,
46+
(p, d) => this.RawDispatch.PingDispatchAsync<PingResponse>(p)
47+
);
48+
}
49+
}
50+
}

src/Nest/ElasticClient.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ D descriptor
8181
var response = dispatch(pathInfo, descriptor);
8282
return ResultsSelector<D, Q, R>(response, descriptor);
8383
}
84-
84+
8585
private static R ResultsSelector<D, Q, R>(
8686
ElasticsearchResponse<R> c,
8787
D descriptor
@@ -95,6 +95,12 @@ D descriptor
9595

9696
if (c.Success || statusCodeAllowed)
9797
{
98+
if (c.Response == null)
99+
{
100+
var bodilessResponse = CreateBodilessInstance<R>(c);
101+
return bodilessResponse;
102+
}
103+
98104
c.Response.IsValid = true;
99105
return c.Response;
100106
}
@@ -106,7 +112,14 @@ private static R CreateInvalidInstance<R>(IElasticsearchResponse response) where
106112
{
107113
var r = (R)typeof(R).CreateInstance();
108114
((IResponseWithRequestInformation)r).RequestInformation = response;
109-
r.IsValid = false;
115+
return r;
116+
}
117+
118+
private static R CreateBodilessInstance<R>(IElasticsearchResponse response) where R : BaseResponse
119+
{
120+
var r = (R)typeof(R).CreateInstance();
121+
((IResponseWithRequestInformation)r).RequestInformation = response;
122+
r.IsValid = true;
110123
return r;
111124
}
112125

src/Nest/IElasticClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,5 +1306,19 @@ Task<IGetFieldMappingResponse> GetFieldMappingAsync<T>(Func<GetFieldMappingDescr
13061306

13071307
/// <inheritdoc />
13081308
Task<IGetFieldMappingResponse> GetFieldMappingAsync(IGetFieldMappingRequest getFieldMappingRequest);
1309+
1310+
/// <summary>
1311+
/// Executes a HEAD request to the cluster to determine whether it's up or not.
1312+
/// </summary>
1313+
IPingResponse Ping(Func<PingDescriptor, PingDescriptor> pingSelector = null);
1314+
1315+
/// <inheritdoc />
1316+
Task<IPingResponse> PingAsync(Func<PingDescriptor, PingDescriptor> pingSelector = null);
1317+
1318+
/// <inheritdoc />
1319+
IPingResponse Ping(IPingRequest pingRequest);
1320+
1321+
/// <inheritdoc />
1322+
Task<IPingResponse> PingAsync(IPingRequest pingRequest);
13091323
}
13101324
}

src/Nest/Nest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<Compile Include="Domain\Responses\GetFieldMappingResponse.cs" />
169169
<Compile Include="Domain\Responses\MultiPercolateResponse.cs" />
170170
<Compile Include="Domain\Responses\NodesHotThreadsResponse.cs" />
171+
<Compile Include="Domain\Responses\PingResponse.cs" />
171172
<Compile Include="DSL\Filter\GeoShapeCircleFilterDescriptor.cs" />
172173
<Compile Include="DSL\Filter\GeoShapeMultiLineStringFilterDescriptor.cs" />
173174
<Compile Include="DSL\Filter\GeoShapeMultiPointFilterDescriptor.cs" />
@@ -183,6 +184,7 @@
183184
<Compile Include="DSL\NodesHotThreadsDescriptor.cs" />
184185
<Compile Include="DSL\Paths\IndexOptionalNamePathDescriptor.cs" />
185186
<Compile Include="DSL\Paths\IndicesOptionalTypesNamePathDecriptor.cs" />
187+
<Compile Include="DSL\PingDescriptor.cs" />
186188
<Compile Include="DSL\Query\SubDescriptors\MoreLikeThisQueryDocumentsDescriptor.cs" />
187189
<Compile Include="DSL\Query\SpanMultiTermQueryDescriptor.cs" />
188190
<Compile Include="DSL\TypeExistsDescriptor.cs" />
@@ -257,6 +259,7 @@
257259
<Compile Include="ElasticClient-DeleteIndex.cs" />
258260
<Compile Include="ElasticClient-MappingGetField.cs" />
259261
<Compile Include="ElasticClient-MultiPercolate.cs" />
262+
<Compile Include="ElasticClient-Ping.cs" />
260263
<Compile Include="ElasticClient-TypeExists.cs" />
261264
<Compile Include="ElasticClient-Explain.cs" />
262265
<Compile Include="Enums\FieldDataLoading.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Nest.Tests.Integration.Core
10+
{
11+
[TestFixture]
12+
public class PingTests : IntegrationTests
13+
{
14+
[Test]
15+
public void PingTest()
16+
{
17+
var r = this.Client.Ping();
18+
r.IsValid.Should().BeTrue();
19+
r.ConnectionStatus.HttpStatusCode.ShouldBeEquivalentTo(200);
20+
}
21+
}
22+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<Compile Include="Core\MultiPercolate\MultiPercolateTests.cs" />
112112
<Compile Include="Core\Percolate\PercolateTests.cs" />
113113
<Compile Include="Core\Map\Mapping\MappingTransformTests.cs" />
114+
<Compile Include="Core\PingTests.cs" />
114115
<Compile Include="Core\Repository\RestoreTests.cs" />
115116
<Compile Include="Core\Repository\CreateRepositoryTests.cs" />
116117
<Compile Include="Core\ClearScroll\ClearScrollTests.cs" />

0 commit comments

Comments
 (0)