Exemplo n.º 1
0
        protected async Task <long> RemoveAllAsync(QueryOptions options, bool sendNotifications = true)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var fields = new List <string>(new[] { "id" });

            if (_isOwnedByOrganization)
            {
                fields.Add("organization_id");
            }
            if (_isOwnedByProject)
            {
                fields.Add("project_id");
            }
            if (_isOwnedByStack)
            {
                fields.Add("stack_id");
            }
            if (_isStack)
            {
                fields.Add("signature_hash");
            }

            long recordsAffected  = 0;
            var  searchDescriptor = new SearchDescriptor <T>()
                                    .Index(_index.Name)
                                    .Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes))
                                    .Source(s => s.Include(fields.ToArray()))
                                    .Size(Settings.Current.BulkBatchSize);

            _elasticClient.EnableTrace();
            var documents = (await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext()).Documents.ToList();

            _elasticClient.DisableTrace();
            while (documents.Count > 0)
            {
                recordsAffected += documents.Count;
                await RemoveAsync(documents, sendNotifications).AnyContext();

                documents = (await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext()).Documents.ToList();
            }
            _elasticClient.DisableTrace();

            return(recordsAffected);
        }
Exemplo n.º 2
0
        protected async Task <long> UpdateAllAsync(string[] organizationIds, QueryOptions options, object update, bool sendNotifications = true)
        {
            long recordsAffected = 0;

            var searchDescriptor = new SearchDescriptor <T>()
                                   .Index(_index.Name)
                                   .Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes))
                                   .Source(s => s.Include(f => f.Id))
                                   .SearchType(SearchType.Scan)
                                   .Scroll("4s")
                                   .Size(Settings.Current.BulkBatchSize);

            _elasticClient.EnableTrace();
            var scanResults = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext();

            _elasticClient.DisableTrace();

            // Check to see if no scroll id was returned. This will occur when the index doesn't exist.
            if (!scanResults.IsValid || scanResults.ScrollId == null)
            {
                return(0);
            }

            var results = await _elasticClient.ScrollAsync <T>("4s", scanResults.ScrollId).AnyContext();

            while (results.Hits.Any())
            {
                var bulkResult = await _elasticClient.BulkAsync(b => {
                    string script = update as string;
                    if (script != null)
                    {
                        results.Hits.ForEach(h => b.Update <T>(u => u.Id(h.Id).Index(h.Index).Script(script)));
                    }
                    else
                    {
                        results.Hits.ForEach(h => b.Update <T, object>(u => u.Id(h.Id).Index(h.Index).Doc(update)));
                    }

                    return(b);
                }).AnyContext();

                if (!bulkResult.IsValid)
                {
                    Logger.Error().Message("Error occurred while bulk updating").Exception(bulkResult.ConnectionStatus.OriginalException).Write();
                    return(0);
                }

                if (EnableCache)
                {
                    foreach (var hit in results.Hits)
                    {
                        await InvalidateCacheAsync(hit.Id).AnyContext();
                    }
                }

                recordsAffected += results.Documents.Count();
                results          = await _elasticClient.ScrollAsync <T>("4s", results.ScrollId).AnyContext();
            }

            if (recordsAffected <= 0)
            {
                return(0);
            }

            if (!sendNotifications)
            {
                return(recordsAffected);
            }

            foreach (var organizationId in organizationIds)
            {
                await PublishMessageAsync(new EntityChanged {
                    ChangeType     = ChangeType.Saved,
                    OrganizationId = organizationId,
                    Type           = _entityType
                }, TimeSpan.FromSeconds(1.5)).AnyContext();
            }

            return(recordsAffected);
        }