/// <summary> /// 批量索引 /// </summary> /// <param name="entities">实体集合</param> /// <param name="routing">路由键</param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > IndexBatchAsync <T>(InsertUpdateModel <IList <T> > insertUpdateModel) where T : class, new() { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = insertUpdateModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } BulkDescriptor bulkDescriptor = new BulkDescriptor(); bulkDescriptor.IndexMany(insertUpdateModel.Entity); bulkDescriptor.Index(indexName); if (!string.IsNullOrEmpty(insertUpdateModel.Routing)) { bulkDescriptor.Routing(new Routing(insertUpdateModel.Routing)); } var result = await client.BulkAsync(bulkDescriptor).ConfigureAwait(false); GetDebugInfo(result); //if (result.Errors || !result.ItemsWithErrors.IsNullOrEmpty()) //{ // Task.Run(() => // { // var json = JsonHelper.SerializeObject(insertUpdateModel.Entity); // WriteLog.WriteLogsAsync(json, "batchError"); // }); //} var isSuccess = result.ItemsWithErrors.IsListNullOrEmpty(); string errorMessage = ""; if (!isSuccess) { var errorIdList = result.ItemsWithErrors.Select(t => t.Id); errorMessage = string.Join(",", errorIdList); } httpResponseResultModel.IsSuccess = isSuccess; httpResponseResultModel.ErrorMessage = errorMessage; return(httpResponseResultModel); }
/// <summary> /// 批量删除索引 /// </summary> /// <typeparam name="TPrimaryKeyType"></typeparam> /// <param name="ids"></param> /// <param name="routing"></param> /// <returns></returns> public async Task <HttpResponseResultModel <bool> > DeleteBatchAsync <TPrimaryKeyType>(DeleteBatchModel <TPrimaryKeyType> deleteBatchModel) { HttpResponseResultModel <bool> httpResponseResultModel = new HttpResponseResultModel <bool>(); string indexName = deleteBatchModel.IndexName?.ToLower(); if (string.IsNullOrEmpty(indexName)) { indexName = PocoIndexName; } BulkDescriptor bulkDescriptor = new BulkDescriptor(); foreach (var id in deleteBatchModel.IdList) { bulkDescriptor.AddOperation(new BulkDeleteOperation <TEntity>(id.ToString())); } bulkDescriptor.Index(indexName); if (!string.IsNullOrEmpty(deleteBatchModel.Routing)) { bulkDescriptor.Routing(new Routing(deleteBatchModel.Routing)); } var result = await client.BulkAsync(bulkDescriptor).ConfigureAwait(false); GetDebugInfo(result); var isSuccess = result.ItemsWithErrors.IsListNullOrEmpty(); httpResponseResultModel.IsSuccess = isSuccess; string errorMessage = ""; if (!isSuccess) { var errorIdList = result.ItemsWithErrors.Select(t => t.Id); errorMessage = string.Join(",", errorIdList); } httpResponseResultModel.ErrorMessage = errorMessage; return(httpResponseResultModel); }