Esempio n. 1
0
        /// <summary>
        /// Index item
        /// </summary>
        /// <param name="pocoIndexer"></param>
        /// <param name="updateStatus"></param>
        /// <param name="count"></param>
        /// <param name="stopSignaled"></param>
        /// <returns></returns>
        public virtual string Index(IVulcanPocoIndexer pocoIndexer, Action <string> updateStatus, ref int count, ref bool stopSignaled)
        {
            if (pocoIndexer == null)
            {
                throw new ArgumentNullException($"{nameof(pocoIndexer)} cannot be null!");
            }

            var total    = pocoIndexer.TotalItems;
            var pageSize = pocoIndexer.PageSize;

            pageSize = pageSize < 1 ? 1 : pageSize; // don't allow 0 or negative
            var totalPages    = (total + pageSize - 1) / pageSize;
            var internalCount = 0;

            for (int page = 1; page <= totalPages; page++)
            {
                updateStatus?.Invoke("Indexing page " + page + " of " + totalPages + " items of " + pocoIndexer.IndexerName + " content!");
                var itemsToIndex = pocoIndexer.GetItems(page, pageSize);
                var firstItem    = itemsToIndex.FirstOrDefault();

                if (firstItem == null)
                {
                    break;
                }

                var itemType      = firstItem.GetType();
                var itemTypeName  = GetTypeName(firstItem);
                var operationType = typeof(BulkIndexOperation <>).MakeGenericType(itemType);
                var operations    = new List <IBulkOperation>();

                foreach (var item in itemsToIndex)
                {
                    if (stopSignaled)
                    {
                        return("Stop of job was called");
                    }

                    var indexItem = Activator.CreateInstance(operationType, item) as IBulkOperation;
                    indexItem.Type = new TypeName()
                    {
                        Name = itemTypeName, Type = itemType
                    };
                    indexItem.Id = pocoIndexer.GetItemIdentifier(item);
                    operations.Add(indexItem);

                    internalCount++;
                    count++;
                }

                // https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/bulk.html
                var request = new BulkRequest()
                {
                    Refresh     = true,
                    Consistency = Consistency.One,
                    Operations  = operations
                };

                var response = _InvariantClient.Bulk(request);
            }

            return("Indexed " + internalCount + " of " + total + " items of " + pocoIndexer.IndexerName + " content!");
        }
Esempio n. 2
0
        /// <summary>
        /// Index item
        /// </summary>
        /// <param name="pocoIndexer"></param>
        /// <param name="updateStatus"></param>
        /// <param name="count"></param>
        /// <param name="stopSignaled"></param>
        /// <param name="alias"></param>
        /// <returns></returns>
        public virtual string Index(IVulcanPocoIndexer pocoIndexer, Action <string> updateStatus, ref int count, ref bool stopSignaled, string alias = null)
        {
            if (pocoIndexer == null)
            {
                throw new ArgumentNullException($"{nameof(pocoIndexer)} cannot be null!");
            }
            VulcanHelper.GuardForNullAlias(ref alias);

            var total    = pocoIndexer.TotalItems;
            var pageSize = pocoIndexer.PageSize;

            pageSize = pageSize < 1 ? 1 : pageSize; // don't allow 0 or negative
            var totalPages    = (total + pageSize - 1) / pageSize;
            var internalCount = 0;

            var invariantClient = GetInvariantClient(alias);

            for (var page = 1; page <= totalPages; page++)
            {
                updateStatus?.Invoke($"Indexing page {page} of {totalPages} items of {pocoIndexer.IndexerName} content!");
                var itemsToIndex = pocoIndexer.GetItems(page, pageSize)?.ToList();
                var firstItem    = itemsToIndex?.FirstOrDefault();

                if (firstItem == null)
                {
                    break;
                }

                var itemType      = firstItem.GetType();
                var itemTypeName  = GetTypeName(firstItem);
                var operationType = typeof(BulkIndexOperation <>).MakeGenericType(itemType);
                var operations    = new List <IBulkOperation>();

                foreach (var item in itemsToIndex)
                {
                    if (stopSignaled)
                    {
                        return("Stop of job was called");
                    }

                    if (!(Activator.CreateInstance(operationType, item) is IBulkOperation indexItem))
                    {
                        throw new Exception("Unable to create item for bulk request");
                    }

                    indexItem.Type = new TypeName {
                        Name = itemTypeName, Type = itemType
                    };
                    indexItem.Id = pocoIndexer.GetItemIdentifier(item);
                    operations.Add(indexItem);

                    internalCount++;
                    count++;
                }

                // https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/bulk.html
                var request = new BulkRequest
                {
#if NEST2
                    Refresh     = true,
                    Consistency = Consistency.One,
#elif NEST5
                    Refresh = Refresh.True,
#endif
                    Operations = operations
                };

                invariantClient.Bulk(request);
            }

            return($"Indexed {internalCount} of {total} items of {pocoIndexer.IndexerName} content!");
        }