/// <summary> /// Delete documents from index document by company's user /// </summary> /// <param name="lstBlobs"></param> /// <param name="usrUser"></param> /// <returns></returns> public List <BlobStructure> DeleteDocumentsFromIndex(List <BlobStructure> lstBlobs, User usrUser) { var idxSearch = CreateIfNotExistIndex(usrUser, cnsIndexTypes.Documents); // Initialize client search var client = new SearchIndexClient(ConfigurationManager.AppSettings["SearchServiceName"], idxSearch.IndexName, new SearchCredentials(ConfigurationManager.AppSettings["SearchApiKey"])); // Only uploaded files foreach (var blbBlob in lstBlobs.Where(x => x.IsUploaded)) { var dcmMetadata = blbBlob.dctMetadata.ToObject <clsDocumentPdfIndex>(); var strFileNameWithOutExt = blbBlob.fntNameStr.Split('.')[0]; // If files not are valid documents if (!strExtensions.Equals(dcmMetadata.Extension)) { continue; } try { var lstActions = new List <IndexAction <clsDocumentPdfIndex> >(); // download blob like stream var var streamFile = new Blobs.BlobStorage().fntGetStreamBlob(blbBlob); streamFile.Position = 0; // Create a index action by each page using (PdfReader reader = new PdfReader(streamFile)) { for (int indexPage = 1; indexPage <= reader.NumberOfPages; indexPage++) { // add index page before extension file dcmMetadata.Id = MakeSafeId(strFileNameWithOutExt + "_" + indexPage + "." + dcmMetadata.Extension); lstActions.Add(IndexAction.Delete(dcmMetadata)); } } // Index batch process for (int i = 0; i < (int)Math.Ceiling(lstActions.Count / (double)intRegistersSkip); i++) { client.Documents.Index(new IndexBatch <clsDocumentPdfIndex>(lstActions.Skip(i * intRegistersSkip).Take(intRegistersSkip))); } lstBlobs.First(l => l.fntNameStr == blbBlob.fntNameStr).IsIndexed = true; } catch (Exception) { lstBlobs.First(l => l.fntNameStr == blbBlob.fntNameStr).IsIndexed = false; } } return(lstBlobs); }
//private readonly string[] lstExtensions = { "pdf", "doc", "docx" }; /// <summary> /// Add documents to index document by company's user /// </summary> /// <param name="lstBlobs">list of blobs</param> /// <param name="usrUser">user data</param> /// <returns></returns> public List <BlobStructure> AddDocumentsToIndex(List <BlobStructure> lstBlobs, User usrUser) { var idxSearch = CreateIfNotExistIndex(usrUser, cnsIndexTypes.Documents); // Initialize client search var client = new SearchIndexClient(ConfigurationManager.AppSettings["SearchServiceName"], idxSearch.IndexName, new SearchCredentials(ConfigurationManager.AppSettings["SearchApiKey"])); // Only uploaded files foreach (var blbBlob in lstBlobs.Where(x => x.IsUploaded)) { // remove path metadata (local path is not stored) blbBlob.dctMetadata.Remove(cnsBlobMetadata.Path); // transform tags to collection string var strTags = blbBlob.dctMetadata[cnsBlobMetadata.Tags]; if (strTags != null) { blbBlob.dctMetadata[cnsBlobMetadata.Tags] = strTags.ToString().Split(','); } // get full blob name with out extension var strFileNameWithOutExt = blbBlob.fntNameStr.Split('.')[0]; // If files not are valid documents if (!strExtensions.Equals(blbBlob.dctMetadata[cnsBlobMetadata.Extension].ToString())) { continue; } try { var lstActions = new List <IndexAction <clsDocumentPdfIndex> >(); // download blob like stream var var streamFile = new Blobs.BlobStorage().fntGetStreamBlob(blbBlob); streamFile.Position = 0; // Create a index action by each page using (PdfReader reader = new PdfReader(streamFile)) { for (int indexPage = 1; indexPage <= reader.NumberOfPages; indexPage++) { // transform dictionary to object var dcmMetadata = blbBlob.dctMetadata.ToObject <clsDocumentPdfIndex>(); dcmMetadata.Container = blbBlob.cntContainer.fntFullNameStr; dcmMetadata.Uri = blbBlob.fntUri.AbsoluteUri; dcmMetadata.FullName = blbBlob.fntNameStr; // add index page before extension file dcmMetadata.Id = MakeSafeId(strFileNameWithOutExt + "_" + indexPage + "." + dcmMetadata.Extension); dcmMetadata.Content = PdfTextExtractor.GetTextFromPage(reader, indexPage); dcmMetadata.Page = indexPage; lstActions.Add(IndexAction.MergeOrUpload(dcmMetadata)); } } // Index batch process for (int i = 0; i < (int)Math.Ceiling(lstActions.Count / (double)intRegistersSkip); i++) { client.Documents.Index(new IndexBatch <clsDocumentPdfIndex>(lstActions.Skip(i * intRegistersSkip).Take(intRegistersSkip))); } lstBlobs.First(l => l.fntNameStr == blbBlob.fntNameStr).IsIndexed = true; } catch (Exception ex) { lstBlobs.First(l => l.fntNameStr == blbBlob.fntNameStr).IsIndexed = false; } } return(lstBlobs); }