Try our conversational search powered by Generative AI!

Highlighting in GetContentResult

Vote:
 

I have the following block in my solution and my Find index:

// Attributes ommitted for clarity
public class FaqItemBlock : BlockData
{
    public virtual string Question { get; set; }
    public virtual XhtmlString Answer { get; set; }
    public virtual ContentArea BottomContent { get; set; }
}

I want to search for it (by free text), and get back all content, including highlights in the Question and Answer properties. I use GetContentResult for that purpose:

var contentQuery = _searchClient.Search<FaqItemBlock>().For(query).WithHighlight(x => x.Question);
var contentResult = contentQuery.GetContentResult();

Now I run into problems.

1. There is a hit, but no highlight. Am I using WithHighlight wrong?

2. How do I get highlights for both Question and Answer? Can I chain WithHighlight() to include highlights for multiple properties?

By the way, I have tried to achieve the same thing using a projection query and GetResult, but I need the content to display on the result page, e.g. content area, xhtml strings, etc... So GetContentResult seemed like a better fit.

#208349
Oct 22, 2019 13:01
Vote:
 

Hi Lasse,

Here a some examples exaplained to use highlited with multiple properties.

https://world.episerver.com/documentation/developer-guides/find/NET-Client-API/searching/Highlightning/

I think following code example should work for you.

searchResult = client.Search<BlogPost>()
  .For("Bacon")
  .Select(x => new { Title = x.Title.AsHighlighted(), 
                     Content = x.Content.AsHighlighted() 
                   })
  .GetResult();
#208382
Oct 22, 2019 19:57
Vote:
 

Hi Praful,

The problem with that code is that I need XhtmlString content back, but Xhtml properties are saved as html-stripped strings in the index by default.

So if I do:

searchResult = client.Search<FaqItemBlock>()
  .For(query)
  .Select(x => new {  
                     Answer = x.Answer,
                     AnswerHighlighted = x.Answer.AsHighlighted() 
                   })
  .GetResult();

"Answer" in my result is an empty XhtmlString, and "AnswerHighlighted" has a value with highlights, but no html tags. That's why I am using GetContentResult in my original post.

#208421
Edited, Oct 23, 2019 13:41
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.