diff --git a/src/Nest/DSL/Bulk/BulkUpdateDescriptor.cs b/src/Nest/DSL/Bulk/BulkUpdateDescriptor.cs index fe02cd2c6c8..c88807b3419 100644 --- a/src/Nest/DSL/Bulk/BulkUpdateDescriptor.cs +++ b/src/Nest/DSL/Bulk/BulkUpdateDescriptor.cs @@ -5,14 +5,15 @@ namespace Nest { - public interface IBulkUpdateOperation : IBulkOperation + public interface IBulkUpdateOperation : IBulkOperation where TDocument : class - where TPartialUpdate : class + where TPartialDocument : class { - TDocument Document { get; set; } + TDocument InferFrom { get; set; } + TDocument Upsert { get; set; } - TPartialUpdate PartialUpdate { get; set; } + TPartialDocument Doc { get; set; } bool? DocAsUpsert { get; set; } @@ -23,18 +24,39 @@ public interface IBulkUpdateOperation : IBulkOperatio Dictionary Params { get; set; } } - public class BulkUpdateOperation - : BulkOperationBase, IBulkUpdateOperation + public class BulkUpdateOperation : BulkOperationBase, IBulkUpdateOperation where TDocument : class - where TPartialUpdate : class + where TPartialDocument : class { + + public BulkUpdateOperation(string id) { this.Id = id; } + public BulkUpdateOperation(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {} - public BulkUpdateOperation() {} - public BulkUpdateOperation(TDocument document, TPartialUpdate update) : this() + /// + /// Create a new bulk operation + /// + /// Use this document to infer the id from + /// Use the document to infer on as the upsert document in this update operation + public BulkUpdateOperation(TDocument idFrom, bool useIdFromAsUpsert = false) { - this.PartialUpdate = update; - this.Document = document; + this.InferFrom = idFrom; + if (useIdFromAsUpsert) + this.Upsert = idFrom; + } + + /// + /// Create a new Bulk Operation + /// + /// Use this document to infer the id from + /// The partial update document (doc) to send as update + /// Use the document to infer on as the upsert document in this update operation + public BulkUpdateOperation(TDocument idFrom, TPartialDocument update, bool useIdFromAsUpsert = false) + { + this.InferFrom = idFrom; + if (useIdFromAsUpsert) + this.Upsert = idFrom; + this.Doc = update; } @@ -44,14 +66,14 @@ public BulkUpdateOperation(TDocument document, TPartialUpdate update) : this() public override string GetIdForOperation(ElasticInferrer inferrer) { - return this.Id ?? inferrer.Id(this.Document); + return this.Id ?? inferrer.Id(this.InferFrom); } public override object GetBody() { - return new BulkUpdateBody + return new BulkUpdateBody { - _PartialUpdate = this.PartialUpdate, + _PartialUpdate = this.Doc, _Script = this.Script, _Lang = this.Lang, _Params = this.Params, @@ -60,38 +82,43 @@ public override object GetBody() }; } - public TDocument Document { get; set; } + public TDocument InferFrom { get; set; } public TDocument Upsert { get; set; } - public TPartialUpdate PartialUpdate { get; set; } + public TPartialDocument Doc { get; set; } public bool? DocAsUpsert { get; set; } public string Lang { get; set; } public string Script { get; set; } public Dictionary Params { get; set; } } - public class BulkUpdateDescriptor - : BulkOperationDescriptorBase, IBulkUpdateOperation + public class BulkUpdateDescriptor : BulkOperationDescriptorBase, IBulkUpdateOperation where TDocument : class - where TPartialUpdate : class + where TPartialDocument : class { - private IBulkUpdateOperation Self { get { return this; } } + private IBulkUpdateOperation Self { get { return this; } } protected override string BulkOperationType { get { return "update"; } } protected override Type BulkOperationClrType { get { return typeof(TDocument); } } - TDocument IBulkUpdateOperation.Document { get; set; } - TDocument IBulkUpdateOperation.Upsert { get; set; } - TPartialUpdate IBulkUpdateOperation.PartialUpdate { get; set; } - bool? IBulkUpdateOperation.DocAsUpsert { get; set; } - string IBulkUpdateOperation.Lang { get; set; } - string IBulkUpdateOperation.Script { get; set; } - Dictionary IBulkUpdateOperation.Params { get; set; } + TDocument IBulkUpdateOperation.InferFrom { get; set; } + + TDocument IBulkUpdateOperation.Upsert { get; set; } + + TPartialDocument IBulkUpdateOperation.Doc { get; set; } + + bool? IBulkUpdateOperation.DocAsUpsert { get; set; } + + string IBulkUpdateOperation.Lang { get; set; } + + string IBulkUpdateOperation.Script { get; set; } + + Dictionary IBulkUpdateOperation.Params { get; set; } protected override object GetBulkOperationBody() { - return new BulkUpdateBody + return new BulkUpdateBody { - _PartialUpdate = Self.PartialUpdate, + _PartialUpdate = Self.Doc, _Script = Self.Script, _Lang = Self.Lang, _Params = Self.Params, @@ -102,13 +129,13 @@ protected override object GetBulkOperationBody() protected override string GetIdForOperation(ElasticInferrer inferrer) { - return Self.Id ?? inferrer.Id(Self.Document); + return Self.Id ?? inferrer.Id(Self.InferFrom) ?? inferrer.Id(Self.Upsert); } /// /// Manually set the index, default to the default index or the fixed index set on the bulk operation /// - public BulkUpdateDescriptor Index(string index) + public BulkUpdateDescriptor Index(string index) { index.ThrowIfNullOrEmpty("indices"); Self.Index = index; @@ -118,7 +145,7 @@ public BulkUpdateDescriptor Index(string index) /// Manualy set the type to get the object from, default to whatever /// T will be inferred to if not passed or the fixed type set on the parent bulk operation /// - public BulkUpdateDescriptor Type(string type) + public BulkUpdateDescriptor Type(string type) { type.ThrowIfNullOrEmpty("type"); Self.Type = type; @@ -128,7 +155,7 @@ public BulkUpdateDescriptor Type(string type) /// /// Manually set the type of which a typename will be inferred /// - public BulkUpdateDescriptor Type(Type type) + public BulkUpdateDescriptor Type(Type type) { type.ThrowIfNull("type"); Self.Type = type; @@ -138,7 +165,7 @@ public BulkUpdateDescriptor Type(Type type) /// /// Manually set the id for the newly created object /// - public BulkUpdateDescriptor Id(long id) + public BulkUpdateDescriptor Id(long id) { return this.Id(id.ToString(CultureInfo.InvariantCulture)); } @@ -146,7 +173,7 @@ public BulkUpdateDescriptor Id(long id) /// /// Manually set the id for the newly created object /// - public BulkUpdateDescriptor Id(string id) + public BulkUpdateDescriptor Id(string id) { Self.Id = id; return this; @@ -156,15 +183,16 @@ public BulkUpdateDescriptor Id(string id) /// The object to update, if id is not manually set it will be inferred from the object. /// Used ONLY to infer the ID see Document() to apply a partial object merge. /// - public BulkUpdateDescriptor Document(TDocument @object) + public BulkUpdateDescriptor IdFrom(TDocument @object, bool useAsUpsert = false) { - Self.Document = @object; + Self.InferFrom = @object; + if (useAsUpsert) return this.Upsert(@object); return this; } /// /// A document to upsert when the specified document to be updated is not found /// - public BulkUpdateDescriptor Upsert(TDocument @object) + public BulkUpdateDescriptor Upsert(TDocument @object) { Self.Upsert = @object; return this; @@ -172,75 +200,75 @@ public BulkUpdateDescriptor Upsert(TDocument @object) /// /// The partial update document to be merged on to the existing object. /// - public BulkUpdateDescriptor PartialUpdate(TPartialUpdate @object) + public BulkUpdateDescriptor Doc(TPartialDocument @object) { - Self.PartialUpdate = @object; + Self.Doc = @object; return this; } - public BulkUpdateDescriptor DocAsUpsert(bool docAsUpsert = true) + public BulkUpdateDescriptor DocAsUpsert(bool partialDocumentAsUpsert = true) { - Self.DocAsUpsert = docAsUpsert; + Self.DocAsUpsert = partialDocumentAsUpsert; return this; } - public BulkUpdateDescriptor Lang(string lang) + public BulkUpdateDescriptor Lang(string lang) { Self.Lang = lang; return this; } - public BulkUpdateDescriptor Script(string script) + public BulkUpdateDescriptor Script(string script) { script.ThrowIfNull("script"); Self.Script = script; return this; } - public BulkUpdateDescriptor Params(Func, FluentDictionary> paramDictionary) + public BulkUpdateDescriptor Params(Func, FluentDictionary> paramDictionary) { paramDictionary.ThrowIfNull("paramDictionary"); Self.Params = paramDictionary(new FluentDictionary()); return this; } - public BulkUpdateDescriptor Version(string version) + public BulkUpdateDescriptor Version(string version) { Self.Version = version; return this; } - public BulkUpdateDescriptor VersionType(VersionType versionType) + public BulkUpdateDescriptor VersionType(VersionType versionType) { Self.VersionType = versionType; return this; } - public BulkUpdateDescriptor Routing(string routing) + public BulkUpdateDescriptor Routing(string routing) { Self.Routing = routing; return this; } - public BulkUpdateDescriptor Parent(string parent) { + public BulkUpdateDescriptor Parent(string parent) { Self.Parent = parent; return this; } - public BulkUpdateDescriptor Timestamp(long timestamp) + public BulkUpdateDescriptor Timestamp(long timestamp) { Self.Timestamp = timestamp; return this; } - public BulkUpdateDescriptor Ttl(string ttl) + public BulkUpdateDescriptor Ttl(string ttl) { Self.Ttl = ttl; return this; } - public BulkUpdateDescriptor RetriesOnConflict(int retriesOnConflict) + public BulkUpdateDescriptor RetriesOnConflict(int retriesOnConflict) { Self.RetriesOnConflict = retriesOnConflict; return this; diff --git a/src/Nest/DSL/Paths/DocumentOptionalPathDescriptor.cs b/src/Nest/DSL/Paths/DocumentOptionalPathDescriptor.cs index d8dab1968a7..ee487a43cb5 100644 --- a/src/Nest/DSL/Paths/DocumentOptionalPathDescriptor.cs +++ b/src/Nest/DSL/Paths/DocumentOptionalPathDescriptor.cs @@ -87,7 +87,7 @@ public abstract class DocumentOptionalPathBase : BasePathRequest public DocumentOptionalPathBase(string id) { this.Id = id; } public DocumentOptionalPathBase(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {} - public DocumentOptionalPathBase(T document) { this.IdFrom = document; } + public DocumentOptionalPathBase(T idFrom) { this.IdFrom = idFrom; } protected override void SetRouteParameters(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) { @@ -152,14 +152,14 @@ public TDescriptor Type() where TAlternative : class } public TDescriptor Id(long id) { - return this.Id(id.ToString()); + return this.Id(id.ToString(CultureInfo.InvariantCulture)); } public TDescriptor Id(string id) { Self.Id = id; return (TDescriptor)this; } - public TDescriptor Object(T @object) + public TDescriptor IdFrom(T @object) { Self.IdFrom = @object; return (TDescriptor)this; diff --git a/src/Nest/DSL/UpdateDescriptor.cs b/src/Nest/DSL/UpdateDescriptor.cs index 39cdf70be3c..d219e9f68a3 100644 --- a/src/Nest/DSL/UpdateDescriptor.cs +++ b/src/Nest/DSL/UpdateDescriptor.cs @@ -8,9 +8,9 @@ namespace Nest { [JsonObject(MemberSerialization = MemberSerialization.OptIn)] - public interface IUpdateRequest : IDocumentOptionalPath - where TUpsert : class - where TDocument : class + public interface IUpdateRequest : IDocumentOptionalPath + where TDocument : class + where TPartialDocument : class { [JsonProperty(PropertyName = "script")] string Script { get; set; } @@ -23,13 +23,13 @@ public interface IUpdateRequest : IDocumentOptionalPath Params { get; set; } [JsonProperty(PropertyName = "upsert")] - TUpsert Upsert { get; set; } + TDocument Upsert { get; set; } [JsonProperty(PropertyName = "doc_as_upsert")] bool? DocAsUpsert { get; set; } [JsonProperty(PropertyName = "doc")] - TDocument Document { get; set; } + TPartialDocument Doc { get; set; } } public class UpdateRequest : UpdateRequest @@ -39,18 +39,25 @@ public UpdateRequest(string id) : base(id) { } public UpdateRequest(long id) : base(id) { } - public UpdateRequest(TDocument document) : base(document) { } + public UpdateRequest(TDocument document, bool useAsUpsert = false) : base(document, useAsUpsert) + { + + } } - public partial class UpdateRequest : DocumentOptionalPathBase, IUpdateRequest - where TUpsert : class - where TDocument : class + public partial class UpdateRequest : DocumentOptionalPathBase, IUpdateRequest + where TDocument : class + where TPartialDocument : class { public UpdateRequest(string id) : base(id) { } public UpdateRequest(long id) : base(id) { } - public UpdateRequest(TUpsert document) : base(document) { } + public UpdateRequest(TDocument idFrom, bool useAsUpsert = false) : base(idFrom) + { + if (useAsUpsert) + this.Upsert = idFrom; + } protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) { @@ -61,52 +68,61 @@ protected override void UpdatePathInfo(IConnectionSettingsValues settings, Elast public string Script { get; set; } public string Language { get; set; } public Dictionary Params { get; set; } - public TUpsert Upsert { get; set; } + public TDocument Upsert { get; set; } public bool? DocAsUpsert { get; set; } - public TDocument Document { get; set; } + public TPartialDocument Doc { get; set; } } - public partial class UpdateDescriptor - : DocumentPathDescriptor, UpdateRequestParameters, TUpsert> - , IUpdateRequest - where TUpsert : class - where TDocument : class + public partial class UpdateDescriptor + : DocumentPathDescriptor, UpdateRequestParameters, TDocument> + , IUpdateRequest + where TDocument : class + where TPartialDocument : class { - private IUpdateRequest Self { get { return this; } } + private IUpdateRequest Self { get { return this; } } - string IUpdateRequest.Script { get; set; } + string IUpdateRequest.Script { get; set; } - string IUpdateRequest.Language { get; set; } + string IUpdateRequest.Language { get; set; } - Dictionary IUpdateRequest.Params { get; set; } + Dictionary IUpdateRequest.Params { get; set; } - TUpsert IUpdateRequest.Upsert { get; set; } + TDocument IUpdateRequest.Upsert { get; set; } - bool? IUpdateRequest.DocAsUpsert { get; set; } + bool? IUpdateRequest.DocAsUpsert { get; set; } - TDocument IUpdateRequest.Document { get; set; } + TPartialDocument IUpdateRequest.Doc { get; set; } - public UpdateDescriptor Script(string script) + public UpdateDescriptor Script(string script) { script.ThrowIfNull("script"); Self.Script = script; return this; } - public UpdateDescriptor Params(Func, FluentDictionary> paramDictionary) + public UpdateDescriptor Params(Func, FluentDictionary> paramDictionary) { paramDictionary.ThrowIfNull("paramDictionary"); Self.Params = paramDictionary(new FluentDictionary()); return this; } + public UpdateDescriptor Id(TDocument document, bool useAsUpsert) + { + ((IDocumentOptionalPath)Self).IdFrom = document; + if (useAsUpsert) + return this.Upsert(document); + return this; + } + + /// /// The full document to be created if an existing document does not exist for a partial merge. /// - public UpdateDescriptor Upsert(TUpsert upsertObject) + public UpdateDescriptor Upsert(TDocument upsertObject) { upsertObject.ThrowIfNull("upsertObject"); Self.Upsert = upsertObject; @@ -116,20 +132,20 @@ public UpdateDescriptor Upsert(TUpsert upsertObject) /// /// The partial update document to be merged on to the existing object. /// - public UpdateDescriptor Document(TDocument @object) + public UpdateDescriptor Doc(TPartialDocument @object) { - Self.Document = @object; + Self.Doc = @object; return this; } - public UpdateDescriptor DocAsUpsert(bool docAsUpsert = true) + public UpdateDescriptor DocAsUpsert(bool docAsUpsert = true) { Self.DocAsUpsert = docAsUpsert; return this; } ///A comma-separated list of fields to return in the response - public UpdateDescriptor Fields(params string[] fields) + public UpdateDescriptor Fields(params string[] fields) { this.Request.RequestParameters.AddQueryString("fields", fields); return this; @@ -137,7 +153,7 @@ public UpdateDescriptor Fields(params string[] fields) ///A comma-separated list of fields to return in the response - public UpdateDescriptor Fields(params Expression>[] typedPathLookups) + public UpdateDescriptor Fields(params Expression>[] typedPathLookups) { if (!typedPathLookups.HasAny()) return this; @@ -148,8 +164,10 @@ public UpdateDescriptor Fields(params Expression pathInfo) { - if (Self.Document != null && Self.Id == null) - Self.Id = new ElasticInferrer(settings).Id(Self.Document); + if (pathInfo.Id.IsNullOrEmpty()) + { + pathInfo.Id = settings.Inferrer.Id(Self.Upsert); + } pathInfo.HttpMethod = PathInfoHttpMethod.POST; } diff --git a/src/Nest/DSL/_Descriptors.generated.cs b/src/Nest/DSL/_Descriptors.generated.cs index 5d64102f8c9..28fb40877fe 100644 --- a/src/Nest/DSL/_Descriptors.generated.cs +++ b/src/Nest/DSL/_Descriptors.generated.cs @@ -5418,13 +5418,13 @@ public TermvectorDescriptor Parent(string parent) ///http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/docs-update.html /// /// - public partial class UpdateDescriptor + public partial class UpdateDescriptor { ///Explicit write consistency setting for the operation - public UpdateDescriptor Consistency(Consistency consistency) + public UpdateDescriptor Consistency(Consistency consistency) { this.Request.RequestParameters.Consistency(consistency); return this; @@ -5432,7 +5432,7 @@ public UpdateDescriptor Consistency(Consistency consistency) ///The script language (default: mvel) - public UpdateDescriptor Lang(string lang) + public UpdateDescriptor Lang(string lang) { this.Request.RequestParameters.Lang(lang); return this; @@ -5440,7 +5440,7 @@ public UpdateDescriptor Lang(string lang) ///ID of the parent document - public UpdateDescriptor Parent(string parent) + public UpdateDescriptor Parent(string parent) { this.Request.RequestParameters.Parent(parent); return this; @@ -5448,7 +5448,7 @@ public UpdateDescriptor Parent(string parent) ///Refresh the index after performing the operation - public UpdateDescriptor Refresh(bool refresh = true) + public UpdateDescriptor Refresh(bool refresh = true) { this.Request.RequestParameters.Refresh(refresh); return this; @@ -5456,7 +5456,7 @@ public UpdateDescriptor Refresh(bool refresh = true) ///Specific replication type - public UpdateDescriptor Replication(Replication replication) + public UpdateDescriptor Replication(Replication replication) { this.Request.RequestParameters.Replication(replication); return this; @@ -5464,7 +5464,7 @@ public UpdateDescriptor Replication(Replication replication) ///Specify how many times should the operation be retried when a conflict occurs (default: 0) - public UpdateDescriptor RetryOnConflict(long retry_on_conflict) + public UpdateDescriptor RetryOnConflict(long retry_on_conflict) { this.Request.RequestParameters.RetryOnConflict(retry_on_conflict); return this; @@ -5472,7 +5472,7 @@ public UpdateDescriptor RetryOnConflict(long retry_on_conflic ///Specific routing value - public UpdateDescriptor Routing(string routing) + public UpdateDescriptor Routing(string routing) { this.Request.RequestParameters.Routing(routing); return this; @@ -5480,7 +5480,7 @@ public UpdateDescriptor Routing(string routing) ///The URL-encoded script definition (instead of using request body) - public UpdateDescriptor ScriptQueryString(string script) + public UpdateDescriptor ScriptQueryString(string script) { this.Request.RequestParameters.Script(script); return this; @@ -5488,7 +5488,7 @@ public UpdateDescriptor ScriptQueryString(string script) ///Explicit operation timeout - public UpdateDescriptor Timeout(string timeout) + public UpdateDescriptor Timeout(string timeout) { this.Request.RequestParameters.Timeout(timeout); return this; @@ -5496,7 +5496,7 @@ public UpdateDescriptor Timeout(string timeout) ///Explicit timestamp for the document - public UpdateDescriptor Timestamp(string timestamp) + public UpdateDescriptor Timestamp(string timestamp) { this.Request.RequestParameters.Timestamp(timestamp); return this; @@ -5504,7 +5504,7 @@ public UpdateDescriptor Timestamp(string timestamp) ///Expiration time for the document - public UpdateDescriptor Ttl(string ttl) + public UpdateDescriptor Ttl(string ttl) { this.Request.RequestParameters.Ttl(ttl); return this; @@ -5512,7 +5512,7 @@ public UpdateDescriptor Ttl(string ttl) ///Explicit version number for concurrency control - public UpdateDescriptor Version(long version) + public UpdateDescriptor Version(long version) { this.Request.RequestParameters.Version(version); return this; @@ -5520,7 +5520,7 @@ public UpdateDescriptor Version(long version) ///Specific version type - public UpdateDescriptor VersionType(VersionType version_type) + public UpdateDescriptor VersionType(VersionType version_type) { this.Request.RequestParameters.VersionType(version_type); return this; diff --git a/src/Nest/DSL/_Requests.generated.cs b/src/Nest/DSL/_Requests.generated.cs index eb404422366..6b838dd4a31 100644 --- a/src/Nest/DSL/_Requests.generated.cs +++ b/src/Nest/DSL/_Requests.generated.cs @@ -4875,7 +4875,7 @@ public string Parent ///http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/docs-update.html /// /// - public partial class UpdateRequest + public partial class UpdateRequest { ///Explicit write consistency setting for the operation diff --git a/src/Nest/ElasticClient-Index.cs b/src/Nest/ElasticClient-Index.cs index 742b716994e..7ea409af173 100644 --- a/src/Nest/ElasticClient-Index.cs +++ b/src/Nest/ElasticClient-Index.cs @@ -13,7 +13,7 @@ public IIndexResponse Index(T @object, Func, IndexDescript { @object.ThrowIfNull("object"); indexSelector = indexSelector ?? (s => s); - var descriptor = indexSelector(new IndexDescriptor().Object(@object)); + var descriptor = indexSelector(new IndexDescriptor().IdFrom(@object)); return this.Dispatch, IndexRequestParameters, IndexResponse>( descriptor, (p, d) => this.RawDispatch.IndexDispatch(p, @object)); @@ -34,7 +34,7 @@ public Task IndexAsync(T @object, Func, In { @object.ThrowIfNull("object"); indexSelector = indexSelector ?? (s => s); - var descriptor = indexSelector(new IndexDescriptor().Object(@object)); + var descriptor = indexSelector(new IndexDescriptor().IdFrom(@object)); return this.DispatchAsync, IndexRequestParameters, IndexResponse, IIndexResponse>( descriptor, (p, d) => this.RawDispatch.IndexDispatchAsync(p, @object)); diff --git a/src/Tests/Nest.Tests.Integration/Core/Bulk/BulkUpdateTests.cs b/src/Tests/Nest.Tests.Integration/Core/Bulk/BulkUpdateTests.cs index 44647f2248b..db0c29c2174 100644 --- a/src/Tests/Nest.Tests.Integration/Core/Bulk/BulkUpdateTests.cs +++ b/src/Tests/Nest.Tests.Integration/Core/Bulk/BulkUpdateTests.cs @@ -26,8 +26,8 @@ public void BulkUpdateObject() { int id = i; descriptor.Update(op => op - .Document(new ElasticsearchProject { Id = id }) - .PartialUpdate(new { name = "SufixedName-" + id}) + .IdFrom(new ElasticsearchProject { Id = id }) + .Doc(new { name = "SufixedName-" + id}) ); } diff --git a/src/Tests/Nest.Tests.Integration/Core/DeleteTests.cs b/src/Tests/Nest.Tests.Integration/Core/DeleteTests.cs index b7030d57ce2..ec1c7b0a4d3 100644 --- a/src/Tests/Nest.Tests.Integration/Core/DeleteTests.cs +++ b/src/Tests/Nest.Tests.Integration/Core/DeleteTests.cs @@ -129,7 +129,7 @@ public void IndexThanDeleteDocumentByObject() //act //now remove the item that was added - this.Client.Delete(f=>f.Object(newDocument).Refresh()); + this.Client.Delete(f=>f.IdFrom(newDocument).Refresh()); //assert //make sure getting by id returns nothing @@ -165,7 +165,7 @@ public void IndexThenDeleteUsingRefresh() //act //now remove the item that was added - this.Client.Delete(d=>d.Object(newDocument).Refresh()); + this.Client.Delete(d=>d.IdFrom(newDocument).Refresh()); //assert //make sure getting by id returns nothing diff --git a/src/Tests/Nest.Tests.Integration/Core/Exists/DocumentExistsTests.cs b/src/Tests/Nest.Tests.Integration/Core/Exists/DocumentExistsTests.cs index 6703b0b60f5..eda1d3f604e 100644 --- a/src/Tests/Nest.Tests.Integration/Core/Exists/DocumentExistsTests.cs +++ b/src/Tests/Nest.Tests.Integration/Core/Exists/DocumentExistsTests.cs @@ -22,12 +22,12 @@ public void TestSuggest() indexResponse.IsValid.Should().BeTrue(); - var existsResponse = this.Client.DocumentExists(d => d.Object(elasticsearchProject).Index(tempIndex)); + var existsResponse = this.Client.DocumentExists(d => d.IdFrom(elasticsearchProject).Index(tempIndex)); existsResponse.IsValid.Should().BeTrue(); existsResponse.Exists.Should().BeTrue(); existsResponse.ConnectionStatus.RequestMethod.Should().Be("HEAD"); - var doesNotExistsResponse = this.Client.DocumentExists(d => d.Object(elasticsearchProject).Index(tempIndex + "-random")); + var doesNotExistsResponse = this.Client.DocumentExists(d => d.IdFrom(elasticsearchProject).Index(tempIndex + "-random")); doesNotExistsResponse.IsValid.Should().BeTrue(); doesNotExistsResponse.Exists.Should().BeFalse(); diff --git a/src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs b/src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs index 0081f47adbe..5e017120a60 100644 --- a/src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs +++ b/src/Tests/Nest.Tests.Integration/Core/UpdateTests.cs @@ -16,7 +16,7 @@ public void TestUpdate() Assert.Greater(project.LOC, 0); var loc = project.LOC; this.Client.Update(u => u - .Object(project) + .IdFrom(project) .Script("ctx._source.loc += 10") .RetryOnConflict(5) .Refresh() @@ -61,7 +61,7 @@ public void DocAsUpsert() var loc = project.LOC; this.Client.Update(u => u .Id(1) - .Document(new ElasticsearchProjectLocUpdate + .Doc(new ElasticsearchProjectLocUpdate { Id = project.Id, LOC = project.LOC + 10 diff --git a/src/Tests/Nest.Tests.Integration/Reproduce/Reproduce732Tests.cs b/src/Tests/Nest.Tests.Integration/Reproduce/Reproduce732Tests.cs index 53f75ef2209..e7880129b5e 100644 --- a/src/Tests/Nest.Tests.Integration/Reproduce/Reproduce732Tests.cs +++ b/src/Tests/Nest.Tests.Integration/Reproduce/Reproduce732Tests.cs @@ -1,4 +1,5 @@ -using Nest.Tests.MockData; +using System.Dynamic; +using Nest.Tests.MockData; using Nest.Tests.MockData.Domain; using NUnit.Framework; using System; @@ -10,6 +11,18 @@ namespace Nest.Tests.Integration.Reproduce [TestFixture] public class Reproduce732Tests : IntegrationTests { + [ElasticType(Name = "load", IdProperty = "Id")] + public class Load + { + public int Id { get; set; } + + public int CustomerId { get; set; } + + public string CustomerName { get; set; } + + public double Total { get; set; } + } + [Test] public void UpdateUsingDynamicObject() { @@ -19,7 +32,7 @@ public void UpdateUsingDynamicObject() var loc = project.LOC; this.Client.Update(u => u .Id(id) - .Document(new + .Doc(new { Id = project.Id, LOC = project.LOC + 10 @@ -30,5 +43,64 @@ public void UpdateUsingDynamicObject() Assert.AreEqual(project.LOC, loc + 10); Assert.AreNotEqual(project.Version, "1"); } + + [Test] + public void TestUpdateUsingPartial() + { + Load load = new Load + { + Id = 1, + CustomerId = 3, + CustomerName = "Customer", + Total = 0.00 + }; + + this.Client.Index(load); + + dynamic partial_load = new ExpandoObject(); + partial_load.Id = load.Id; + partial_load.CustomerId = 3; + + Assert.IsTrue(this.UpdateLoad(partial_load)); + } + + [Test] + public void TestUpdateUsingPartialAndBulk() + { + Load load = new Load + { + Id = 2, + CustomerId = 3, + CustomerName = "Other", + Total = 1.00 + }; + + this.Client.Index(load); + + dynamic partial_load = new ExpandoObject(); + partial_load.Id = load.Id; + partial_load.CustomerId = 3; + + Assert.IsTrue(this.UpdateViaBulk(partial_load)); + } + + public bool UpdateViaBulk(dynamic partial) + { + IBulkResponse response = Client.Bulk(b => b + .Update(u => u + .Id(partial.Id) + .Doc(partial) + )); + + return response.IsValid; + } + public bool UpdateLoad(dynamic partial) + { + IUpdateResponse response = Client.Update(u => u + .Id((int)partial.Id) + .Doc((object)partial) + ); + return response.IsValid; + } } } diff --git a/src/Tests/Nest.Tests.Unit/BigBadUrlUnitTests.cs b/src/Tests/Nest.Tests.Unit/BigBadUrlUnitTests.cs index 484ae16d30f..f40a700dc75 100644 --- a/src/Tests/Nest.Tests.Unit/BigBadUrlUnitTests.cs +++ b/src/Tests/Nest.Tests.Unit/BigBadUrlUnitTests.cs @@ -110,7 +110,7 @@ public void TestAllTheUrls() Do("PUT", "/mydefaultindex/doc/_mapping", c => c.Map(m => m.MapFromAttributes())); Do("PUT", "/mycustomindex/doc/_mapping", c => c.Map(m => m.Index("mycustomindex"))); Do("PUT", "/mycustomindex/customtype/_mapping", c => c.Map(m => m.Index("mycustomindex").Type("customtype"))); - Do("GET", "/mydefaultindex/doc/1/_mlt", c => c.MoreLikeThis(m => m.Object(new Doc { Id = "1" }))); + Do("GET", "/mydefaultindex/doc/1/_mlt", c => c.MoreLikeThis(m => m.IdFrom(new Doc { Id = "1" }))); Do("GET", "/mydefaultindex/doc/1/_mlt", c => c.MoreLikeThis(m => m.Id(1))); Do("GET", "/mycustomindex/mycustomtype/1/_mlt", c => c.MoreLikeThis(m => m.Id(1).Index("mycustomindex").Type("mycustomtype"))); Do("POST", "/_msearch", c => c.MultiSearch(m => m.Search(s => s.MatchAll()))); @@ -150,8 +150,8 @@ public void TestAllTheUrls() Do("GET", "/mydefaultindex/_status", c => c.Status(s => s.Index())); Do("DELETE", "/mydefaultindex/.percolator/mypercolator", c => c.UnregisterPercolator("mypercolator")); Do("DELETE", "/mycustomindex/.percolator/mypercolator", c => c.UnregisterPercolator("mypercolator", r => r.Index("mycustomindex"))); - Do("POST", "/mydefaultindex/doc/1/_update", c => c.Update(u => u.Id(1).Document(new OtherDoc { Name = "asd" }))); - Do("POST", "/mydefaultindex/customtype/1/_update", c => c.Update(u => u.Id(1).Type("customtype").Document(new OtherDoc { Name = "asd" }))); + Do("POST", "/mydefaultindex/doc/1/_update", c => c.Update(u => u.Id(1).Doc(new OtherDoc { Name = "asd" }))); + Do("POST", "/mydefaultindex/customtype/1/_update", c => c.Update(u => u.Id(1).Type("customtype").Doc(new OtherDoc { Name = "asd" }))); Do("PUT", "/mydefaultindex/_settings", c => c.UpdateSettings(u => u.AutoExpandReplicas(false))); Do("PUT", "/mycustomindex/_settings", c => c.UpdateSettings(u => u.Index("mycustomindex").AutoExpandReplicas(false))); Do("POST", "/_all/doc/_validate/query", c => c.Validate(v => v.AllIndices())); diff --git a/src/Tests/Nest.Tests.Unit/Core/Bulk/BulkTests.cs b/src/Tests/Nest.Tests.Unit/Core/Bulk/BulkTests.cs index 8a2f42407ab..edaf8d45e97 100644 --- a/src/Tests/Nest.Tests.Unit/Core/Bulk/BulkTests.cs +++ b/src/Tests/Nest.Tests.Unit/Core/Bulk/BulkTests.cs @@ -24,9 +24,9 @@ public void BulkOperations() .Document(new ElasticsearchProject { Id = 4 }) .VersionType(VersionType.ExternalGte)) .Update(i => i - .Document(new ElasticsearchProject { Id = 3 }) + .IdFrom(new ElasticsearchProject { Id = 3 }) .VersionType(VersionType.External) - .PartialUpdate(new { name = "NEST"}) + .Doc(new { name = "NEST"}) ) ); var status = result.ConnectionStatus; @@ -52,11 +52,10 @@ public void BulkOperations_ObjectInitializer() { VersionType = VersionType.ExternalGte }}, - { new BulkUpdateOperation + { new BulkUpdateOperation(new ElasticsearchProject { Id = 3}) { - Document = new ElasticsearchProject { Id = 3 }, VersionType = VersionType.External, - PartialUpdate = new { name = "NEST"} + Doc = new { name = "NEST"} }}, } }); @@ -69,8 +68,8 @@ public void BulkUpdateDetails() { var result = this._client.Bulk(b => b .Update(i => i - .Document(new ElasticsearchProject { Id = 3 }) - .PartialUpdate(new { name = "NEST" }) + .IdFrom(new ElasticsearchProject { Id = 3 }) + .Doc(new { name = "NEST" }) .RetriesOnConflict(4) ) .Index(i=>i diff --git a/src/Tests/Nest.Tests.Unit/Core/MoreLikeThis/MltUrlTests.cs b/src/Tests/Nest.Tests.Unit/Core/MoreLikeThis/MltUrlTests.cs index 91c61aa201b..3db82a87bea 100644 --- a/src/Tests/Nest.Tests.Unit/Core/MoreLikeThis/MltUrlTests.cs +++ b/src/Tests/Nest.Tests.Unit/Core/MoreLikeThis/MltUrlTests.cs @@ -19,7 +19,7 @@ public void MltSimpleById() [Test] public void MltSimpleByObject() { - var result = this._client.MoreLikeThis(mlt => mlt.Object(new ElasticsearchProject { Id = 1 })); + var result = this._client.MoreLikeThis(mlt => mlt.IdFrom(new ElasticsearchProject { Id = 1 })); var status = result.ConnectionStatus; StringAssert.EndsWith("/nest_test_data/elasticsearchprojects/1/_mlt", status.RequestUrl); StringAssert.AreEqualIgnoringCase("GET", status.RequestMethod); @@ -30,7 +30,7 @@ public void MltSimpleByObjectAlternativeIndexAndType() var result = this._client.MoreLikeThis(mlt => mlt .Index("someotherindex") .Type("sillytypename") - .Object(new ElasticsearchProject { Id = 1 }) + .IdFrom(new ElasticsearchProject { Id = 1 }) ); var status = result.ConnectionStatus; StringAssert.EndsWith("/someotherindex/sillytypename/1/_mlt", status.RequestUrl); diff --git a/src/Tests/Nest.Tests.Unit/Core/Update/UpdateAPIConsistencyTests.cs b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateAPIConsistencyTests.cs new file mode 100644 index 00000000000..6e18da0edde --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateAPIConsistencyTests.cs @@ -0,0 +1,154 @@ +using System.Collections.Generic; +using System.Reflection; +using NUnit.Framework; +using Nest.Tests.MockData.Domain; +using NUnit.Framework.Constraints; + +namespace Nest.Tests.Unit.Core.Update +{ + [TestFixture] + public class UpdateAPIConsistencyTests : BaseJsonTests + { + public class MyDocument + { + public int Id { get; set; } + + public string Name { get; set; } + + public string Country { get; set; } + } + + public class MyUpdate + { + public string Name { get; set; } + } + + private MyDocument _document = new MyDocument + { + Id = 1, + Name = "My document", + Country = "Netherlands" + }; + + private MyUpdate _updateDocument = new MyUpdate { Name = "Martijn" }; + + [Test] + public void UpdateAPI_IdFromDocument() + { + this._client.Update(u => u + .IdFrom(_document) + .Doc(_updateDocument) + .DocAsUpsert() + ); + } + + [Test] + public void BulkUpdateAPI_IdFromDocument() + { + this._client.Bulk(u => u + .Update(o=>o + .IdFrom(_document) + .Doc(_updateDocument) + .DocAsUpsert() + ) + ); + } + + [Test] + public void UpdateAPI_IdFromDocument_OIS() + { + this._client.Update(new UpdateRequest(_document) + { + Doc = _updateDocument, + DocAsUpsert = true + } + ); + } + + [Test] + public void BulkUpdateAPI_IdFromDocument_OIS() + { + this._client.Bulk(new BulkRequest + { + Operations = new List + { + { + new BulkUpdateOperation(_document) + { + Doc = _updateDocument, + DocAsUpsert = true + } + } + } + }); + } + + [Test] + public void UpdateAPI_IdFromDocument_PassToUpsert() + { + this._client.Update(u => u + .Id(_document, useAsUpsert: true) + .Doc(_updateDocument) + ); + } + + [Test] + public void BulkUpdateAPI_IdFromDocument_PassToUpsert() + { + this._client.Bulk(u => u + .Update(o=>o + .IdFrom(_document, useAsUpsert: true) + .Doc(_updateDocument) + ) + ); + } + + [Test] + public void UpdateAPI_IdFromDocument_PassToUpsert_OIS() + { + this._client.Update(new UpdateRequest(_document, useAsUpsert: true) + { + Doc = _updateDocument + }); + } + + [Test] + public void BulkUpdateAPI_IdFromDocument_PassToUpsert_OIS() + { + this._client.Bulk(new BulkRequest + { + Operations = new List + { + { + new BulkUpdateOperation(_document, useIdFromAsUpsert: true) + { + Doc = _updateDocument, + } + } + } + }); + } + + [Test] + public void UpdateAPI_ManualId_WithUpsert() + { + this._client.Update(u => u + .Id(_document.Id) + .Doc(_updateDocument) + .Upsert(_document) + ); + } + + [Test] + public void BulkUpdateAPI_ManualId_WithUpsert() + { + this._client.Bulk(u => u + .Update(o=>o + .Id(_document.Id) + .Doc(_updateDocument) + .Upsert(_document) + ) + ); + } + } +} diff --git a/src/Tests/Nest.Tests.Unit/Core/Update/UpdateRequestTests.cs b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateRequestTests.cs index d227266157f..804a4742b21 100644 --- a/src/Tests/Nest.Tests.Unit/Core/Update/UpdateRequestTests.cs +++ b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateRequestTests.cs @@ -12,7 +12,7 @@ public class UpdateRequestTests : BaseJsonTests public void Inferred() { var result = this._client.Update(u => u - .Object(new ElasticsearchProject { Id = 2 }) + .IdFrom(new ElasticsearchProject { Id = 2 }) ); Assert.NotNull(result, "PutWarmer result should not be null"); var status = result.ConnectionStatus; @@ -25,7 +25,7 @@ public void InferredWithOverrides() { var result = this._client.Update(u => u .Index("myindex") - .Object(new ElasticsearchProject { Id = 2 }) + .IdFrom(new ElasticsearchProject { Id = 2 }) ); Assert.NotNull(result, "PutWarmer result should not be null"); var status = result.ConnectionStatus; diff --git a/src/Tests/Nest.Tests.Unit/Core/Update/UpdateTests.cs b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateTests.cs index 73e00f4edc1..2bad38c3b3c 100644 --- a/src/Tests/Nest.Tests.Unit/Core/Update/UpdateTests.cs +++ b/src/Tests/Nest.Tests.Unit/Core/Update/UpdateTests.cs @@ -48,8 +48,8 @@ public void UpdateUsingPartial() var partialUpdate = new PartialElasticSearchProject { Name = "NEST", Country = "Netherlands" }; var s = new UpdateDescriptor() - .Object(originalProject) //only used to infer the id - .Document(partialUpdate); //the actual partial update statement; + .IdFrom(originalProject) //only used to infer the id + .Doc(partialUpdate); //the actual partial update statement; this.JsonEquals(s, MethodInfo.GetCurrentMethod()); } diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index 6cb98fa5f81..c7001d6e682 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -185,6 +185,7 @@ + diff --git a/src/Tests/Nest.Tests.Unit/ObjectInitializer/Update/UpdateRequestTests.cs b/src/Tests/Nest.Tests.Unit/ObjectInitializer/Update/UpdateRequestTests.cs index 89fb6f34dcd..db49afabe3e 100644 --- a/src/Tests/Nest.Tests.Unit/ObjectInitializer/Update/UpdateRequestTests.cs +++ b/src/Tests/Nest.Tests.Unit/ObjectInitializer/Update/UpdateRequestTests.cs @@ -18,7 +18,7 @@ public UpdateRequestTests() var request = new UpdateRequest(project) { - Document = new { Name = "NEST" }, + Doc = new { Name = "NEST" }, DocAsUpsert = true };