Skip to content

Commit fe16bb7

Browse files
committed
Forward port GetMany changes
cherry picked from 4b3744b
1 parent 5a30516 commit fe16bb7

File tree

6 files changed

+186
-17
lines changed

6 files changed

+186
-17
lines changed

src/Nest/Document/Multiple/MultiGet/EllasticClient-GetMany.cs renamed to src/Nest/Document/Multiple/MultiGet/ElasticClient-GetMany.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Nest
1111
public static class GetManyExtensions
1212
{
1313
/// <summary>
14-
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
15-
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
14+
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
15+
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
1616
/// provided by the get API.
1717
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
1818
/// </summary>
@@ -21,16 +21,16 @@ public static class GetManyExtensions
2121
/// <param name="ids">IEnumerable of ids as string for the documents to fetch</param>
2222
/// <param name="index">Optionally override the default inferred index name for T</param>
2323
/// <param name="type">Optionally overiide the default inferred typename for T</param>
24-
public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null)
24+
public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client, IEnumerable<string> ids, IndexName index = null, TypeName type = null)
2525
where T : class
2626
{
27-
var result = client.MultiGet(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type)));
27+
var result = client.MultiGet(s => s.GetMany<T>(ids).Index(index).Type(type));
2828
return result.GetMany<T>(ids);
2929
}
30-
30+
3131
/// <summary>
32-
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
33-
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
32+
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
33+
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
3434
/// provided by the get API.
3535
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
3636
/// </summary>
@@ -39,15 +39,15 @@ public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client
3939
/// <param name="ids">IEnumerable of ids as ints for the documents to fetch</param>
4040
/// <param name="index">Optionally override the default inferred index name for T</param>
4141
/// <param name="type">Optionally overiide the default inferred typename for T</param>
42-
public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client, IEnumerable<long> ids, string index = null, string type = null)
42+
public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client, IEnumerable<long> ids, IndexName index = null, TypeName type = null)
4343
where T : class
4444
{
4545
return client.GetMany<T>(ids.Select(i => i.ToString(CultureInfo.InvariantCulture)), index, type);
4646
}
4747

4848
/// <summary>
49-
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
50-
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
49+
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
50+
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
5151
/// provided by the get API.
5252
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
5353
/// </summary>
@@ -56,16 +56,16 @@ public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client
5656
/// <param name="ids">IEnumerable of ids as string for the documents to fetch</param>
5757
/// <param name="index">Optionally override the default inferred index name for T</param>
5858
/// <param name="type">Optionally overiide the default inferred typename for T</param>
59-
public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null)
59+
public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IElasticClient client, IEnumerable<string> ids, IndexName index = null, TypeName type = null)
6060
where T : class
6161
{
62-
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type))).ConfigureAwait(false);
62+
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids).Index(index).Type(type)).ConfigureAwait(false);
6363
return response.GetMany<T>(ids);
6464
}
6565

6666
/// <summary>
67-
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
68-
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
67+
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
68+
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
6969
/// provided by the get API.
7070
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
7171
/// </summary>
@@ -74,10 +74,10 @@ public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IEla
7474
/// <param name="ids">IEnumerable of ids as ints for the documents to fetch</param>
7575
/// <param name="index">Optionally override the default inferred index name for T</param>
7676
/// <param name="type">Optionally overiide the default inferred typename for T</param>
77-
public static Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IElasticClient client, IEnumerable<long> ids, string index = null, string type = null)
77+
public static Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(this IElasticClient client, IEnumerable<long> ids, IndexName index = null, TypeName type = null)
7878
where T : class
7979
{
8080
return client.GetManyAsync<T>(ids.Select(i => i.ToString(CultureInfo.InvariantCulture)), index, type);
8181
}
8282
}
83-
}
83+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@
548548
<Compile Include="Document\Multiple\DeleteByQuery\ElasticClient-DeleteByQuery.cs" />
549549
<Compile Include="Document\Multiple\MultiGet\ElasticClient-MultiGet.cs" />
550550
<Compile Include="Document\Multiple\MultiGet\ElasticClient-SourceMany.cs" />
551-
<Compile Include="Document\Multiple\MultiGet\EllasticClient-GetMany.cs" />
551+
<Compile Include="Document\Multiple\MultiGet\ElasticClient-GetMany.cs" />
552552
<Compile Include="Document\Multiple\MultiGet\Request\IMultiGetOperation.cs" />
553553
<Compile Include="Document\Multiple\MultiGet\Request\MultiGetOperation.cs" />
554554
<Compile Include="Document\Multiple\MultiGet\Request\MultiGetRequest.cs" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using Nest;
8+
using Tests.Framework;
9+
using Tests.Framework.Integration;
10+
using Tests.Framework.MockData;
11+
using Xunit;
12+
13+
namespace Tests.Document.Multiple.MultiGet
14+
{
15+
[Collection(TypeOfCluster.ReadOnly)]
16+
public class GetManyApiTests
17+
{
18+
private readonly ReadOnlyCluster _cluster;
19+
private readonly IEnumerable<long> _ids = Developer.Developers.Select(d => (long)d.Id).Take(10);
20+
private readonly IElasticClient _client;
21+
22+
public GetManyApiTests(ReadOnlyCluster cluster)
23+
{
24+
_cluster = cluster;
25+
_client = _cluster.Client();
26+
}
27+
28+
[I]
29+
public void UsesDefaultIndexAndInferredType()
30+
{
31+
var response = _client.GetMany<Developer>(_ids);
32+
response.Count().Should().Be(10);
33+
foreach (var hit in response)
34+
{
35+
hit.Index.Should().NotBeNullOrWhiteSpace();
36+
hit.Type.Should().NotBeNullOrWhiteSpace();
37+
hit.Id.Should().NotBeNullOrWhiteSpace();
38+
hit.Found.Should().BeTrue();
39+
}
40+
}
41+
42+
[I]
43+
public async Task UsesDefaultIndexAndInferredTypeAsync()
44+
{
45+
var response = await _client.GetManyAsync<Developer>(_ids);
46+
response.Count().Should().Be(10);
47+
foreach (var hit in response)
48+
{
49+
hit.Index.Should().NotBeNullOrWhiteSpace();
50+
hit.Type.Should().NotBeNullOrWhiteSpace();
51+
hit.Id.Should().NotBeNullOrWhiteSpace();
52+
hit.Found.Should().BeTrue();
53+
}
54+
}
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Nest;
7+
using Tests.Framework;
8+
using Tests.Framework.MockData;
9+
using static Tests.Framework.CapturingUrlTester;
10+
11+
namespace Tests.Document.Multiple.MultiGet
12+
{
13+
public class GetManyUrlTests : IUrlTests
14+
{
15+
private static readonly string[] StringIds = { "1" };
16+
private static readonly long[] LongIds = { 1 };
17+
18+
[U]
19+
public async Task Urls()
20+
{
21+
await POST("/_mget")
22+
.Request(c => c.GetMany<Project>(StringIds))
23+
.Request(c => c.GetMany<Project>(LongIds))
24+
.RequestAsync(c => c.GetManyAsync<Project>(StringIds))
25+
.RequestAsync(c => c.GetManyAsync<Project>(LongIds))
26+
;
27+
28+
await POST("/project/_mget")
29+
.Request(c => c.GetMany<Project>(StringIds, "project"))
30+
.Request(c => c.GetMany<Project>(LongIds, "project"))
31+
.RequestAsync(c => c.GetManyAsync<Project>(StringIds, "project"))
32+
.RequestAsync(c => c.GetManyAsync<Project>(LongIds, "project"))
33+
;
34+
35+
await POST("/project/project/_mget")
36+
.Request(c => c.GetMany<Project>(StringIds, "project", "project"))
37+
.Request(c => c.GetMany<Project>(LongIds, "project", "project"))
38+
.RequestAsync(c => c.GetManyAsync<Project>(StringIds, "project", "project"))
39+
.RequestAsync(c => c.GetManyAsync<Project>(LongIds, "project", "project"))
40+
;
41+
}
42+
}
43+
}

src/Tests/Framework/EndpointTests/UrlTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,72 @@ public static void ComparePathAndQuerstring(string expectedPathAndUri, Uri actua
115115
public static UrlTester DELETE(string url) => new UrlTester(HttpMethod.DELETE, url);
116116
public static string EscapeUriString(string s) => Uri.EscapeDataString(s);
117117
}
118+
119+
public static class CapturingUrlTesterExtensions
120+
{
121+
public static async Task<CapturingUrlTester> RequestAsync<TResponse>(this Task<CapturingUrlTester> tester, Func<IElasticClient, Task<TResponse>> call)
122+
=> await (await tester).WhenCallingAsync(call, "request async");
123+
124+
public static async Task<CapturingUrlTester> FluentAsync<TResponse>(this Task<CapturingUrlTester> tester, Func<IElasticClient, Task<TResponse>> call)
125+
=> await (await tester).WhenCallingAsync(call, "fluent async");
126+
}
127+
128+
public class CapturingUrlTester : SerializationTestBase
129+
{
130+
protected string ExpectedUrl { get; set; }
131+
protected HttpMethod ExpectedHttpMethod { get; set; }
132+
protected IApiCallDetails CallDetails { get; set; }
133+
134+
protected override object ExpectJson => null;
135+
136+
protected override IElasticClient Client => TestClient.GetInMemoryClient(c => c
137+
.PrettyJson(false)
138+
.OnRequestCompleted(h => CallDetails = h));
139+
140+
internal CapturingUrlTester(HttpMethod method, string expectedUrl)
141+
{
142+
this.ExpectedHttpMethod = method;
143+
this.ExpectedUrl = expectedUrl;
144+
}
145+
146+
public CapturingUrlTester Fluent<TResponse>(Func<IElasticClient, TResponse> call) => WhenCalling(call, "fluent");
147+
148+
public Task<CapturingUrlTester> FluentAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call)
149+
=> WhenCallingAsync(call, "fluent async");
150+
151+
public CapturingUrlTester Request<TResponse>(Func<IElasticClient, TResponse> call)
152+
=> WhenCalling(call, "request");
153+
154+
public Task<CapturingUrlTester> RequestAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call)
155+
=> WhenCallingAsync(call, "request async");
156+
157+
internal CapturingUrlTester WhenCalling<TResponse>(Func<IElasticClient, TResponse> call, string typeOfCall)
158+
{
159+
call(this.Client);
160+
return Assert(typeOfCall, CallDetails);
161+
}
162+
163+
internal async Task<CapturingUrlTester> WhenCallingAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call, string typeOfCall)
164+
{
165+
await call(this.Client);
166+
return Assert(typeOfCall, CallDetails);
167+
}
168+
169+
private CapturingUrlTester Assert(string typeOfCall, IApiCallDetails callDetails)
170+
{
171+
var url = callDetails.Uri.PathAndQuery;
172+
url.Should().Be(this.ExpectedUrl, $"when calling the {typeOfCall} Api");
173+
callDetails.HttpMethod.Should().Be(this.ExpectedHttpMethod, typeOfCall);
174+
return this;
175+
}
176+
177+
public static CapturingUrlTester ExpectUrl(HttpMethod method, string url) => new CapturingUrlTester(method, url);
178+
public static CapturingUrlTester POST(string url) => new CapturingUrlTester(HttpMethod.POST, url);
179+
public static CapturingUrlTester PUT(string url) => new CapturingUrlTester(HttpMethod.PUT, url);
180+
public static CapturingUrlTester GET(string url) => new CapturingUrlTester(HttpMethod.GET, url);
181+
public static CapturingUrlTester HEAD(string url) => new CapturingUrlTester(HttpMethod.HEAD, url);
182+
public static CapturingUrlTester DELETE(string url) => new CapturingUrlTester(HttpMethod.DELETE, url);
183+
184+
public static string EscapeUriString(string s) => Uri.EscapeDataString(s);
185+
}
118186
}

src/Tests/Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
<Compile Include="Cat\CatShards\CatShardsUrlTests.cs" />
211211
<Compile Include="Cat\CatThreadPool\CatThreadpoolApiTests.cs" />
212212
<Compile Include="Cat\CatThreadPool\CatThreadPoolUrlTests.cs" />
213+
<Compile Include="Document\Multiple\MultiGet\GetManyApiTests.cs" />
214+
<Compile Include="Document\Multiple\MultiGet\GetManyUrlTests.cs" />
213215
<Compile Include="Document\Multiple\ReindexRethrottle\ReindexRethrottleApiTests.cs" />
214216
<Compile Include="Document\Multiple\ReindexRethrottle\ReindexRethrottleUrlTests.cs" />
215217
<Compile Include="Framework\EndpointTests\ApiIntegrationTestBase.cs" />

0 commit comments

Comments
 (0)