Beispiel #1
0
        /// <summary>
        /// Highlights chosen terms in a text, extracting the most relevant sections.
        /// </summary>
        /// <param name="input">Text to highlight terms in.</param>
        /// <param name="fieldName">Name of the field to highlight.</param>
        /// <param name="query">Search query.</param>
        /// <param name="engine">Search engine. Use <c>null</c> to fallback to simple string highlighting method.</param>
        /// <param name="preMatch">Pre matching HTML.</param>
        /// <param name="postMatch">Post matching HTML.</param>
        /// <returns>Highlighted text fragments.</returns>
        public string Highlight(
            string input,
            string fieldName,
            ISearchQuery query,
            ISearchEngine engine = null,
            string preMatch      = "<strong>",
            string postMatch     = "</strong>")
        {
            Guard.NotEmpty(fieldName, nameof(fieldName));

            if (query?.Term == null || input.IsEmpty())
            {
                return(input);
            }

            string hilite = null;

            if (engine != null)
            {
                try
                {
                    hilite = engine.Highlight(input, fieldName, preMatch, postMatch);
                }
                catch { }
            }

            if (hilite.HasValue())
            {
                return(hilite);
            }

            return(input.HighlightKeywords(query.Term, preMatch, postMatch));
        }