示例#1
0
        /// <summary>
        /// Deletes the specified tag.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="force">if set to <c>true</c> [force].</param>
        /// <param name="cleanDocuments">if set to <c>true</c> [clean documents].</param>
        /// <returns></returns>
        public void Delete(string dataSetName, string id, bool force, bool cleanDocuments)
        {
            var dataSet       = DataSet(dataSetName).DataSet;
            var result        = new BulkResults();
            var tagQuery      = TagQuery(dataSetName);
            var documentQuery = DocumentQuery(dataSetName);
            var tagElastic    = tagQuery.Get(id);
            var childTagIds   = tagQuery.GetDescendantTagIds(id);

            TagElastic parentTagElastic = null;

            if (!string.IsNullOrWhiteSpace(tagElastic.ParentId()))
            {
                parentTagElastic = tagQuery.Get(tagElastic.ParentId());
            }

            if (cleanDocuments)
            {
                foreach (var childTagId in childTagIds)
                {
                    foreach (var documentElastic in documentQuery.GetByTagId(childTagId, dataSet.TagField))
                    {
                        DocumentHelper.RemoveTagIds(documentElastic.DocumentObject, dataSet.TagField, new List <string> {
                            childTagId
                        });
                        documentQuery.Index(documentElastic);
                    }
                }
            }

            var deleteTagIds = new List <string> {
                id
            };

            if (force)
            {
                deleteTagIds.AddRange(childTagIds);
            }

            tagQuery.DeleteByIds(deleteTagIds);
            tagQuery.AdjustLeafStatus(parentTagElastic);
        }
        public BulkResults Bulk(string dataSetName, IEnumerable <object> documents, long requestSize, int parallelLimit)
        {
            var dataSet            = DataSet(dataSetName).DataSet;
            var results            = new BulkResults();
            var validatedDocuments = documents
                                     .Select((document, index) =>
                                             new
            {
                Index    = index,
                Result   = ValidateDocument(dataSetName, document),
                Document = document
            })
                                     .ToList();

            var invalidDocumentResults = validatedDocuments
                                         .Where(document => document.Result.IsFailure)
                                         .Select(document => BulkResult.Create(
                                                     DocumentHelper.GetValue(document.Document, dataSet.IdField)?.ToString(),
                                                     StatusCodes.Status400BadRequest,
                                                     document.Result.Error)
                                                 )
                                         .ToList();

            results.Results.AddRange(invalidDocumentResults);

            var documentElastics = validatedDocuments
                                   .Where(obj => obj.Result.IsSuccess)
                                   .Select(document => new DocumentElastic
            {
                Id             = DocumentHelper.GetValue(document.Document, dataSet.IdField).ToString(),
                DocumentObject = document.Document,
                Text           = DocumentHelper.GetConcatenatedText(document.Document, dataSet.InterpretedFields)
            })
                                   .ToList();

            var bulkResponseStruct = DocumentQuery(dataSetName).ParallelBulkIndex(documentElastics, parallelLimit, requestSize);

            results.Results.AddRange(bulkResponseStruct.ToBulkResult());

            return(results);
        }
        public void CopyOrMove(string processId, string dataSetName, IEnumerable <string> documentIds, string targetDataSetName, int parallelLimit, bool isMove, CancellationToken token, string hostUrl)
        {
            var results = new BulkResults();
            //// TODO: Validate target schema
            var copiedDocumentIds = new ConcurrentBag <string>();
            var parallelOptions   = new ParallelOptions {
                MaxDegreeOfParallelism = parallelLimit
            };
            var sourceDocumentQuery = DocumentQuery(dataSetName);
            var targetDocumentQuery = DocumentQuery(targetDataSetName);
            var allCount            = documentIds.Count();

            Parallel.ForEach(
                documentIds.Batch(siteConfig.Resources.MaxSearchBulkCount),
                parallelOptions,
                (batchIds, loopState) =>
            {
                try
                {
                    if (token.IsCancellationRequested)
                    {
                        processHandler.Cancelled(processId);
                        return;
                    }
                    var batchDocuments    = sourceDocumentQuery.Get(batchIds);
                    var interpretedFields = DataSet(dataSetName).DataSet.InterpretedFields;

                    foreach (var document in batchDocuments.Where(doc => string.IsNullOrEmpty(doc.Text)))
                    {
                        document.Text = DocumentHelper.GetConcatenatedText(document.DocumentObject, interpretedFields);
                    }

                    var bulkResponse = targetDocumentQuery.Index(batchDocuments);
                    results.Results.AddRange(bulkResponse.ToBulkResult());
                    processHandler.Changed(processId, Math.Round(results.Results.Count / (double)allCount * 100, 2));
                }
                catch (Exception ex)
                {
                    processHandler.Interrupted(processId, ex);
                    loopState.Stop();
                }
            });
            targetDocumentQuery.Flush();

            var succeedDocumentIds = results.Results.Where(r => r.StatusCode == StatusCodes.Status200OK).Select(r => r.Id).ToList();
            var failedDocumentIds  = results.Results.Where(r => r.StatusCode != StatusCodes.Status200OK).Select(r => r.Id).ToList();

            if (isMove)
            {
                if (succeedDocumentIds.Any())
                {
                    sourceDocumentQuery.Delete(succeedDocumentIds);
                    sourceDocumentQuery.Flush();
                }
            }

            // save the response
            var fileName   = string.Format("{0}.json", processId);
            var resultPath = string.Format("{0}/{1}", siteConfig.Directory.User, fileName);

            File.AppendAllText(resultPath, JsonConvert.SerializeObject(results.Results.OrderByDescending(r => r.StatusCode), Formatting.Indented));
            var url = string.Format("{0}{1}/{2}", hostUrl, Constants.FilesPath, fileName);

            processHandler.Finished(processId,
                                    string.Format("{0}\n{1}",
                                                  string.Format(
                                                      isMove ?
                                                      DocumentResources.MoveFinishedFrom_0_To_1_Succeeded_2_Failed_3 :
                                                      DocumentResources.CopyFinishedFrom_0_To_1_Succeeded_2_Failed_3,
                                                      dataSetName, targetDataSetName, succeedDocumentIds.Count, failedDocumentIds.Count),
                                                  string.Format(DocumentResources.ResultFileCanBeDownloadFromHere_0, url)
                                                  ));
        }
示例#4
0
        /// <summary>
        /// Bulk tag creation.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <returns></returns>
        public Result <BulkResults> BulkCreate(string dataSetName, List <Tag> tags, int parallelLimit, long requestSize)
        {
            var results = new BulkResults();

            var tagIsInteger = DataSet(dataSetName).TagIsInteger;

            if (tagIsInteger && tags.Any(tag => tag.Id.Any(ch => !char.IsDigit(ch))))
            {
                results.Results = tags
                                  .Where(tag => tag.Id.Any(ch => !char.IsDigit(ch)))
                                  .Select(t => BulkResult.Create(
                                              t.Id,
                                              StatusCodes.Status400BadRequest,
                                              string.Format(TagResources.TagIdShouldBeIntegerType, t.ParentId, t.Id)))
                                  .ToList();

                return(Result.Ok(results));
            }

            var tagIdsByLevel = TagHelper.GetTagIdsByLevel(tags, item => item.ParentId, item => item.Id);
            var validIds      = tagIdsByLevel.SelectMany(l => l.Value).ToList();
            var invalidIds    = tags.Select(t => t.Id).Except(validIds);

            if (invalidIds.Any())
            {
                results.Results = tags
                                  .Where(t => invalidIds.Contains(t.Id))
                                  .Select(t => BulkResult.Create(t.Id, StatusCodes.Status404NotFound, string.Format(TagResources.ParentId_0_NotFoundInTagWithId_1, t.ParentId, t.Id))).ToList();

                // returns with OK status, individual items contain error code
                return(Result.Ok(results));
            }

            var orderedTagElasticList = tagIdsByLevel
                                        .SelectMany(dic => dic.Value)
                                        .Select(id =>
            {
                var tag        = tags.FirstOrDefault(t => t.Id == id);
                var tagElastic = new TagElastic
                {
                    Id           = tag.Id,
                    Name         = tag.Name,
                    ParentIdList = new List <string>()
                };
                if (!string.IsNullOrWhiteSpace(tag.ParentId))
                {
                    tagElastic.ParentIdList.Add(tag.ParentId);
                }
                return(tagElastic);
            })
                                        .ToList();

            TagHelper.AdjustTagElastics(orderedTagElasticList);

            var tagQuery = TagQuery(dataSetName);

            tagQuery.DeleteAll();

            var bulkResponseStruct = tagQuery.ParallelBulkIndex(orderedTagElasticList, parallelLimit, requestSize);

            results.Results.AddRange(bulkResponseStruct.ToBulkResult());

            return(Result.Ok(results));
        }