Skip to content

Commit 4b3744b

Browse files
committed
Infer index and type from T for GetMany
Set Index and Type on MultiGetDescriptor. If they have values, the specific index and type will be overridden on each get operation with them. Url Tests for GetMany Introduce a CapturingUrlTester to capture call details for client calls that do not return an IResponse directly, such as GetMany<T>
1 parent 2feab1f commit 4b3744b

File tree

6 files changed

+176
-13
lines changed

6 files changed

+176
-13
lines changed

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

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

src/Tests/Framework/UrlTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,69 @@ internal IntermediateUrlTester(Func<ConnectionSettings, ConnectionSettings> sett
102102
public UrlTester DELETE(string url) => new UrlTester(HttpMethod.DELETE, url, _connectionSettingsModifier);
103103
}
104104

105+
public static class CapturingUrlTesterExtensions
106+
{
107+
public static async Task<CapturingUrlTester> RequestAsync<TResponse>(this Task<CapturingUrlTester> tester, Func<IElasticClient, Task<TResponse>> call)
108+
=> await (await tester).WhenCallingAsync(call, "request async");
109+
110+
public static async Task<CapturingUrlTester> FluentAsync<TResponse>(this Task<CapturingUrlTester> tester, Func<IElasticClient, Task<TResponse>> call)
111+
=> await (await tester).WhenCallingAsync(call, "fluent async");
112+
}
113+
114+
public class CapturingUrlTester : SerializationTestBase
115+
{
116+
protected string ExpectedUrl { get; set; }
117+
protected HttpMethod ExpectedHttpMethod { get; set; }
118+
protected IApiCallDetails CallDetails { get; set; }
119+
120+
protected override object ExpectJson => null;
121+
122+
internal CapturingUrlTester(HttpMethod method, string expectedUrl)
123+
{
124+
this.ExpectedHttpMethod = method;
125+
this.ExpectedUrl = expectedUrl;
126+
this._connectionSettingsModifier = (c => c
127+
.PrettyJson(false)
128+
.OnRequestCompleted(h => CallDetails = h));
129+
}
130+
131+
public CapturingUrlTester Fluent<TResponse>(Func<IElasticClient, TResponse> call) => WhenCalling(call, "fluent");
132+
133+
public Task<CapturingUrlTester> FluentAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call)
134+
=> WhenCallingAsync(call, "fluent async");
135+
136+
public CapturingUrlTester Request<TResponse>(Func<IElasticClient, TResponse> call)
137+
=> WhenCalling(call, "request");
138+
139+
public Task<CapturingUrlTester> RequestAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call)
140+
=> WhenCallingAsync(call, "request async");
141+
142+
internal CapturingUrlTester WhenCalling<TResponse>(Func<IElasticClient, TResponse> call, string typeOfCall)
143+
{
144+
call(this.GetClient());
145+
return Assert(typeOfCall, CallDetails);
146+
}
147+
148+
internal async Task<CapturingUrlTester> WhenCallingAsync<TResponse>(Func<IElasticClient, Task<TResponse>> call, string typeOfCall)
149+
{
150+
await call(this.GetClient());
151+
return Assert(typeOfCall, CallDetails);
152+
}
153+
154+
private CapturingUrlTester Assert(string typeOfCall, IApiCallDetails callDetails)
155+
{
156+
var url = callDetails.Uri.PathAndQuery;
157+
url.Should().Be(this.ExpectedUrl, $"when calling the {typeOfCall} Api");
158+
callDetails.HttpMethod.Should().Be(this.ExpectedHttpMethod, typeOfCall);
159+
return this;
160+
}
161+
162+
public static CapturingUrlTester ExpectUrl(HttpMethod method, string url) => new CapturingUrlTester(method, url);
163+
public static CapturingUrlTester POST(string url) => new CapturingUrlTester(HttpMethod.POST, url);
164+
public static CapturingUrlTester PUT(string url) => new CapturingUrlTester(HttpMethod.PUT, url);
165+
public static CapturingUrlTester GET(string url) => new CapturingUrlTester(HttpMethod.GET, url);
166+
public static CapturingUrlTester HEAD(string url) => new CapturingUrlTester(HttpMethod.HEAD, url);
167+
public static CapturingUrlTester DELETE(string url) => new CapturingUrlTester(HttpMethod.DELETE, url);
168+
public static string EscapeUriString(string s) => Uri.EscapeDataString(s);
169+
}
105170
}

src/Tests/Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@
550550
<Compile Include="Cluster\TaskManagement\TasksList\TasksListApiTests.cs" />
551551
<Compile Include="Cluster\TaskManagement\TasksList\TasksListUrlTests.cs" />
552552
<Compile Include="CommonOptions\DistanceUnit\DistanceUnits.doc.cs" />
553+
<Compile Include="Document\Multiple\MultiGet\GetManyApiTests.cs" />
554+
<Compile Include="Document\Multiple\MultiGet\GetManyUrlTests.cs" />
553555
<Compile Include="Document\Multiple\ReindexOnServer\ReindexOnServerUrlTests.cs" />
554556
<Compile Include="Document\Multiple\ReindexOnServer\ReindexOnServerApiTests.cs" />
555557
<Compile Include="Document\Multiple\UpdateByQuery\UpdateByQueryApiTests.cs" />

0 commit comments

Comments
 (0)