public void QueueDelete(SearchIndexDocumentModel document)
        {
            var request = new SearchIndexQueueRequestModel
            {
                Verb     = SearchIndexingVerbs.Delete,
                Index    = _searchConfiguration.AWSESIndex,
                Document = document
            };

            QueueRequest(request);
        }
        public void QueueUpsert(SearchIndexDocumentModel document)
        {
            if (string.IsNullOrWhiteSpace(document.Content))
            {
                LogHelper.Info <SearchIndexingQueueService>("Skipping Queue Upsert for node (ID: {0}, Title: {1}). Reason: Content is Null or WhiteSpace.", () => document.NodeId, () => document.Title);
                return;
            }

            var request = new SearchIndexQueueRequestModel
            {
                Verb     = SearchIndexingVerbs.Upsert,
                Index    = _searchConfiguration.AWSESIndex,
                Document = document
            };

            QueueRequest(request);
        }
Esempio n. 3
0
        public override void DeleteFromIndex(string nodeId)
        {
            if (_searchConfiguration.EnableIndexing == false)
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>("Skipping DeleteFromIndex. EnableIndexing is configured to disabled.");
                return;
            }

            var internalSearcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
            var parsedNodeId     = int.Parse(nodeId);
            var descendantPath   = string.Format(@"\-1\,*{0}\,*", parsedNodeId);
            var rawQuery         = string.Format("{0}:{1}", IndexPathFieldName, descendantPath);
            var c           = internalSearcher.CreateSearchCriteria();
            var filtered    = c.RawQuery(rawQuery);
            var descendants = internalSearcher.Search(filtered);

            DataService.LogService.AddVerboseLog(parsedNodeId, string.Format("DeleteFromIndex with query: {0} (found {1} descandant(s))", rawQuery, descendants.Count()));

            var document = new SearchIndexDocumentModel()
            {
                NodeId = parsedNodeId,
                Site   = _site
            };

            using (var searchIndexingQueueService = new SearchIndexingQueueService(_searchConfiguration))
            {
                searchIndexingQueueService.QueueDelete(document);

                //need to create a delete queue item for each one found
                foreach (var node in descendants)
                {
                    var descendantDocument = new SearchIndexDocumentModel()
                    {
                        NodeId = node.Id,
                        Site   = _site
                    };

                    searchIndexingQueueService.QueueDelete(document);
                }
            }
        }
Esempio n. 4
0
        protected void AddNodesToQueue(IEnumerable <XElement> nodes, string type)
        {
            if (_searchConfiguration.EnableIndexing == false)
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>("Skipping AddNodesToQueue. EnableIndexing is configured to disabled.");
                return;
            }

            var umbracoContext = GetUmbracoContext();

            if (umbracoContext == null)
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>(() => "Skipping AddNodesToQueue as a valid UmbracoContext was unobtainable.");
                return;
            }

            var fullUrlResolverService = new UmbracoContextFullUrlResolverService(umbracoContext);

            using (var searchIndexingQueueService = new SearchIndexingQueueService(_searchConfiguration))
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>("AddNodesToQueue: {0} node(s) for index type {1}.", () => nodes.Count(), () => type);

                foreach (var node in nodes)
                {
                    DataService.LogService.AddVerboseLog((int)node.Attribute("id"), string.Format("AddSingleNodeToIndex with type: {0}", type));

                    int nodeId = int.Parse(node.Attribute(XName.Get("id")).Value);

                    var values = GetDataToIndex(node, type);
                    //raise the event and assign the value to the returned data from the event
                    var indexingNodeDataArgs = new IndexingNodeDataEventArgs(node, nodeId, values, type);
                    OnGatheringNodeData(indexingNodeDataArgs);
                    values = indexingNodeDataArgs.Fields;

                    values.TryGetValue("nodeName", out string nodeName);
                    string publishDate = string.Empty;
                    if (!values.TryGetValue("reviewedDate", out publishDate))
                    {
                        if (!values.TryGetValue("publishedDate", out publishDate))
                        {
                            if (values.TryGetValue("updateDate", out publishDate))
                            {
                                // Defaulted to updateDate property
                            }
                        }
                    }

                    var document = new SearchIndexDocumentModel()
                    {
                        NodeId    = nodeId,
                        Site      = _site,
                        Published = DateTime.Parse(publishDate),
                        Title     = nodeName
                    };

                    // Determine if this is content or media
                    if (string.Equals(type, UmbracoExamine.IndexTypes.Content, StringComparison.OrdinalIgnoreCase))
                    {
                        var url = fullUrlResolverService.ResolveContentFullUrlById(nodeId);

                        // Get content based on content fields in order of priority
                        var contentBuilder = new StringBuilder();

                        foreach (var contentField in values.Where(x => IndexerData.UserFields.Select(y => y.Name).Contains(x.Key)))
                        {
                            var contentFieldValue = contentField.Value;
                            // Check if it has a value and append it
                            if (string.IsNullOrEmpty(contentFieldValue) == false)
                            {
                                if (contentFieldValue.DetectIsJson() && JsonUtility.TryParseJson(contentFieldValue, out object parsedJson))
                                {
                                    var processedJsonValue = ProcessJsonValue(parsedJson);

                                    if (string.IsNullOrEmpty(processedJsonValue) == false)
                                    {
                                        contentBuilder.AppendLine(processedJsonValue);
                                    }
                                }
                                else
                                {
                                    var sanitisedValue = contentFieldValue.StripHtml().Trim();

                                    if (string.IsNullOrWhiteSpace(sanitisedValue) == false)
                                    {
                                        contentBuilder.AppendLine(sanitisedValue);
                                    }
                                }
                            }
                        }


                        string content = contentBuilder.ToString().Trim();

                        document.Url     = url;
                        document.Content = content;

                        // index the node
                        searchIndexingQueueService.QueueUpsert(document);
                    }
                    else if (string.Equals(type, UmbracoExamine.IndexTypes.Media, StringComparison.OrdinalIgnoreCase))
                    {
                        var fileExtension = node.Elements().FirstOrDefault(x =>
                        {
                            if (x.Attribute("alias") != null)
                            {
                                return((string)x.Attribute("alias") == this.UmbracoExtensionProperty);
                            }
                            else
                            {
                                return(x.Name == this.UmbracoExtensionProperty);
                            }
                        });

                        if (HasNode(fileExtension) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: " + UmbracoExtensionProperty + " value was not present.");
                            continue;
                        }

                        if (!SupportedExtensions.Contains(fileExtension.Value, StringComparer.OrdinalIgnoreCase))
                        {
                            LogHelper.Info <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: File extension, " + fileExtension.Value + ", is not supported.");
                            continue;
                        }

                        var filePath = node.Elements().FirstOrDefault(x =>
                        {
                            if (x.Attribute("alias") != null)
                            {
                                return((string)x.Attribute("alias") == this.UmbracoFileProperty);
                            }
                            else
                            {
                                return(x.Name == this.UmbracoFileProperty);
                            }
                        });


                        if (HasNode(filePath) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: " + UmbracoFileProperty + " value was not present.");
                            continue;
                        }

                        //get the file path from the data service
                        var fullPath = this.DataService.MapPath((string)filePath);

                        if (System.IO.File.Exists(fullPath) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: Physical file does not exist.");
                            continue;
                        }

                        var fileInfo = new FileInfo(fullPath);
                        var url      = fullUrlResolverService.ResolveMediaFullUrl(filePath.Value);
                        // index the node

                        var pdf        = System.IO.File.ReadAllBytes(fullPath);
                        var pdfEncoded = Convert.ToBase64String(pdf);

                        document.Url               = url;
                        document.Content           = "Umbraco Media File";
                        document.FileBase64Encoded = pdfEncoded;
                        document.FileExtension     = fileInfo.Extension;
                        document.FileSizeInBytes   = fileInfo.Length;

                        searchIndexingQueueService.QueueUpsert(document);
                    }
                }
            }
        }