November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
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));
}
I'm unable to get the HighlightSpec working, the following code returns the full content of the Content property.