private static IEnumerable <FullTextResult> ConvertSearchResultsToFullTextResults(ISearchResults SearchResults, Summarizer Summarizer, int PageNumber = 0)
        {
            IEnumerable <SearchResult> results;

            results = SearchResults.AsEnumerable();

            var numNodesInSet         = 0;
            var resultNumber          = 0;
            var allResults            = new List <FullTextResult>();
            var returnAllFieldsInXslt = Config.Instance.GetBooleanByKey("ReturnAllFieldsInXSLT");

            foreach (var result in results)
            {
                //var resultNumber = toSkip + numNodesInSet + 1;
                resultNumber++;
                OnResultOutput(new ResultOutputEventArgs(result, PageNumber, resultNumber, numNodesInSet + 1));

                var ftResult = new FullTextResult();
                ftResult.ExamineResult = result;
                ftResult.NodeId        = result.Id;
                ftResult.Score         = result.Score;
                ftResult.Number        = resultNumber;

                var nodeTypeAlias = result.Fields.ContainsKey("nodeTypeAlias") ? result.Fields["nodeTypeAlias"] : "[Need to add 'nodeTypeAlias' to <IndexAttributeFields>]";
                ftResult.NodeTypeAlias = nodeTypeAlias;

                //var node = new XElement("node",
                //    new XAttribute("id", ),
                //    new XAttribute("score", ),
                //    new XAttribute("number", )
                //);

                if (returnAllFieldsInXslt)
                {
                    //Add all fields from index, you would think this would slow things
                    //down, but it doesn't (that much) really, could be useful
                    //foreach (var field in result.Fields)
                    //{
                    //    ftResult.Fields.Add(field.Key, field.Value);
                    //}
                    ftResult.Fields = result.Fields;
                }

                //Add title (optionally highlighted)
                string title;
                Summarizer.GetTitle(result, out title);
                ftResult.Title = title;

                //Add Summary(optionally highlighted)
                string summary;
                Summarizer.GetSummary(result, out summary);
                ftResult.Summary = summary;

                allResults.Add(ftResult);
                numNodesInSet++;
            }

            return(allResults);
        }
Example #2
0
        /// <summary>
        /// Take ISearchResults from examine, create title and body summary, and convert to an XML document
        /// This is broadly based off the same function in the Examine codebase, the XML it returns should be
        /// broadly compatible, that seems best...
        /// </summary>
        /// <returns>XPathNodeIterator to return to Umbraco XSLT foreach</returns>
        static XPathNodeIterator ResultsAsXml(ISearchResults searchResults, Summarizer Summarizer, int pageNumber = 0, int pageLength = 0, Stopwatch stopwatch = null)
        {
            var output        = new XDocument();
            var numNodesInSet = 0;
            var numResults    = searchResults.TotalItemCount;

            if (numResults < 1)
            {
                return(ReturnError("NoResults", "Your search returned no results"));
            }
            IEnumerable <SearchResult> results;
            var toSkip = 0;

            if (pageLength > 0)
            {
                if (pageNumber > 1)
                {
                    toSkip = (pageNumber - 1) * pageLength;
                }
                results = searchResults.Skip(toSkip).Take(pageLength);
            }
            else
            {
                results = searchResults.AsEnumerable();
            }
            var rootNode              = new XElement("results");
            var nodesNode             = new XElement("nodes");
            var returnAllFieldsInXslt = Config.Instance.GetBooleanByKey("ReturnAllFieldsInXSLT");

            foreach (var result in results)
            {
                var resultNumber = toSkip + numNodesInSet + 1;
                OnResultOutput(new ResultOutputEventArgs(result, pageNumber, resultNumber, numNodesInSet + 1));
                var node = new XElement("node",
                                        new XAttribute("id", result.Id),
                                        new XAttribute("score", result.Score),
                                        new XAttribute("number", resultNumber)
                                        );
                if (returnAllFieldsInXslt)
                {
                    //Add all fields from index, you would think this would slow things
                    //down, but it doesn't (that much) really, could be useful
                    foreach (var field in result.Fields)
                    {
                        node.Add(
                            new XElement("data",
                                         new XAttribute("alias", field.Key),
                                         new XCData(field.Value)
                                         ));
                    }
                }
                //Add title (optionally highlighted)
                string title;
                Summarizer.GetTitle(result, out title);
                node.Add(
                    new XElement("data",
                                 new XAttribute("alias", "FullTextTitle"),
                                 new XCData(title)
                                 ));
                //Add Summary(optionally highlighted)
                string summary;
                Summarizer.GetSummary(result, out summary);
                node.Add(
                    new XElement("data",
                                 new XAttribute("alias", "FullTextSummary"),
                                 new XCData(summary)
                                 ));

                nodesNode.Add(node);
                numNodesInSet++;
            }
            if (numNodesInSet > 0)
            {
                rootNode.Add(nodesNode);
                var summary = new XElement("summary");
                summary.Add(new XAttribute("numResults", numResults));
                var numPages = numResults % pageLength == 0 ? numResults / pageLength : numResults / pageLength + 1;
                summary.Add(new XAttribute("numPages", numPages));
                if (stopwatch != null)
                {
                    stopwatch.Stop();
                    double millisecs = stopwatch.ElapsedMilliseconds;
                    var    numSecs   = Math.Round((millisecs / 1000), 3);
                    summary.Add(new XAttribute("timeTaken", numSecs));
                }
                summary.Add(new XAttribute("firstResult", toSkip + 1));
                var lastResult = toSkip + pageLength;
                if (lastResult > numResults)
                {
                    lastResult = numResults;
                }
                summary.Add(new XAttribute("lastResult", lastResult));
                rootNode.Add(summary);
                output.Add(rootNode);
            }
            else
            {
                return(ReturnError("NoPage", "Pagination incorrectly set up, no results on page " + pageNumber));
            }

            return(output.CreateNavigator().Select("/"));
        }