protected long UpdateAll(string organizationId, 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(RepositoryConstants.BATCH_SIZE);

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

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

                results.Hits.ForEach(d => InvalidateCache(d.Id));

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

            if (EnableNotifications && sendNotifications)
            {
                PublishMessage(new EntityChanged {
                    ChangeType     = EntityChangeType.UpdatedAll,
                    OrganizationId = organizationId,
                    Type           = _entityType
                });
            }

            return(recordsAffected);
        }
Esempio n. 2
0
        protected long RemoveAll(QueryOptions options, bool sendNotifications = true)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

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

            if (_isOwnedByOrganization)
            {
                fields.Add("organization_id");//CommonFieldNames.OrganizationId);
            }
            if (_isOwnedByProject)
            {
                fields.Add("project_id");//CommonFieldNames.ProjectId);
            }
            if (_isOwnedByStack)
            {
                fields.Add("stack_id");//CommonFieldNames.StackId);
            }
            if (_isStack)
            {
                fields.Add("signature_hash");
            }

            long recordsAffected = 0;

            var searchDescriptor = new SearchDescriptor <T>()
                                   .Filter(options.GetElasticSearchFilter <T>() ?? Filter <T> .MatchAll())
                                   .Source(s => s.Include(fields.ToArray()))
                                   .Size(Settings.Current.BulkBatchSize);

            var documents = _elasticClient.Search <T>(searchDescriptor).Documents.ToList();

            while (documents.Count > 0)
            {
                recordsAffected += documents.Count;
                Remove(documents, sendNotifications);

                documents = _elasticClient.Search <T>(searchDescriptor).Documents.ToList();
            }

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