Skip to content

Commit 47be8da

Browse files
committed
Add missing metadata to IGetResponse<T> and IMultiHit<T>
Closes #2213
1 parent bfb468d commit 47be8da

File tree

9 files changed

+243
-37
lines changed

9 files changed

+243
-37
lines changed

src/Nest/Document/Multiple/MultiGet/Response/MultiGetHit.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public interface IMultiGetHit<out T> where T : class
1515
long Version { get; }
1616

1717
string Id { get; }
18+
19+
string Parent { get; }
20+
21+
string Routing { get; }
22+
23+
long? Timestamp { get; }
24+
25+
long? Ttl { get; }
1826
}
1927

2028
[JsonObject]
@@ -23,22 +31,34 @@ public class MultiGetHit<T> : IMultiGetHit<T>
2331
{
2432
public FieldValues Fields { get; internal set; }
2533

26-
[JsonProperty(PropertyName = "_source")]
34+
[JsonProperty("_source")]
2735
public T Source { get; internal set; }
2836

29-
[JsonProperty(PropertyName = "_index")]
37+
[JsonProperty("_index")]
3038
public string Index { get; internal set; }
3139

32-
[JsonProperty(PropertyName = "found")]
40+
[JsonProperty("found")]
3341
public bool Found { get; internal set; }
3442

35-
[JsonProperty(PropertyName = "_type")]
43+
[JsonProperty("_type")]
3644
public string Type { get; internal set; }
3745

38-
[JsonProperty(PropertyName = "_version")]
46+
[JsonProperty("_version")]
3947
public long Version { get; internal set; }
4048

41-
[JsonProperty(PropertyName = "_id")]
49+
[JsonProperty("_id")]
4250
public string Id { get; internal set; }
51+
52+
[JsonProperty("_parent")]
53+
public string Parent { get; internal set; }
54+
55+
[JsonProperty("_routing")]
56+
public string Routing { get; internal set; }
57+
58+
[JsonProperty("_timestamp")]
59+
public long? Timestamp { get; internal set; }
60+
61+
[JsonProperty("_ttl")]
62+
public long? Ttl { get; internal set; }
4363
}
4464
}

src/Nest/Document/Multiple/MultiGet/Response/MultiGetResponse.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,29 @@ public MultiGetResponse()
3232

3333
internal ICollection<IMultiGetHit<object>> _Documents { get; set; }
3434

35-
public IEnumerable<IMultiGetHit<object>> Documents { get { return this._Documents.ToList(); } }
36-
35+
public IEnumerable<IMultiGetHit<object>> Documents => this._Documents.ToList();
3736

3837
public MultiGetHit<T> Get<T>(string id) where T : class
3938
{
4039
return this.Documents.OfType<MultiGetHit<T>>().FirstOrDefault(m => m.Id == id);
4140
}
41+
4242
public MultiGetHit<T> Get<T>(long id) where T : class
4343
{
4444
return this.Get<T>(id.ToString(CultureInfo.InvariantCulture));
4545
}
46+
4647
public T Source<T>(string id) where T : class
4748
{
4849
var multiHit = this.Get<T>(id);
49-
if (multiHit == null)
50-
return null;
51-
return multiHit.Source;
50+
return multiHit?.Source;
5251
}
52+
5353
public T Source<T>(long id) where T : class
5454
{
5555
return this.Source<T>(id.ToString(CultureInfo.InvariantCulture));
5656
}
57+
5758
public IEnumerable<T> SourceMany<T>(IEnumerable<string> ids) where T : class
5859
{
5960
var docs = this.Documents.OfType<IMultiGetHit<T>>();
@@ -62,29 +63,31 @@ join id in ids on d.Id equals id
6263
where d.Found
6364
select d.Source;
6465
}
66+
6567
public IEnumerable<T> SourceMany<T>(IEnumerable<long> ids) where T : class
6668
{
6769
return this.SourceMany<T>(ids.Select(i=>i.ToString(CultureInfo.InvariantCulture)));
6870
}
69-
71+
7072
public IEnumerable<IMultiGetHit<T>> GetMany<T>(IEnumerable<string> ids) where T : class
7173
{
7274
var docs = this.Documents.OfType<IMultiGetHit<T>>();
7375
return from d in docs
7476
join id in ids on d.Id equals id
7577
select d;
7678
}
79+
7780
public IEnumerable<IMultiGetHit<T>> GetMany<T>(IEnumerable<long> ids) where T : class
7881
{
7982
return this.GetMany<T>(ids.Select(i=>i.ToString(CultureInfo.InvariantCulture)));
8083
}
84+
8185
public FieldValues GetFieldValues<T>(string id) where T : class
8286
{
8387
var multiHit = this.Get<T>(id);
84-
if (multiHit == null)
85-
return null;
86-
return multiHit.Fields;
88+
return multiHit?.Fields;
8789
}
90+
8891
public FieldValues GetFieldSelection<T>(long id) where T : class
8992
{
9093
return this.GetFieldValues<T>(id.ToString(CultureInfo.InvariantCulture));

src/Nest/Document/Single/Get/GetResponse.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@ namespace Nest
88
{
99
public interface IGetResponse<T> : IResponse where T : class
1010
{
11-
[JsonProperty(PropertyName = "_index")]
11+
[JsonProperty("_index")]
1212
string Index { get; }
1313

14-
[JsonProperty(PropertyName = "_type")]
14+
[JsonProperty("_type")]
1515
string Type { get; }
1616

17-
[JsonProperty(PropertyName = "_id")]
17+
[JsonProperty("_id")]
1818
string Id { get; }
1919

20-
[JsonProperty(PropertyName = "_version")]
20+
[JsonProperty("_version")]
2121
long Version { get; }
2222

23-
[JsonProperty(PropertyName = "found")]
23+
[JsonProperty("found")]
2424
bool Found { get; }
2525

26-
[JsonProperty(PropertyName = "_source")]
26+
[JsonProperty("_source")]
2727
T Source { get; }
2828

29-
[JsonProperty(PropertyName = "fields")]
29+
[JsonProperty("fields")]
3030
FieldValues Fields { get; }
31+
32+
[JsonProperty("_parent")]
33+
string Parent { get; }
34+
35+
[JsonProperty("_routing")]
36+
string Routing { get; }
37+
38+
[JsonProperty("_timestamp")]
39+
long? Timestamp { get; }
40+
41+
[JsonProperty("_ttl")]
42+
long? Ttl { get; }
3143
}
3244

3345
[JsonObject(MemberSerialization.OptIn)]
@@ -40,5 +52,9 @@ public class GetResponse<T> : ResponseBase, IGetResponse<T> where T : class
4052
public bool Found { get; private set; }
4153
public T Source { get; private set; }
4254
public FieldValues Fields { get; private set; }
55+
public string Parent { get; private set; }
56+
public string Routing { get; private set; }
57+
public long? Timestamp { get; private set; }
58+
public long? Ttl { get; private set; }
4359
}
4460
}

src/Tests/Document/Multiple/MultiGet/MultiGetApiTests.cs

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override LazyResponses ClientUsage() => Calls(
3232

3333
protected override bool SupportsDeserialization => false;
3434

35-
protected override object ExpectJson => new
35+
protected override object ExpectJson => new
3636
{
3737
ids = this._ids
3838
};
@@ -53,7 +53,7 @@ protected override void ExpectResponse(IMultiGetResponse response)
5353
.Index<Developer>()
5454
.Type<Developer>()
5555
.GetMany<Developer>(this._ids);
56-
56+
5757

5858
protected override MultiGetRequest Initializer => new MultiGetRequest(Index<Developer>(), Type<Developer>())
5959
{
@@ -82,7 +82,7 @@ protected override LazyResponses ClientUsage() => Calls(
8282

8383
protected override bool SupportsDeserialization => false;
8484

85-
protected override object ExpectJson { get; } = new
85+
protected override object ExpectJson { get; } = new
8686
{
8787
docs = Developer.Developers.Select(p => new { _type = "developer", _id = p.Id, _routing = p.Id.ToString(), _source = false }).Take(10)
8888
};
@@ -109,4 +109,109 @@ protected override void ExpectResponse(IMultiGetResponse response)
109109
}
110110
}
111111
}
112+
113+
[Collection(IntegrationContext.ReadOnly)]
114+
public class MultiGetMetadataApiTests : ApiIntegrationTestBase<IMultiGetResponse, IMultiGetRequest, MultiGetDescriptor, MultiGetRequest>
115+
{
116+
public MultiGetMetadataApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
117+
protected override LazyResponses ClientUsage() => Calls(
118+
fluent: (client, f) => client.MultiGet(f),
119+
fluentAsync: (client, f) => client.MultiGetAsync(f),
120+
request: (client, r) => client.MultiGet(r),
121+
requestAsync: (client, r) => client.MultiGetAsync(r)
122+
);
123+
124+
private IEnumerable<string> _ids = Project.Projects.Select(d => d.Name).Take(10);
125+
126+
protected override bool ExpectIsValid => true;
127+
protected override int ExpectStatusCode => 200;
128+
protected override HttpMethod HttpMethod => HttpMethod.POST;
129+
protected override string UrlPath => $"/project/project/_mget";
130+
131+
protected override bool SupportsDeserialization => false;
132+
133+
protected override object ExpectJson => new
134+
{
135+
ids = this._ids
136+
};
137+
138+
protected override Func<MultiGetDescriptor, IMultiGetRequest> Fluent => d => d
139+
.Index<Project>()
140+
.Type<Project>()
141+
.GetMany<Project>(this._ids);
142+
143+
protected override MultiGetRequest Initializer => new MultiGetRequest(Index<Project>(), Type<Project>())
144+
{
145+
Documents = this._ids.Select(n => new MultiGetOperation<Project>(n))
146+
};
147+
148+
protected override void ExpectResponse(IMultiGetResponse response)
149+
{
150+
response.Documents.Should().NotBeEmpty().And.HaveCount(10);
151+
152+
foreach (var hit in response.GetMany<Project>(_ids))
153+
{
154+
hit.Index.Should().NotBeNullOrWhiteSpace();
155+
hit.Type.Should().NotBeNullOrWhiteSpace();
156+
hit.Id.Should().NotBeNullOrWhiteSpace();
157+
hit.Found.Should().BeTrue();
158+
hit.Version.Should().Be(1);
159+
hit.Timestamp.HasValue.Should().BeTrue();
160+
hit.Ttl.HasValue.Should().BeTrue();
161+
}
162+
}
163+
}
164+
165+
[Collection(IntegrationContext.ReadOnly)]
166+
public class MultiGetParentApiTests : ApiIntegrationTestBase<IMultiGetResponse, IMultiGetRequest, MultiGetDescriptor, MultiGetRequest>
167+
{
168+
public MultiGetParentApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
169+
protected override LazyResponses ClientUsage() => Calls(
170+
fluent: (client, f) => client.MultiGet(f),
171+
fluentAsync: (client, f) => client.MultiGetAsync(f),
172+
request: (client, r) => client.MultiGet(r),
173+
requestAsync: (client, r) => client.MultiGetAsync(r)
174+
);
175+
176+
private IEnumerable<CommitActivity> _activities = CommitActivity.CommitActivities.Take(10);
177+
178+
protected override bool ExpectIsValid => true;
179+
protected override int ExpectStatusCode => 200;
180+
protected override HttpMethod HttpMethod => HttpMethod.POST;
181+
protected override string UrlPath => $"/project/commits/_mget";
182+
183+
protected override bool SupportsDeserialization => false;
184+
185+
protected override object ExpectJson => new
186+
{
187+
docs = _activities.Select(p => new { _id = p.Id, _routing = p.ProjectName })
188+
};
189+
190+
protected override Func<MultiGetDescriptor, IMultiGetRequest> Fluent => d => d
191+
.Index<Project>()
192+
.Type<CommitActivity>()
193+
.GetMany<CommitActivity>(this._activities.Select(c => c.Id), (m, id) => m.Routing(_activities.Single(a => a.Id == id).ProjectName));
194+
195+
protected override MultiGetRequest Initializer => new MultiGetRequest(Index<Project>(), Type<CommitActivity>())
196+
{
197+
Documents = this._activities.Select(n => new MultiGetOperation<CommitActivity>(n.Id) { Routing = n.ProjectName })
198+
};
199+
200+
protected override void ExpectResponse(IMultiGetResponse response)
201+
{
202+
response.Documents.Should().NotBeEmpty().And.HaveCount(10);
203+
204+
foreach (var hit in response.GetMany<CommitActivity>(_activities.Select(c => c.Id)))
205+
{
206+
hit.Index.Should().NotBeNullOrWhiteSpace();
207+
hit.Type.Should().NotBeNullOrWhiteSpace();
208+
hit.Id.Should().NotBeNullOrWhiteSpace();
209+
hit.Found.Should().BeTrue();
210+
hit.Version.Should().Be(1);
211+
hit.Timestamp.HasValue.Should().BeTrue();
212+
hit.Parent.Should().NotBeNullOrEmpty();
213+
hit.Routing.Should().NotBeNullOrEmpty();
214+
}
215+
}
216+
}
112217
}

src/Tests/Document/Multiple/Reindex/ReindexApiTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public override void Boostrap()
2121
seeder.DeleteIndicesAndTemplates();
2222
seeder.CreateIndices();
2323
}
24-
}
24+
}
2525

2626
[Collection(IntegrationContext.Reindex)]
2727
public class ReindexApiTests : SerializationTestBase
@@ -45,7 +45,7 @@ public ReindexApiTests(ReindexCluster cluster, EndpointUsage usage)
4545
var indexProjectsResponse = this._client.IndexMany(projects, IndexName);
4646

4747
// create a thousand commits and associate with the projects
48-
var commits = CommitActivity.Generator.Generate(1000).ToList();
48+
var commits = CommitActivity.CommitActivities;
4949
var bb = new BulkDescriptor();
5050
for (int index = 0; index < commits.Count; index++)
5151
{
@@ -100,7 +100,7 @@ [I] public void ReturnsExpectedResponse()
100100
.SearchType(SearchType.Scan)
101101
.Scroll(scroll)
102102
);
103-
103+
104104
do
105105
{
106106
var result = searchResult;

0 commit comments

Comments
 (0)