With the Truffler .NET API (introduced here) we strive very hard to make it easy for developers to build great search and querying functionality. In my opinion search engines are often underused when building web sites today and by bringing search to the domain the developer is otherwise working in rather than the opposite I believe we can help developers create more business value. Both in term of building functionality faster and in terms of building better solutions.
So far I think we’ve been fairly good at doing this in the .NET API and that’s the impression I get from the feedback we’ve gotten so far to. For instance, Allan Thraen, legendary developer and product manager for EPiServer CMS told me:
“It's all a matter of the UX. Always! And in your case the users are developers and I'd say that it looks as if you've done the coolest search API I've ever seen.”
Just had to show off that quote :)
However, there’s one area where the .NET API has been awful in terms of usability for developers so far – highlighting. With crawler based search engines this is usually fairly easy as there’s options are limited due to that the search engine doesn’t know the structure of the data. But with search engines such as SOLR or ElasticSearch (the latter of which Truffler is built on) you have a lot of options. You may want highlights from a specific field. You may also want one long text with keywords highlighted in one situation and many shorter fragments in another. Up until recently this wealth of options was reflected in the .NET API which of course in some regard is good, but it also made it rather complex to deal with highlights.
So we did some thinking on how we, as developers, would like to work with highlights. First of all, we’re most likely mainly interested in highlights in situations where we don’t want the whole indexed object returned but a subset of it’s fields as we’re going to present it in a search result. Second, we don’t really want to regard highlights as something separate from the actual object returned. So, we decided to make it possible to fetch highlights when doing projections using the Select method.
We accomplished this by introducing a “magic” extension method for strings named AsHighlighted. What it does is best illustrated with a code example. Imagine we’ve indexed an object of type BlogPost that has a Title and a Content property and we want to search for BlogPosts and retrieve the Content property with keywords highlighted. Here’s what we could do then.
searchResult = client.Search<BlogPost>() .For("Bacon") .Select(x => new { Title = x.Title, Content = x.Content.AsHighlighted() }) .GetResult();
Without any parameters a field projected with AsHighlighted will return the whole string. That can be tweaked by passing it an instance of the HighlightSpec class. For instance, to retrieve (a maximum of) two fragments that are each 200 characters long we could do this:
searchResult = client.Search<BlogPost>() .For("Bacon") .Select(x => new { Title = x.Title, Content = x.Content.AsHighlighted( new HighlightSpec { NumberOfFragments = 2, FragmentSize = 200 }) }) .GetResult();
The two fragments will be concatenated with a space between them but that can be tweaked. More on that in the documentation. We’re also able to control what tag will be used to highlight keywords (the default is em):
searchResult = client.Search<BlogPost>() .For("Bacon") .Select(x => new { Title = x.Title, Content = x.Content.AsHighlighted( new HighlightSpec { PreTag = "<strong>", PostTag = "</strong>" }) }) .GetResult();
In my opinion it can’t get much easier than this while still allowing us as developers to be in control. If you have a different idea on how to work with highlights though, we’d love to hear it!
New version of the .NET API
There’s quite a lot of other things that have been added or fixed during the last few days so if you’re trying out Truffler, make sure to get the latest version of the .NET API. For those waiting for the EPiServer CMS integration, stay tuned. A first version should be available very soon and we’re eager to hear you feedback!
PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.
Similar articles
- Truffler update – dotting the i’s and crossing the t’s
- Building a search page for an EPiServer site using Truffler - Part 2
- Introducing Truffler – Advanced search made easy
- Would you like some business logic with that search?
- First version of the Truffler EPiServer CMS integration
- Cool new features in the Truffler .NET API
- Building a search page for an EPiServer site using Truffler
- New in EPiServer Find – Unified Search
My book
Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!
Comments
comments powered by Disqus