Ejemplo n.º 1
0
        public async Task <long> UpdateAsync(IList <string> ids, object part, Refresh refresh = Refresh.WaitFor)
        {
            if (!ids.HasItems())
            {
                throw new ArgumentException(string.Format(Utils.ARGUMENT_EMPTY_LIST_MESSAGE, nameof(ids)), nameof(ids));
            }

            if (part == null)
            {
                throw new ArgumentNullException(string.Format(Utils.ARGUMENT_NULL_MESSAGE, nameof(part)), nameof(part));
            }

            var descriptor = new BulkDescriptor();

            foreach (var id in ids)
            {
                descriptor.Update <T, object>(x => x.Id(id)
                                              .Doc(part)
                                              .RetriesOnConflict(_maxRetries)
                                              );
            }

            descriptor.Refresh(refresh);

            var response = await _client.BulkAsync(descriptor);

            return(!(response.IsValid && response.Items.Count > 0) ? 0 : response.Items.Count);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deletes the specified documents.
        /// </summary>
        /// <param name="items">The items.</param>
        public override void Delete(ref IEnumerable <ContentBatchOperationDescriber> items)
        {
            var descriptor = new BulkDescriptor();

            _ = descriptor.Refresh(Refresh.WaitFor);

            foreach (var item in items)
            {
                item.State = ContentDeletionStateEnum.SucceededIndexDeletion;
                var document = (item.Content as Document) !;
                foreach (var field in document.Fields)
                {
                    var indexId = Connection.GetIndexId(field.Key);
                    _ = descriptor.Delete <object>(op => op
                                                   .Index(indexId)
                                                   .Id(document.Id)
                                                   );
                }
            }

            var response = Connection.Client.Bulk(descriptor);

            if (response.Errors && response.ItemsWithErrors.Any())
            {
                foreach (var errorItem in response.ItemsWithErrors)
                {
                    var item = items.First(i => i.Content.Id.ToString(CultureInfo.InvariantCulture) == errorItem.Id && errorItem.Type == "_doc");
                    item.State = ContentDeletionStateEnum.ErrorIndexDeletion;
                    item.Error = errorItem.Error;
                }
            }
        }
Ejemplo n.º 3
0
        public async Task <int> CreateAsync(IList <T> items, Refresh refresh = Refresh.WaitFor)
        {
            if (!items.HasItems())
            {
                throw new ArgumentException(string.Format(Utils.ARGUMENT_EMPTY_LIST_MESSAGE, nameof(items)), nameof(items));
            }

            var descriptor = new BulkDescriptor();

            foreach (var item in items)
            {
                if (string.IsNullOrWhiteSpace(item.Id))
                {
                    descriptor.Index <T>(x => x
                                         .Document(item));
                }
                else
                {
                    descriptor.Create <T>(x => x
                                          .Document(item));
                }
            }

            descriptor.Refresh(refresh);

            var response = await _client.BulkAsync(descriptor);

            return(!response.IsValid ? 0 : response.Items.Count);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Indexes the specified documents.
        /// </summary>
        /// <param name="documents">The documents.</param>
        public override void Index(IEnumerable <Document> documents)
        {
            var descriptor = new BulkDescriptor();

            _ = descriptor.Refresh(Refresh.WaitFor);

            var docId = 0;

            foreach (var document in documents)
            {
                foreach (var field in document.Fields)
                {
                    var     indexId   = Connection.GetIndexId(field.Key);
                    dynamic updateDoc = new System.Dynamic.ExpandoObject();
                    updateDoc.value = field.Value;

                    _ = descriptor.Index <object>(op => op
                                                  .Index(indexId)
                                                  // TODO: docIds should come from document object, this is only for testing purposes
                                                  .Id(document.Id > 0 ? document.Id : docId++)
                                                  .Document(updateDoc)
                                                  );
                }
            }
            var result = Connection.Client.Bulk(descriptor);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Indexes the specified document ids.
        /// </summary>
        /// <param name="documentIds">The document ids.</param>
        /// <param name="fields">The fields.</param>
        public override void Index(IEnumerable <long> documentIds, IDictionary <string, object?> fields)
        {
            var descriptor = new BulkDescriptor();

            // https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html
            // Refresh.WaitFor: waits until the indexing completed and changes are visisble for search
            _ = descriptor.Refresh(Refresh.WaitFor);

            foreach (var field in fields)
            {
                var     indexId   = Connection.GetIndexId(field.Key);
                dynamic updateDoc = new System.Dynamic.ExpandoObject();
                updateDoc.value = field.Value;

                foreach (var documentId in documentIds)
                {
                    _ = descriptor.Index <object>(op => op
                                                  .Index(indexId)
                                                  .Id(documentId)
                                                  .Document(updateDoc)
                                                  );
                }
            }
            var result = Connection.Client.Bulk(descriptor);
        }
Ejemplo n.º 6
0
        public async Task <IBulkResponse> SaveAsync(List <MediaRef> mediasRef, CancellationToken cancellationToken)
        {
            var descriptor = new BulkDescriptor();

            descriptor.IndexMany(mediasRef);
            descriptor.Refresh(Elasticsearch.Net.Refresh.True);
            return(await _elasticConnectionClient.Client.Value.BulkAsync(descriptor, cancellationToken));
        }
Ejemplo n.º 7
0
        public async Task <IBulkResponse> SaveAsync(List <SitePackChannel> sitepacks, CancellationToken cancellationToken)
        {
            var descriptor = new BulkDescriptor();

            descriptor.Pipeline(ElasticConnectionClient.SITE_PACK_PIPELINE);
            descriptor.IndexMany(sitepacks);
            descriptor.Refresh(Elasticsearch.Net.Refresh.True);
            return(await _elasticConnectionClient.Client.Value.BulkAsync(descriptor, cancellationToken));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Force update country field
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IBulkResponse> SyncCountrySitePackAsync(CancellationToken cancellationToken = default)
        {
            _logger.LogInformation($"Lister les SitePackChannels");

            var tvgSites = await _elasticConnectionClient.Client.Value.SearchAsync <SitePackChannel>(s => s
                                                                                                     .Index(_elasticConnectionClient.ElasticConfig.SitePackIndex)
                                                                                                     .Size(_elasticConnectionClient.ElasticConfig.MaxResultWindow)
                                                                                                     .From(0)
                                                                                                     , cancellationToken);

            var descriptor = new BulkDescriptor();

            descriptor.Pipeline(ElasticConnectionClient.SITE_PACK_PIPELINE);
            descriptor.IndexMany(tvgSites.Documents);
            descriptor.Refresh(Elasticsearch.Net.Refresh.True);

            return(await _elasticConnectionClient.Client.Value.BulkAsync(descriptor, cancellationToken));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 添加一个文档集合到索引中(此方法查询数据会有延时)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public virtual async Task <bool> BulkAsync <TEntity>(IEnumerable <TEntity> entities, string indexName = null) where TEntity : class
        {
            BulkDescriptor bulkDescriptor = new BulkDescriptor();

            if (!string.IsNullOrWhiteSpace(indexName))
            {
                bulkDescriptor.Index(indexName);
            }
            bulkDescriptor.Refresh(Elasticsearch.Net.Refresh.True);
            var response = await Context.BulkAsync(bulkDescriptor.IndexMany(entities));

            if (!response.IsValid)
            {
                Logger.LogError($"[Success:{response.ApiCall.Success}]\t{response.ApiCall.Uri}");
                Logger.LogError(response.ApiCall.DebugInformation);
                Logger.LogError(response.ApiCall.OriginalException?.Message);
            }
            return(response.ApiCall.Success);
        }
Ejemplo n.º 10
0
        public void BulkUpdateDocuments()
        {
            var client     = GetElasticClient();
            var descriptor = new BulkDescriptor();

            foreach (var i in Enumerable.Range(0, 10))
            {
                var isca = new IscaPartial {
                    SerialNumber = "0000" + i, LastPosition = DateTime.UtcNow
                };
                descriptor.Update <Isca, IscaPartial>(op => op.Doc(isca).RetriesOnConflict(3).Id(isca.SerialNumber));
            }
            descriptor.Refresh(Refresh.True);
            var result = client.Bulk(descriptor);

            //var client = new ElasticClient(config);
            //var isca = new Isca { NumeroSerie = "00001", Date = DateTime.UtcNow.ToString() };
            //var response = client.Update<Isca, Isca>(new DocumentPath<Isca>("00001"), u => u.Doc(isca));
        }
Ejemplo n.º 11
0
        public async Task <bool> DeleteAsync(IList <string> ids, Refresh refresh = Refresh.WaitFor)
        {
            if (!ids.HasItems())
            {
                throw new ArgumentException(string.Format(Utils.ARGUMENT_EMPTY_LIST_MESSAGE, nameof(ids)), nameof(ids));
            }

            var descriptor = new BulkDescriptor();

            foreach (var id in ids.Where(x => !string.IsNullOrWhiteSpace(x)))
            {
                descriptor.Delete <T>(x => x
                                      .Id(id));
            }

            descriptor.Refresh(refresh);

            var response = await _client.BulkAsync(descriptor);

            return(response.IsValid);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Synk picons
        /// </summary>
        /// <param name="picons"></param>
        /// <param name="reset">Delete the index and recreate it</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IBulkResponse> SynkAsync(IEnumerable <Picon> picons, bool reset = false, CancellationToken cancellationToken = default)
        {
            if (reset)
            {
                var res = await _elasticConnectionClient.Client.Value.DeleteByQueryAsync <Picon>(r => r.Query(rq => rq.Wildcard(f => f.Name, "*")), cancellationToken);

                if (!res.IsValid)
                {
                    if (res.ServerError.Status != 404)
                    {
                        throw new ApplicationException(res.ServerError.Error.Reason, res.OriginalException);
                    }
                }
            }

            var descriptor = new BulkDescriptor();

            descriptor.Pipeline(ElasticConnectionClient.PICONS_RETREIVE_CHANNEL_NUMBER_PIPELINE);
            descriptor.IndexMany(picons);
            descriptor.Refresh(Elasticsearch.Net.Refresh.True);

            _logger.LogInformation($"Sync picons count : {picons.Count()}");
            return(await _elasticConnectionClient.Client.Value.BulkAsync(descriptor, cancellationToken));
        }
Ejemplo n.º 13
0
 internal static BulkDescriptor Refresh(this BulkDescriptor descriptor, bool refresh = true)
 {
     return(descriptor.Refresh(refresh ? Elasticsearch.Net.Refresh.True : Elasticsearch.Net.Refresh.False));
 }