Try our conversational search powered by Generative AI!

Problems with HighlightSpec

Vote:
 

I'm unable to get the HighlightSpec working, the following code returns the full content of the Content property.

var results = query.Select(
x =>
new SearchResult
{
Heading = x.Heading,
Url = x.Url,
Content = string.IsNullOrEmpty(x.Content.AsHighlighted()) ?
x.Content.AsCropped(200) : 
x.Content.AsHighlighted(new HighlightSpec 
{ 
NumberOfFragments = 1,
FragmentSize = 200
})
})
.GetResult();

    

#61584
Sep 24, 2012 16:04
Vote:
 

Hi Olov,

Unfortunately the current version of Find, or rather or ElasticSearch, doesn't support fetching the same field highlighted in different ways and therefor you need to use the highlightspec in the first call to AsHighlighted as well. Try this:

var highlightSpec = new HighlightSpec 
{ 
NumberOfFragments = 1,
FragmentSize = 200
};

var results = query.Select(
x =>
new SearchResult
{
Heading = x.Heading,
Url = x.Url,
Content = string.IsNullOrEmpty(x.Content.AsHighlighted(highlightSpec)) ?
x.Content.AsCropped(200) : 
x.Content.AsHighlighted(highlightSpec)
})
.GetResult();

To make the code more easy to manager you could also extract the method for getting the excerpt:

var highlightSpec = new HighlightSpec 
{ 
NumberOfFragments = 1,
FragmentSize = 200
};

var results = query.Select(
x =>
new SearchResult
{
Heading = x.Heading,
Url = x.Url,
Content = FirstNotEmpty(x.Content.AsHighlighted(highlightSpec), x.Content.AsCropped(200))
.GetResult();

///in class scope
private string FirstNotEmpty(params string[] values)
{
  return values.FirstOrDefault(x => !string.IsNullOrEmpty(x));
}

    

 

   

 

#61602
Sep 25, 2012 1:07
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.