Skip to content

Commit 1d8019f

Browse files
committed
Merge branch 'fix/2.x-getresponse-metadata' into 2.x
2 parents 818bee0 + 91f3fb3 commit 1d8019f

File tree

9 files changed

+232
-29
lines changed

9 files changed

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ReindexApiTests(ReindexCluster cluster, EndpointUsage usage)
4343
var indexProjectsResponse = this._client.IndexMany(projects, IndexName);
4444

4545
// create a thousand commits and associate with the projects
46-
var commits = CommitActivity.Generator.Generate(1000).ToList();
46+
var commits = CommitActivity.CommitActivities;
4747
var bb = new BulkDescriptor();
4848
for (int index = 0; index < commits.Count; index++)
4949
{

src/Tests/Document/Single/Get/GetApiTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@ protected override void ExpectResponse(IGetResponse<Project> response)
3939
{
4040
response.Source.Should().NotBeNull();
4141
response.Source.Name.Should().Be(ProjectId);
42+
response.Timestamp.HasValue.Should().BeTrue();
43+
response.Ttl.HasValue.Should().BeTrue();
44+
}
45+
}
46+
47+
public class GetApiParentTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetResponse<CommitActivity>, IGetRequest, GetDescriptor<CommitActivity>, GetRequest<CommitActivity>>
48+
{
49+
protected CommitActivity CommitActivity => CommitActivity.CommitActivities.First();
50+
51+
protected string CommitActivityId => CommitActivity.Id;
52+
53+
protected string ParentIdForUrl => Uri.EscapeDataString(this.CommitActivity.ProjectName);
54+
55+
protected string CommitActivityIdForUrl => Uri.EscapeDataString(this.CommitActivityId);
56+
57+
public GetApiParentTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
58+
protected override LazyResponses ClientUsage() => Calls(
59+
fluent: (client, f) => client.Get<CommitActivity>(this.CommitActivityId, f),
60+
fluentAsync: (client, f) => client.GetAsync<CommitActivity>(this.CommitActivityId, f),
61+
request: (client, r) => client.Get<CommitActivity>(r),
62+
requestAsync: (client, r) => client.GetAsync<CommitActivity>(r)
63+
);
64+
65+
protected override bool ExpectIsValid => true;
66+
protected override int ExpectStatusCode => 200;
67+
protected override HttpMethod HttpMethod => HttpMethod.GET;
68+
protected override string UrlPath => $"/project/commits/{CommitActivityIdForUrl}?parent={ParentIdForUrl}";
69+
70+
protected override bool SupportsDeserialization => false;
71+
72+
protected override GetDescriptor<CommitActivity> NewDescriptor() => new GetDescriptor<CommitActivity>(CommitActivity);
73+
74+
protected override Func<GetDescriptor<CommitActivity>, IGetRequest> Fluent => g => g
75+
.Parent(this.CommitActivity.ProjectName)
76+
;
77+
78+
protected override GetRequest<CommitActivity> Initializer => new GetRequest<CommitActivity>(this.CommitActivityId)
79+
{
80+
Parent = this.CommitActivity.ProjectName
81+
};
82+
83+
protected override void ExpectResponse(IGetResponse<CommitActivity> response)
84+
{
85+
response.Source.Should().NotBeNull();
86+
response.Source.Id.Should().Be(CommitActivityId);
87+
response.Timestamp.HasValue.Should().BeTrue();
88+
response.Parent.Should().NotBeNullOrEmpty();
89+
response.Routing.Should().NotBeNullOrEmpty();
4290
}
4391
}
4492

src/Tests/Document/Single/Index/IndexApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void OpTypeCreate()
110110
public void Index()
111111
{
112112
var indexName = RandomString();
113-
var commitActivity = CommitActivity.Generator.Generate(1).First();
113+
var commitActivity = CommitActivity.CommitActivities.First();
114114
var indexResult = this.Client.Index(commitActivity, f => f.Index(indexName));
115115
indexResult.ShouldBeValid();
116116
indexResult.ApiCall.HttpStatusCode.Should().Be(201);

0 commit comments

Comments
 (0)