Skip to content

fix #1296 map get response on the update response #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Nest/Domain/Responses/UpdateResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Nest.Domain;
using Newtonsoft.Json;

namespace Nest
{
Expand All @@ -9,6 +11,10 @@ public interface IUpdateResponse : IResponse
string Type { get; }
string Id { get; }
string Version { get; }
GetFromUpdate Get { get; }

T Source<T>() where T : class;
FieldSelection<T> Fields<T>() where T : class;
}

[JsonObject]
Expand All @@ -25,5 +31,36 @@ public class UpdateResponse : BaseResponse, IUpdateResponse
public string Id { get; private set; }
[JsonProperty(PropertyName = "_version")]
public string Version { get; private set; }

[JsonProperty(PropertyName = "get")]
public GetFromUpdate Get { get; private set; }

public T Source<T>() where T : class
{
if (this.Get == null) return null;
return this.Get.Source.As<T>();
}

public FieldSelection<T> Fields<T>() where T : class
{
if (this.Get == null) return null;
return new FieldSelection<T>(this.Settings, this.Get._fields);
}
}

[JsonObject]
public class GetFromUpdate
{
[JsonProperty(PropertyName = "found")]
public bool Found { get; set; }

[JsonProperty(PropertyName = "_source")]
internal IDocument Source { get; set; }


[JsonProperty(PropertyName = "fields")]
internal IDictionary<string, object> _fields { get; set; }


}
}
24 changes: 17 additions & 7 deletions src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ public void TestUpdate()
Assert.NotNull(project);
Assert.Greater(project.LOC, 0);
var loc = project.LOC;
this.Client.Update<ElasticsearchProject>(u => u
.IdFrom(project)
.Script("ctx._source.loc += 10")
.RetryOnConflict(5)
.Refresh()
var update = this.Client.Update<ElasticsearchProject>(u => u
.IdFrom(project)
.Script("ctx._source.loc += 10")
.Fields("_source", "loc")
.RetryOnConflict(5)
.Refresh()
);
project = this.Client.Source<ElasticsearchProject>(s => s.Id(1));
Assert.AreEqual(project.LOC, loc + 10);
Assert.AreNotEqual(project.Version, "1");

update.Get.Should().NotBeNull();
update.Get.Found.Should().BeTrue();
update.Source<ElasticsearchProject>().Should().NotBeNull();
update.Source<ElasticsearchProject>().LOC.Should().Be(loc + 10);
var fieldLoc = update.Fields<ElasticsearchProject>().FieldValues(p => p.LOC);
fieldLoc.Should().HaveCount(1);
fieldLoc.First().Should().Be(loc + 10);

}

[Test]
public void TestUpdate_ObjectInitializer()
{
Expand All @@ -49,7 +59,7 @@ public void TestUpdate_ObjectInitializer()
public class ElasticsearchProjectLocUpdate
{
public int Id { get; set; }
[ElasticProperty(Name="loc",AddSortField=true)]
[ElasticProperty(Name = "loc", AddSortField = true)]
public int LOC { get; set; }
}

Expand Down