protected long UpdateAll(string organizationId, QueryOptions options, object update, bool sendNotifications = true)
 {
     return(UpdateAll(new[] { organizationId }, options, update, sendNotifications));
 }
        protected long UpdateAll(string[] organizationIds, QueryOptions options, object update, bool sendNotifications = true)
        {
            long recordsAffected = 0;

            var searchDescriptor = new SearchDescriptor <T>()
                                   .Filter(options.GetElasticSearchFilter <T>() ?? Filter <T> .MatchAll())
                                   .Source(s => s.Include(f => f.Id))
                                   .SearchType(SearchType.Scan)
                                   .Scroll("4s")
                                   .Size(Settings.Current.BulkBatchSize);

            _elasticClient.EnableTrace();
            var scanResults = _elasticClient.Search <T>(searchDescriptor);

            _elasticClient.DisableTrace();

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

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

            while (results.Hits.Any())
            {
                var bulkResult = _elasticClient.Bulk(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);
                });

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

                if (EnableCache)
                {
                    results.Hits.ForEach(d => InvalidateCache(d.Id));
                }

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

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

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

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

            return(recordsAffected);
        }
Ejemplo n.º 3
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);
        }