Skip to content

Commit d8d62ac

Browse files
committed
Merge pull request #1489 from elastic/fix/update-get-response
fix #1296 map get response on the update response
2 parents 01955f1 + 5fd519e commit d8d62ac

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

Diff for: src/Nest/Domain/Responses/UpdateResponse.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Newtonsoft.Json;
1+
using System.Collections.Generic;
2+
using Nest.Domain;
3+
using Newtonsoft.Json;
24

35
namespace Nest
46
{
@@ -9,6 +11,10 @@ public interface IUpdateResponse : IResponse
911
string Type { get; }
1012
string Id { get; }
1113
string Version { get; }
14+
GetFromUpdate Get { get; }
15+
16+
T Source<T>() where T : class;
17+
FieldSelection<T> Fields<T>() where T : class;
1218
}
1319

1420
[JsonObject]
@@ -25,5 +31,36 @@ public class UpdateResponse : BaseResponse, IUpdateResponse
2531
public string Id { get; private set; }
2632
[JsonProperty(PropertyName = "_version")]
2733
public string Version { get; private set; }
34+
35+
[JsonProperty(PropertyName = "get")]
36+
public GetFromUpdate Get { get; private set; }
37+
38+
public T Source<T>() where T : class
39+
{
40+
if (this.Get == null) return null;
41+
return this.Get.Source.As<T>();
42+
}
43+
44+
public FieldSelection<T> Fields<T>() where T : class
45+
{
46+
if (this.Get == null) return null;
47+
return new FieldSelection<T>(this.Settings, this.Get._fields);
48+
}
49+
}
50+
51+
[JsonObject]
52+
public class GetFromUpdate
53+
{
54+
[JsonProperty(PropertyName = "found")]
55+
public bool Found { get; set; }
56+
57+
[JsonProperty(PropertyName = "_source")]
58+
internal IDocument Source { get; set; }
59+
60+
61+
[JsonProperty(PropertyName = "fields")]
62+
internal IDictionary<string, object> _fields { get; set; }
63+
64+
2865
}
2966
}

Diff for: src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ public void TestUpdate()
1616
Assert.NotNull(project);
1717
Assert.Greater(project.LOC, 0);
1818
var loc = project.LOC;
19-
this.Client.Update<ElasticsearchProject>(u => u
20-
.IdFrom(project)
21-
.Script("ctx._source.loc += 10")
22-
.RetryOnConflict(5)
23-
.Refresh()
19+
var update = this.Client.Update<ElasticsearchProject>(u => u
20+
.IdFrom(project)
21+
.Script("ctx._source.loc += 10")
22+
.Fields("_source", "loc")
23+
.RetryOnConflict(5)
24+
.Refresh()
2425
);
2526
project = this.Client.Source<ElasticsearchProject>(s => s.Id(1));
2627
Assert.AreEqual(project.LOC, loc + 10);
2728
Assert.AreNotEqual(project.Version, "1");
29+
30+
update.Get.Should().NotBeNull();
31+
update.Get.Found.Should().BeTrue();
32+
update.Source<ElasticsearchProject>().Should().NotBeNull();
33+
update.Source<ElasticsearchProject>().LOC.Should().Be(loc + 10);
34+
var fieldLoc = update.Fields<ElasticsearchProject>().FieldValues(p => p.LOC);
35+
fieldLoc.Should().HaveCount(1);
36+
fieldLoc.First().Should().Be(loc + 10);
37+
2838
}
29-
39+
3040
[Test]
3141
public void TestUpdate_ObjectInitializer()
3242
{
@@ -49,7 +59,7 @@ public void TestUpdate_ObjectInitializer()
4959
public class ElasticsearchProjectLocUpdate
5060
{
5161
public int Id { get; set; }
52-
[ElasticProperty(Name="loc",AddSortField=true)]
62+
[ElasticProperty(Name = "loc", AddSortField = true)]
5363
public int LOC { get; set; }
5464
}
5565

0 commit comments

Comments
 (0)