Ejemplo n.º 1
0
        /// <summary>
        /// Searches among all help topics by the given search term.
        /// </summary>
        /// <param name="term">Search term.</param>
        /// <param name="offset">Zero-based offset of the matching record.</param>
        /// <param name="amount">Amount of matching records to return.</param>
        /// <returns>Matching help topics.</returns>
        public HelpTopicSearchResultSet Search(string term, int offset = 0, int amount = 20)
        {
            HelpTopicSearchResultSet ret = new HelpTopicSearchResultSet();

            using (var repo = base.OpenRespository())
                ret = repo.Search(term, offset, amount);

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Searches among all help topics by the given search term.
        /// </summary>
        /// <param name="term">Search term.</param>
        /// <param name="offset">Zero-based offset of the matching record.</param>
        /// <param name="amount">Amount of matching records to return.</param>
        /// <returns>Matching help topics.</returns>
        public HelpTopicSearchResultSet Search(string term, int offset = 0, int amount = 20)
        {
            HelpTopicSearchResultSet ret = new HelpTopicSearchResultSet();

            // Normalizing search terms.
            string[] terms = (term ?? string.Empty)
                             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                             .Select(t => t.Trim().ToLowerInvariant()).Distinct().ToArray();

            // Limiting the maximum number of terms.
            if (terms.Length > 10)
            {
                terms = terms.Take(10).ToArray();
            }

            if (terms.Any())
            {
                // Space-separated term sequence for Raven.
                string termSequence = string.Join(" ", terms);

                // Getting results with highlights.
                var results = base.Session.Query <HelpTopic>("HelpTopics/ByTitleAndBody")
                              .Search(topic => topic.Title, termSequence)
                              .Search(topic => topic.Body, termSequence)
                              .ToList()
                              .Where(topic => topic.ReferenceKey != "index")
                              .ToList()
                              .OrderBy(topic => topic.Score.Total);

                // Materializing the count.
                ret.Total = results.Count();

                // Materializing results for the current page.
                ret.Results = results.Skip(offset).Take(amount).ToList().Select(topic => new HelpTopicSearchResult()
                {
                    TopicId           = topic.Id,
                    TopicReferenceKey = topic.ReferenceKey,
                    Title             = topic.Title,
                    Summary           = HelpTopicRepository.SummarizeAndHighlightMatches(topic.Body, terms)
                }).ToList();
            }

            return(ret);
        }