public virtual Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class { var documentType = typeof(TDocument); var client = this._clientFactory.GetClient(); var documentPath = new DocumentPath <TDocument>(context.Id) .Type(documentType); var index = this.GetIndex(context.IndexContext); var updateDescriptor = new UpdateDescriptor <TDocument, dynamic>(documentPath) .Index(index); if (context.Script != null) { updateDescriptor.Script(d => d.Inline(context.Script) .Params(context.ScriptParams.ToDictionary(k => k.Key, v => v.Value))); } else { updateDescriptor.Upsert(context.Document) .Doc(context.PartialUpdate ?? context.Document); } var updateDocResponse = client.Update <TDocument, dynamic>(updateDescriptor); this._responseHandler.ValdateAndHandleException(updateDocResponse, true); return(Task.CompletedTask); }
/// <summary>生成部分更新查询</summary> private IUpdateRequest <TIndexType, TIndexType> UseUpdateQuery(UpdateDescriptor <TIndexType, TIndexType> descriptor, TIndexType metadata) { descriptor.Type(this.IndexType) .Doc(metadata) .DocAsUpsert(true) .Upsert(metadata) ; return(descriptor); }
protected ITransactionOperation CreateTransactionOperation(TransactionType transactionType) { ITransactionOperation transactionOperation; switch (transactionType) { case TransactionType.CreateNode: transactionOperation = new CreateNode(MapObjects.Parameters); break; case TransactionType.DeleteNode: transactionOperation = new DeleteNode(MapObjects.Parameters); break; case TransactionType.CreateRelationship: transactionOperation = new CreateRelationship(MapObjects.Parameters); break; case TransactionType.DeleteRelationship: transactionOperation = new DeleteRelationship(MapObjects.Parameters); break; case TransactionType.CreateDescriptor: transactionOperation = new CreateDescriptor(MapObjects.Parameters); break; case TransactionType.CreateMetadata: transactionOperation = new CreateMetadata(MapObjects.Parameters); break; case TransactionType.UpdateMetadata: transactionOperation = new UpdateMetadata(MapObjects.Parameters); break; case TransactionType.DeleteMetadata: transactionOperation = new DeleteMetadata(MapObjects.Parameters); break; case TransactionType.UpdateNode: transactionOperation = new UpdateNode(MapObjects.Parameters); break; case TransactionType.UpdateRelationship: transactionOperation = new UpdateRelationship(MapObjects.Parameters); break; case TransactionType.UpdateDescriptor: transactionOperation = new UpdateDescriptor(MapObjects.Parameters); break; default: throw new NotSupportedException("The requested transaction type doesn't exist."); } return(transactionOperation); }
public void UpsertUsingScript() { var s = new UpdateDescriptor<UpsertCount, UpsertCount>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(new UpsertCount { Count = 1 }); this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
private IUpdateRequest <T, T> GetMetaForUpdate(UpdateDescriptor <T, T> request, T data, UpdateAction action, Expression <Func <T, IList> > fields, bool immediately = true) { var result = request.Index(IndexName).Script(GetScriptForUpdate(data, action, fields)); if (immediately) { result.Refresh(Elasticsearch.Net.Refresh.True); } return(result); }
public void UpdateUsingPartialWithNull() { var originalProject = new ElasticsearchProject { Id = 1, Name = "NEST", Country = "UK" }; var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null }; var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticsearchProjectWithNull>() .IdFrom(originalProject) //only used to infer the id .Doc(partialUpdate); //the actual partial update statement; this.JsonEquals(s, MethodBase.GetCurrentMethod()); }
public void UpdateUsingPartial() { var originalProject = new ElasticSearchProject { Id = 1, Name = "NeST", Country = "UK" }; var partialUpdate = new PartialElasticSearchProject { Name = "NEST", Country = "Netherlands" }; var s = new UpdateDescriptor<ElasticSearchProject, PartialElasticSearchProject>() .Object(originalProject) //only used to infer the id .Document(partialUpdate); //the actual partial update statement; this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpsertUsingScriptAndPartialObject() { var s = new UpdateDescriptor<ElasticSearchProject, object>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(new { count = 4}); this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpsertUsingScriptAndPartialObject() { var s = new UpdateDescriptor <ElasticSearchProject, object>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(new { count = 4 }); this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpsertUsingScript() { var s = new UpdateDescriptor <ElasticsearchProject, ElasticsearchProject>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(u => u .Add("count", 1) ); this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpsertUsingScript() { var s = new UpdateDescriptor <UpsertCount, UpsertCount>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(new UpsertCount { Count = 1 }); this.JsonEquals(s, MethodBase.GetCurrentMethod()); }
public void UpsertUsingScript() { var s = new UpdateDescriptor<ElasticsearchProject, ElasticsearchProject>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ) .Upsert(u=>u .Add("count", 1) ); this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpdateUsingPartial() { var originalProject = new ElasticSearchProject { Id = 1, Name = "NeST", Country = "UK" }; var partialUpdate = new PartialElasticSearchProject { Name = "NEST", Country = "Netherlands" }; var s = new UpdateDescriptor <ElasticSearchProject, PartialElasticSearchProject>() .Object(originalProject) //only used to infer the id .Document(partialUpdate); //the actual partial update statement; this.JsonEquals(s, MethodInfo.GetCurrentMethod()); }
public void UpdateUsingPartialWithNull() { var originalProject = new ElasticsearchProject { Id = 1, Name = "NEST", Country = "UK" }; var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null }; var s = new UpdateDescriptor <ElasticsearchProject, PartialElasticsearchProjectWithNull>() .IdFrom(originalProject) //only used to infer the id .Doc(partialUpdate); //the actual partial update statement; this.JsonEquals(s, MethodBase.GetCurrentMethod()); }
public void TestUpdate() { var s = new UpdateDescriptor<ElasticSearchProject>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ); var json = ElasticClient.Serialize(s); var expected = @" { script: ""ctx._source.counter += count"", params: { count: 4 } }"; Assert.True(json.JsonEquals(expected), json); }
public void TestUpdate() { var s = new UpdateDescriptor <ElasticSearchProject>() .Script("ctx._source.counter += count") .Params(p => p .Add("count", 4) ); var json = TestElasticClient.Serialize(s); var expected = @" { script: ""ctx._source.counter += count"", params: { count: 4 } }" ; Assert.True(json.JsonEquals(expected), json); }
private IUpdateRequest <T, T> GetMetaForUpdate(UpdateDescriptor <T, T> request, T data, bool immediately = true, params Expression <Func <T, object> >[] fields) { var result = request.Index(IndexName); if (fields.Any()) { result.Script(GetScriptUpdateByQuery(data, fields)); } else { result.Doc(data); } if (immediately) { result.Refresh(Elasticsearch.Net.Refresh.True); } return(result); }
/// <summary> /// 根据id更新部分实体字段 /// </summary> /// <typeparam name="TPartialDocument">包含实体T中部分字段的是model</typeparam> /// <param name="id">主键</param> /// <param name="partEntity">部分实体model</param> /// <param name="routing">路由键</param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > UpdatePartialDocumentAsync <TPartialDocument, TPrimaryKeyType>(TPrimaryKeyType id, InsertUpdateModel <TPartialDocument> insertUpdateModel) where TPartialDocument : class, new() { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = insertUpdateModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } UpdateDescriptor <TEntity, TPartialDocument> updRequest = new UpdateDescriptor <TEntity, TPartialDocument>(indexName, id.ToString()); updRequest.Doc(insertUpdateModel.Entity); updRequest.Index(indexName); if (!string.IsNullOrEmpty(insertUpdateModel.Routing)) { updRequest.Routing(insertUpdateModel.Routing); } var response = await client.UpdateAsync <TEntity, TPartialDocument>(updRequest).ConfigureAwait(false); GetDebugInfo(response); httpResponseResultModel.IsSuccess = (response.Result == Result.Updated); return(httpResponseResultModel); }
protected IUpdateRequest <Project, Project> UpdateFluent(string id, UpdateDescriptor <Project, Project> d) => d .Doc(new Project { Description = id + " updated" });
private void Add(UpdateDescriptor updateDescriptor) { updateDescriptors.Add(updateDescriptor); docIdToUpdateDescriptorMap.Add(updateDescriptor.DocumentId, updateDescriptor); }