public async Task RemoveAsync(ICollection <T> documents, bool sendNotification = true) { if (documents == null || documents.Count == 0) { return; } await OnDocumentsRemovingAsync(documents).AnyContext(); foreach (var g in documents.GroupBy(d => GetDocumentIndexFunc?.Invoke(d))) { await Context.ElasticClient.DeleteByQueryAsync <T>(q => q.Query(q1 => q1.Ids(g.Select(d => d.Id))).Index(g.Key)).AnyContext(); } await OnDocumentsRemovedAsync(documents, sendNotification).AnyContext(); }
public async Task RemoveAsync(ICollection <T> documents, bool sendNotification = true) { if (documents == null || documents.Count == 0) { return; } await OnDocumentsRemovingAsync(documents).AnyContext(); var documentsByIndex = documents.GroupBy(d => GetDocumentIndexFunc?.Invoke(d)).ToList(); foreach (var g in documentsByIndex) { await Context.ElasticClient.DeleteByQueryAsync <T>(q => q.Query(q1 => q1.Ids(g.Select(d => d.Id))).Index(g.Key)).AnyContext(); } // TODO: Add tests and ensure we can bulk delete. //foreach (var group in documentsByIndex.Where(g => String.IsNullOrEmpty(g.Key))) { // var response = await Context.ElasticClient.DeleteByQueryAsync<T>(q => q.Query(q1 => q1.Ids(group.Select(d => d.Id))).Index(group.Key)).AnyContext(); // if (!response.IsValid) // throw new ApplicationException(String.Join("\r\n", response.ServerError?.Error, response.ConnectionStatus.OriginalException)); //} //var indexedDocuments = documentsByIndex.Where(g => !String.IsNullOrEmpty(g.Key)).ToList(); //if (indexedDocuments.Count > 0) { // var response = await Context.ElasticClient.BulkAsync(bulk => { // foreach (var group in indexedDocuments) // bulk.DeleteMany(group.Select(g => g.Id), (b, id) => b.Index(group.Key)); // return bulk; // }).AnyContext(); // if (!response.IsValid) // throw new ApplicationException(String.Join("\r\n", response.ItemsWithErrors.Select(i => i.Error)), response.ConnectionStatus.OriginalException); //} await OnDocumentsRemovedAsync(documents, sendNotification).AnyContext(); }
public async Task RemoveAsync(IEnumerable <T> documents, bool sendNotification = true) { var docs = documents?.ToList(); if (docs == null || docs.Any(d => d == null)) { throw new ArgumentNullException(nameof(documents)); } if (docs.Count == 0) { return; } if (HasMultipleIndexes) { foreach (var documentGroup in docs.GroupBy(TimeSeriesType.GetDocumentIndex)) { await TimeSeriesType.EnsureIndexAsync(documentGroup.First()).AnyContext(); } } await OnDocumentsRemovingAsync(docs).AnyContext(); // TODO: support Parent and child docs. if (docs.Count == 1) { var document = docs.First(); var response = await _client.DeleteAsync(document, descriptor => descriptor.Index(GetDocumentIndexFunc?.Invoke(document)).Type(ElasticType.Name)).AnyContext(); _logger.Trace(() => response.GetRequest()); if (!response.IsValid) { string message = response.GetErrorMessage(); _logger.Error().Exception(response.ConnectionStatus.OriginalException).Message(message).Property("request", response.GetRequest()).Write(); throw new ApplicationException(message, response.ConnectionStatus.OriginalException); } } else { var documentsByIndex = docs.GroupBy(d => GetDocumentIndexFunc?.Invoke(d)); var response = await _client.BulkAsync(bulk => { foreach (var group in documentsByIndex) { bulk.DeleteMany <T>(group.Select(g => g.Id), (b, id) => b.Index(group.Key).Type(ElasticType.Name)); } return(bulk); }).AnyContext(); _logger.Trace(() => response.GetRequest()); if (!response.IsValid) { string message = response.GetErrorMessage(); _logger.Error().Exception(response.ConnectionStatus.OriginalException).Message(message).Property("request", response.GetRequest()).Write(); throw new ApplicationException(message, response.ConnectionStatus.OriginalException); } } await OnDocumentsRemovedAsync(docs, sendNotification).AnyContext(); }