Skip to content

Commit c4ed692

Browse files
Mpdreamzgithub-actions[bot]
authored andcommitted
Fix GetMany when no default index is set (#4463)
#4353 Fixed an issue with the GetMany helpers that returned the cartesian product of all ids specified rather then creating a distinct list if more then one index was targeted. This PR also updated the routine in the serializer to omit the index name from each item if the index is already specified on the url in case of multiple indices This updated routine in the `7.6.0` could throw if you are calling: client.GetMany<T>(ids, "indexName"); Without configuring `ConnectionSettings()` with either a default index for T or a global default index.
1 parent 8fe28a9 commit c4ed692

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

src/Nest/Document/Multiple/Bulk/ElasticClient-DeleteMany.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Nest
77
{
88
/// <summary>
9-
/// Provides GetMany extensions that make it easier to get many documents given a list of ids
9+
/// Provides DeleteMany extensions that make it easier to get many documents given a list of ids
1010
/// </summary>
1111
public static class DeleteManyExtensions
1212
{

src/Nest/Document/Multiple/MultiGet/ElasticClient-GetMany.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
45
using System.Threading;
@@ -11,6 +12,14 @@ namespace Nest
1112
/// </summary>
1213
public static class GetManyExtensions
1314
{
15+
private static Func<MultiGetOperationDescriptor<T>, string, IMultiGetOperation> Lookup<T>(IndexName index)
16+
where T : class
17+
{
18+
if (index == null) return null;
19+
20+
return (d, id) => d.Index(index);
21+
}
22+
1423
/// <summary>
1524
/// Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
1625
/// The response includes a docs array with all the fetched documents, each element similar in structure to a document
@@ -29,7 +38,7 @@ public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client
2938
var result = client.MultiGet(s => s
3039
.Index(index)
3140
.RequestConfiguration(r => r.ThrowExceptions())
32-
.GetMany<T>(ids)
41+
.GetMany<T>(ids, Lookup<T>(index))
3342
);
3443
return result.GetMany<T>(ids);
3544
}
@@ -68,7 +77,7 @@ public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(
6877
var response = await client.MultiGetAsync(s => s
6978
.Index(index)
7079
.RequestConfiguration(r => r.ThrowExceptions())
71-
.GetMany<T>(ids),
80+
.GetMany<T>(ids, Lookup<T>(index)),
7281
cancellationToken
7382
)
7483
.ConfigureAwait(false);

src/Nest/Document/Multiple/MultiGet/ElasticClient-SourceMany.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
45
using System.Threading;
@@ -11,6 +12,15 @@ namespace Nest
1112
/// </summary>
1213
public static class SourceManyExtensions
1314
{
15+
16+
private static Func<MultiGetOperationDescriptor<T>, string, IMultiGetOperation> Lookup<T>(IndexName index)
17+
where T : class
18+
{
19+
if (index == null) return null;
20+
21+
return (d, id) => d.Index(index);
22+
}
23+
1424
/// <summary>
1525
/// SourceMany allows you to get a list of T documents out of Elasticsearch, internally it calls into MultiGet()
1626
/// <para>
@@ -31,7 +41,7 @@ public static IEnumerable<T> SourceMany<T>(this IElasticClient client, IEnumerab
3141
var result = client.MultiGet(s => s
3242
.Index(index)
3343
.RequestConfiguration(r => r.ThrowExceptions())
34-
.GetMany<T>(ids, (gs, i) => gs )
44+
.GetMany<T>(ids, Lookup<T>(index))
3545
);
3646
return result.SourceMany<T>(ids);
3747
}
@@ -75,7 +85,7 @@ public static async Task<IEnumerable<T>> SourceManyAsync<T>(
7585
var response = await client.MultiGetAsync(s => s
7686
.Index(index)
7787
.RequestConfiguration(r => r.ThrowExceptions())
78-
.GetMany<T>(ids, (gs, i) => gs), cancellationToken)
88+
.GetMany<T>(ids, Lookup<T>(index)), cancellationToken)
7989
.ConfigureAwait(false);
8090
return response.SourceMany<T>(ids);
8191
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Text;
3+
using Elastic.Xunit.XunitPlumbing;
4+
using Elasticsearch.Net;
5+
using FluentAssertions;
6+
using Nest;
7+
using Tests.Domain;
8+
9+
namespace Tests.Reproduce
10+
{
11+
/*
12+
* https://github.com/elastic/elasticsearch-net/pull/4353
13+
* Fixed an issue with the GetMany helpers that returned the cartesian product of all ids specified rather
14+
* then creating a distinct list if more then one index was targeted.
15+
*
16+
* This PR also updated the routine in the serializer to omit the index name from each item if the index is
17+
* already specified on the url in case of multiple indices
18+
*
19+
* This updated routine in the `7.6.0` could throw if you are calling:
20+
*
21+
* client.GetMany<T>(ids, "indexName");
22+
*
23+
* Without configuring `ConnectionSettings()` with either a default index for T or a global default index.
24+
*/
25+
public class GitHubIssue4462
26+
{
27+
[U] public void GetManyShouldNotThrowIfIndexIsProvided()
28+
{
29+
var json = "{}";
30+
31+
var bytes = Encoding.UTF8.GetBytes(json);
32+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
33+
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(bytes));
34+
var client = new ElasticClient(connectionSettings);
35+
36+
var response = client.GetMany<Project>(new long[] {1, 2, 3}, "indexName");
37+
response.Should().NotBeNull();
38+
}
39+
40+
[U] public void SourceManyShouldNotThrowIfIndexIsProvided()
41+
{
42+
var json = "{}";
43+
44+
var bytes = Encoding.UTF8.GetBytes(json);
45+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
46+
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(bytes));
47+
var client = new ElasticClient(connectionSettings);
48+
49+
var response = client.SourceMany<Project>(new long[] {1, 2, 3}, "indexName");
50+
response.Should().NotBeNull();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)