Ejemplo n.º 1
0
        public static IEnumerable <PageBase> GetPageSuggestions(this UmbracoHelper umbraco, HttpRequestBase request, bool useFuzzy = true)
        {
            var targetUrl = request.Url;

            // attempt to get a page name...
            var urlParts = targetUrl.AbsolutePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var filter   = urlParts.Last().Replace('-', ' ');

            if (filter.Contains('.'))
            {
                filter = filter.Substring(0, filter.LastIndexOf('.'));
            }

            try
            {
                var searcher       = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var alternateTitle = "_title";
                var criteria       = searcher.CreateSearchCriteria(BooleanOperation.Or);

                criteria.NodeName(filter.Boost(10.0f));
                criteria.NodeName(filter.MultipleCharacterWildcard());
                if (useFuzzy)
                {
                    criteria.NodeName(filter.Fuzzy(0.5f));
                }

                criteria.Field(alternateTitle, filter.Boost(7.0f));
                criteria.Field(alternateTitle, filter.MultipleCharacterWildcard());
                if (useFuzzy)
                {
                    criteria.Field(alternateTitle, filter.Fuzzy(0.8f));
                }

                var results = searcher.Search(criteria);
                if (results.TotalItemCount == 0 && !useFuzzy)
                {
                    // Try again with fuzzy search enabled
                    return(umbraco.GetPageSuggestions(request, true));
                }
                return(results.Select(r => umbraco.TypedContent(r.Id).As <PageBase>()).Where(p => p != null));
            }
            catch
            {
            }
            return(Enumerable.Empty <PageBase>());
        }